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
@rew1nd How about calling the method
getBytes()
?– Victor Stafusa
@Vitor please read the question! String Crypto = [B@541fe858 byte[] Bt = Crypto.getBytes(); = [B@f 2f55b3 !
– rew1nd