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.
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.
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?
My ENCRYPTION CODE is below:
//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 < 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 < 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();
}
}
my DECRYPTION code is below:
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 < 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 < 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());
}
ANY ASSISTANCE IS APPRECIATED !!!!!