Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.getBytes("UTF-32") returns different results on JVM and Dalvik VM

I have a 48 character AES-192 encryption key which I'm using to decrypt an encrypted database.

However, it tells me the key length is invalid, so I logged the results of getBytes().

When I execute:

final String string = "346a23652a46392b4d73257c67317e352e3372482177652c";
final byte[] utf32Bytes = string.getBytes("UTF-32");
System.out.println(utf32Bytes.length);

Using BlueJ on my mac (Java Virtual Machine), I get 192 as the output.

However, when I use:

Log.d(C.TAG, "Key Length: " + String.valueOf("346a23652a46392b4d73257c67317e352e3372482177652c".getBytes("UTF-32").length));

I get 196 as the output.

Does anybody know why this is happening, and where Dalvik is getting an additional 4 bytes from?

like image 296
Raghav Sood Avatar asked Dec 17 '25 07:12

Raghav Sood


1 Answers

You should specify endianess on both machines

final byte[] utf32Bytes = string.getBytes("UTF-32BE");

Note that "UTF-32BE" is a different encoding, not special .getBytes parameter. It has fixed endianess and doesn't need BOM. More info: http://www.unicode.org/faq/utf_bom.html#gen6

like image 170
Esailija Avatar answered Dec 19 '25 23:12

Esailija



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!