Byte array to string and reverse: recovered byte array is NOT the same as original byte array in Java

I converted the byte array to string as follows in Java:

String str_bytearray = new String(bytearray_original);

      

and then I reconstructed the original byte array using the string:

byte[] bytearray_recovered = str_bytearray.getBytes();

      

but I wondered when I compared bytearray_original and bytearray_recovered. the result is as follows:

[48, 89, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 7, 3, 66, 0, 4, 100, -27, 48, -31, 13, -33, 107, -90, 91, -9, 119, 121, -73, 83, -105, 51, -87, -109, -84, 99, 115, -123, 119, -117, -1, -62, 71, -32, 99, 4, -103, -115, -47, 113, -83, 8, -91, 14, -74, 113, -40, -26, 50, 111, 95, 71, -9, 112, 120, 16, 0, 113, -80, 124, -71, 53, -97, 69, -85, 38, -112, -30, -110, 115]

[48, 89, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 7, 3, 66, 0, 4, 100, -27, 48, -31, 13, -33, 107, -90, 91, -9, 119, 121, -73, 83, -105, 51, -87, -109, -84, 99, 115, -123, 119, -117, -1, -62, 71, -32, 99, 4, -103, 63, -47, 113, -83, 8, -91, 14, -74, 113, -40, -26, 50, 111, 95, 71, -9, 112, 120, 16, 0, 113, -80, 124, -71, 53, -97, 69, -85, 38, 63, -30, -110, 115]

      

as you can see, the two bytes are different from the original byte array, i.e. -115 to 63

and -112 to 63

. Is it possible to solve this problem?

Note. In fact, both the original and the recovered byte array are the public key. The first public key is converted to a string for storage in a file, and then after reading the string value of the public key, it needs to be restored to verify the signature.

Bytearray_original is generated like this:

PublicKey signPublicKey = keypair.getPublic(); 
byte [] bytearray_original = signPublicKey.getEncoded();

      

I appreciate any help.

Hello

+3


source to share


3 answers


You cannot convert an arbitrary sequence of bytes to String

and expect the reverse conversion to work. To store an arbitrary sequence of bytes, you need to use the type encoding Base64

. (This is available from several places - built into Java 8, and also available from Guava and Apache Commons.)

For example, as of Java 8,

String encoded = Base64.getEncoder().encodeToString(myByteArray);

      



reversible with

byte[] decoded = Base64.getDecoder().decode(encoded);

      

+7


source


As an alternative to Louis Wasserman's answer and as long as you have BouncyCastle in your project, you can use the utility classorg.bouncycastle.util.encoders.Hex

import org.bouncycastle.util.encoders.Hex;
import java.util.Arrays;

class EncodingTest {
    public static void main(String[] args) {
        byte[] bytearray_original = new byte[]{48, 89, 48, 19, 6, 7, 42, -122, 72, -50, 61, 2, 1, 6, 8, 42, -122, 72, -50, 61, 3, 1, 7, 3, 66, 0, 4, 100, -27, 48, -31, 13, -33, 107, -90, 91, -9, 119, 121, -73, 83, -105, 51, -87, -109, -84, 99, 115, -123, 119, -117, -1, -62, 71, -32, 99, 4, -103, -115, -47, 113, -83, 8, -91, 14, -74, 113, -40, -26, 50, 111, 95, 71, -9, 112, 120, 16, 0, 113, -80, 124, -71, 53, -97, 69, -85, 38, -112, -30, -110, 115};
        String str_bytearray = Hex.toHexString(bytearray_original);
        byte[] bytearray_recovered = Hex.decode(str_bytearray);
        System.out.println("Results are equal: " + Arrays.equals(bytearray_original, bytearray_recovered));
    }
}

      



This option requires an external library, but does not require Java 8.

+1


source


It might help to specify an encoding such as UTF-8.

Both the String constructor and getBytes allow you to do this, for example:

String str_bytearray = new String(bytearray_original, "UTF-8");
byte[] bytearray_recovered = str_bytearray.getBytes("UTF-8");

      

This will result in the same byte array.

EDIT: As RealSkeptic points out, you need to figure out what the encoding of your original byte array is and use that instead of "UTF-8" in the above code.

0


source







All Articles