String to byte conversion
I'm using the following code to encrypt a String (inText) whose output is a byte array named cipherText;
The cipherText is printed in a textbox as follows:
Everything works fine until here. However, in the decryption section, I need to get the contents of the textbox as a byte array again. For this purpose, I use the following code:
When I try to do the encryption it gives a 'bad size' error. I found out that the lenghts of the two byte arrays cipherText and cText aren't equal:
The 'result' textbox always prints 'false' meaning that the two byte arrays cipherText and cText aren't equal. However I convert cipherText to a String to print in a textbox and then read the contents of that textbox, convert to String back and then to Byte Aray again. Obviously some data is lost during these conversions. What would you recommend to solve this problem?
boolean res=Arrays.equals(cText, cipherText);
Re: String to byte conversion
Print them both out to see what the difference is. The Arrays toString() method can format arrays.
I found out that the lenghts of the two byte arrays cipherText and cText aren't equal:
If you have non-text values you will need to write a loop and use the Integer.toHexString() method to show the contents of the bytes
If the bytes can contain non-text characters the conversion to String is not symmetric. Several different byte patterns will generate the same character: a '?'
The String class has a constructor that will take a Charset with better encoding/decoding characteristics. To find which one works, fill a byte array with values from 0 to 255, convert to String and then back to an array and compare the input array with the output array to see if all the bytes make it without loss.