• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps String to byte conversion

Hi,

I'm using the following code to encrypt a String (inText) whose output is a byte array named cipherText;

Code:
byte[] cipherText;
cipherText=cipher.doFinal(inText.getBytes());

The cipherText is printed in a textbox as follows:

Code:
encText.setText(new String(cipherText));

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:

Code:
byte[] cText;
cText=encText.getText().toString().getBytes();

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:

Code:
decText.setText(Integer.toString(cText.length));
    		encText.setText(Integer.toString(cipherText.length));
    		boolean res=Arrays.equals(cText, cipherText);
    		result.setText(Boolean.toString(res));

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?

Thanks...
 
I guess the first thing I would try is to see if the two are equal at each step.

that is, are they equal without converting to bytes?

Are they equal when unencrypted? etc

That might help you get a more accurate picture of where the conversion is losing something.
 
Back
Top Bottom