3

I want to convert the result of a TEA encryption (a byte[]) to a String and then convert it again to a byte[] and retrieve the same byte[].

//Encryption in the sending side
String stringToEncrypt = "blablabla"
byte[] encryptedDataSent = tea.encrypt(stringToEncrypt.getBytes());
String dataToSend = new BigInteger(encryptedDataSent).toString());

//Decryption side in the reception side
byte[] encryptedDataReceived = new BigInteger(dataToSend).toByteArray();

However, when I try this :

System.out.println(new String(encryptedDataSent));

System.out.println(new String(encryptedDataReceived));

boolean equality = Arrays.equals(encryptedDataReceived,encryptedDataSent);
System.out.println("Are two byte arrays equal ? : " + equality);

The output is :

&h�7�"�PAtj݄�I��Z`H-jK�����f

&h�7�"�PAtj݄�I��Z`H-jK�����f

Are two byte arrays equal ? : false

So, it looks like the two byte[] are the same when we print it but they are not exactly the same as we see "false" and this is a problem for the decryption that I perform after that.

I also tried to send a String with new String(byte[]) but it has the same problem when we want to convert it back to a byte[]

I want to have exactly the same byte[] in the beginning and after the conversion byte[]->String->byte[]

Do you have a solution or understand what I do wrong in my conversion ?

1

4 Answers 4

4

Don't try to convert from the byte[] to String as if it were regular encoded text data - it isn't. It's an arbitrary byte array.

The simplest approaches are to convert it to base64 or hex - that will result in ASCII text which can be reversibly decoded back to the same binary data. For example, using a public domain base64 encoder:

String dataToSend = Base64.encodeBytes(encryptedDataSent);
...
byte[] encryptedDataReceived = Base64.decode(receivedText);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. But if my need is to write the byte[] data into a text file and then to be able to copy the text, paste it in a field and retrieve the data in what format should I store the byte[] data in the text file ?
@RédaBk: I'd use base64 for that, just as I've shown.
Thank you @JonSkeet it worked ! The Base64 encoding gave me a String like this (AAAAKSvMGMmVa75daqF+XuFyDdnbDrhljLsv8o+fl7ZOfE5rdEOVv5EIxrQBGy2FgQVVwQ==) and then I was able to convert this to the original byte[] again ! Why does this String encoding works for proper byte conversion and not others ? Wondering...
1

Try to use in the decryption byte[] encode = Base64.encode(bytesToStore, Base64.DEFAULT)

Comments

0

You can't. String is not a container for binary data. It is a container for UTF-16 characters. The round trip between chars and bytes is nowhere guaranteed.

1 Comment

You can't do it like that, but the idea that you can't represent arbitrary binary data as text and then convert it back to the same binary data is clearly flawed... hex, base64, base32... any number of ways exist.
-2

try to specify charset explicitly. UTF-8 is ok for major cases:

public static void main(String[] args) {
    String in = "幸福";
    try {
        byte[] bytes = in.getBytes("utf-8");
        String out = new String(bytes, "utf-8");
        System.out.println(in + " -> " + out);
        System.out.println("equals: " + out.equals(in));
    } catch (UnsupportedEncodingException unsupportedEncodingException) {
        // do something
    }
}

Please note that you will get exactly the same result while you byte array remains unchanged.

1 Comment

No, UTF-8 is not okay when the data is arbitrary binary data, rather than UTF-8-encoded text. The OP wants to convert the result of encryption as text. That is not UTF-8-encoded text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.