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 ?

red.and.black
  • 33
  • 1
  • 5
  • check out these two links, it may be helpful: [string to byte and vice versa](http://stackoverflow.com/questions/1536054/how-to-convert-byte-array-to-string-and-vice-versa), [another link](http://stackoverflow.com/questions/4318693/string-to-byte-and-vice-versa) – user45 May 23 '16 at 10:38

4 Answers4

3

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 ? – red.and.black May 23 '16 at 10:48
  • @RédaBk: I'd use base64 for that, just as I've shown. – Jon Skeet May 23 '16 at 10:49
  • 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... – red.and.black May 23 '16 at 11:20
1

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

Pavel Poley
  • 5,307
  • 4
  • 35
  • 66
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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 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. – Jon Skeet May 23 '16 at 10:43
-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.

S. Kadakov
  • 861
  • 1
  • 6
  • 15
  • 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. – Jon Skeet May 23 '16 at 12:07