Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Decrypted string using XOR not producing original/encrypted string
    primarykey
    data
    text
    <p>I've been provided with specific steps regarding encryption which is essentially converting a string to a byte array and "XORing" each byte array element with a specific number and converting this new byte array to a string. </p> <p>As for the decryption I'm to reconvert the decrypted string into a byte array, take each element XOR this with the number used in the encryption and convert the new decrypted array to a string to give me the exact string i started with.</p> <p>Unfortunately I'm unable to do obtain the string I started with e.g. when i attempt encrypting 6,7,8,9,10 the decryption returns B@722b9406 not 6,7,8,9,10. Any ideas why this is so?</p> <p>My ENCRYPTION CODE is below:</p> <pre><code> //Take each array element and do "Exclusive or" with "50" public void XORUnencryptedByteArray() { System.out.println("\nPART 3:\nXORUnencryptedByteArray() - " + "UnencryptedStringAsByteArray.length IS: " + UnencryptedStringAsByteArray.length); byte[] UnencryptedByteArraybyXOR = new byte[UnencryptedStringAsByteArray.length]; int XOR_Value = 50; byte XOR_Value_as_byte = (byte) XOR_Value; System.out.println("\nPART 3:\nXORUnencryptedByteArray() - " + "XOR_Value_as_byte IS: " + XOR_Value_as_byte + "\n"); for ( int j = 0; j &lt; UnencryptedStringAsByteArray.length; j++ ) { System.out.println("\nPOSITION " + j + " OF UnencryptedStringAsByteArray[j] CONTAINS: " + UnencryptedStringAsByteArray[j]); System.out.println("\n" + UnencryptedStringAsByteArray[j] + " ^ " + XOR_Value_as_byte + " IS: " + (UnencryptedStringAsByteArray[j] ^ XOR_Value_as_byte)); } for (int i = 0; i &lt; UnencryptedByteArraybyXOR.length; i++) { UnencryptedByteArraybyXOR[i] = (byte) (UnencryptedStringAsByteArray[i] ^ XOR_Value_as_byte); System.out.println( "\nPOSITION " + i + " OF UnencryptedByteArraybyXOR[i] IS: " + UnencryptedByteArraybyXOR[i]); } //Collect all encrypted array elements and convert the byte array to string System.out.println("\nUnencryptedByteArraybyXOR.length IS: " + UnencryptedByteArraybyXOR.length); System.out.println("\nUnencryptedByteArraybyXOR.toString() AS STRING IS: " + UnencryptedByteArraybyXOR.toString()); UnencryptedByteArraybyXORAsString = new String(UnencryptedByteArraybyXOR); //System.out.println("\nUnencryptedByteArraybyXORAsString IS: " + UnencryptedByteArraybyXORAsString); WriteStringToFile(UnencryptedByteArraybyXOR.toString()); } public void WriteStringToFile(String UnencryptedByteArraybyXORAsString) { try { String str = UnencryptedByteArraybyXORAsString; File newTextFile = new File("/Users/anmonari/Desktop/textfile.txt"); FileWriter fw = new FileWriter(newTextFile); fw.write(str); fw.close(); } catch (IOException iox) { //do stuff with exception iox.printStackTrace(); } } </code></pre> <p>my DECRYPTION code is below:</p> <pre><code> public void XORDecryptedByteArray(byte[] encryptedStringAsByteArray) { System.out.println("\nXORencryptedByteArray() - " + "encryptedStringAsByteArray.length IS: " + encryptedStringAsByteArray.length); byte[] encryptedByteArraybyXOR = new byte[encryptedStringAsByteArray.length]; int XOR_Value = 50; byte XOR_Value_as_byte = (byte) XOR_Value; System.out.println("\nXORencryptedByteArray() - " + "XOR_Value_as_byte IS: " + XOR_Value_as_byte + "\n"); for ( int j = 0; j &lt; encryptedStringAsByteArray.length; j++ ) { System.out.println("\n" + encryptedStringAsByteArray[j] + " ^ " + XOR_Value_as_byte + " IS: " + (encryptedStringAsByteArray[j] ^ XOR_Value_as_byte)); } for (int i = 0; i &lt; encryptedByteArraybyXOR.length; i++) { encryptedByteArraybyXOR[i] = (byte) (encryptedStringAsByteArray[i] ^ XOR_Value_as_byte); System.out.println("\n" + "POSITION " + i + " OF encryptedByteArraybyXOR[i] CONTAINS: " + encryptedByteArraybyXOR[i]); } System.out.println("\ndecryptedByteArraybyXOR.length IS: " + encryptedByteArraybyXOR.length); System.out.println("\ndecryptedByteArraybyXOR.toString() AS STRING IS: " + encryptedByteArraybyXOR.toString()); } </code></pre> <p>ANY ASSISTANCE IS APPRECIATED !!!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload