Convert String encoded to byte

Asked

Viewed 1,147 times

0

I’m user encrypting AES-GCM on my server, and decrypting it on my android client. However I’m having a problem with the conversion.

We assume that:

1-Encrypted text generates the value in bytes: [B@541fe858

2- This valve is soaked in a JSONObject and sent to the customer.

3- The customer receives this amount with:

  String cryptoJSON =  JSONencryptedresponseWS.getString("crypto");

Which results in the correct value of [B@541fe858.

4- However then this String has to be converted to bytes, to pass that value to the Decrypt. The problem is that by making the methods cryptoJSON.getBytes or using the base64.decode, results in a different value than expected (e. g [B@f2f55b3).

Does anyone know how I can derive the concrete value that’s in String to byte format, make a direct conversion?

1 answer

2


1-Encrypted text generates the value in bytes: [B@541fe858

Wrong. That’s not the encrypted value. It’s just the result of calling the method toString() in an array.

For example:

byte[] bytes = {(byte) 'a', (byte) 'b', (byte) 'c'};
System.out.println(bytes);
System.out.println(bytes.toString());
byte[] bytes2 = {(byte) 'a', (byte) 'b', (byte) 'c'};
System.out.println(bytes2);

Produces as output:

[B@1540e19d
[B@1540e19d
[B@677327b6

See here in ideone.

What you should do is use the builder of String who receives a byte[] as a parameter:

byte[] bytes = {(byte) 'a', (byte) 'b', (byte) 'c'};
String s = new String(bytes);
System.out.println(s);

Here’s the way out:

abc

The rest of your question is wrong because it is based on the fact that the format starting with [B@ is correct, but it is not. This is because arrays do not overwrite the method toString() class Object, which by default produces as a result the binary name of the class, followed by a '@' and followed by the result of hashCode() hexadecimal. The binary name of byte[] is [B. The hashCode() an array is also inherited from Object and derived from the memory address of the object.

This standard behavior of toString() inherited from Object is most often a useless behavior that produces a result without any usefulness. Even objects with identical content but in different locations in memory will generate different and equally useless results in the toString() (see in the first code of this answer that the two arrays of identical contents generate different results in the toString()). That’s what’s happening to you. You should use the suitable builder of String to have the encoding of byte[] correct.

  • "1-The encrypted text generates the value in bytes: [B@541fe858 Wrong. Hence it is not the value in encrypted. " You friend did not read well. I agree that this is a toString method of a CRYPTOGRAM (ENCRYPTED TEXT). Your first example is very good and I have implemented it exactly the same way. What I need is the reverse process! Example: I get in my android client String jsonCryptograma=Jsonencryptedresponsews.getString("Crypto") Which produces the value [B@1540e19d. Now how do I convert jsonCryptogram to bytes?

  • @rew1nd How about calling the method getBytes()?

  • @Vitor please read the question! String Crypto = [B@541fe858 byte[] Bt = Crypto.getBytes(); = [B@f 2f55b3 !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.