1

I am trying to encode byte[] to String, then decode this String to byte[], my code is:

byte[] aaa = new byte[1]; aaa[0] = (byte) 153; String encoder = Base64.encodeBase64String(aaa); System.out.println("encoder <<>> " + encoder); // Decode the Base64 String. byte[] bytes = Base64.decodeBase64(encoder); String decoder = new String(bytes, "UTF08"); System.out.println("decoder <<>> " + decoder ); 

Result is:

encoder <<>> mQ== decoder <<>> ? 

The result are not the same one. Why does this happen?

4
  • 3
    Of course it is not the same. You are interpreting the byte as UTF-8, while what you want to do is to print out the value of the byte instead. Commented Apr 11, 2013 at 4:29
  • Like nhatdh said, just do: System.out.println("decoder <<>> " + bytes[0]); Commented Apr 11, 2013 at 4:31
  • I have tried, and i print the value of bytes, result is -103, code is: Commented Apr 11, 2013 at 4:43
  • 1
    -103 is what you would expect. Java's byte type is signed, so the maximum value it can hold is 127. When you cast 153 (an int) to a byte you get the bit pattern 10011001. When interpreted as a twos-complement number this is -103. Commented Apr 11, 2013 at 4:53

2 Answers 2

1

Try this:

byte[] aaa = new byte[1]; aaa[0] = (byte) 153; System.out.println("original bytes <<>> " + Arrays.toString(aaa)); // Encode the bytes to Base64 String encoder = Base64.encodeBase64String(aaa); System.out.println("encoder <<>> " + encoder); // Decode the Base64 String to bytes byte[] bytes = Base64.decodeBase64(encoder); System.out.println("decoded bytes <<>> " + Arrays.toString(bytes)); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quickly response, i try it, the print result is: original bytes <<>> [-103] encoder <<>> mQ== decoded bytes <<>> [-103] i don't know why? what can i do for this problem?
@TonyChou - That's actually correct. Java bytes are always signed quantities. The maximum byte value is +127. When you cast 153 to a byte, it comes out to 0x99. (When values overflow in Java, they just silently wrap around within the allowed range.) Finally, 0x99 is -103 in two's complement. If you need to see a positive value, you need to do something like byteValue & 0xff. That promotes byteValue to an int and then masks off any sign extension that might have taken place.
0

simple static utility methods to encode and decode the given string.

import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; ... private static byte[] key = { 0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79 }; // "ThisIsASecretKey"; public static String encrypt(String stringToEncrypt) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); final String encryptedString = Base64.encodeBase64String(cipher.doFinal(stringToEncrypt.getBytes())); return encryptedString; } public static String decrypt(String stringToDecrypt) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(stringToDecrypt))); return decryptedString; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.