6

I am working on a web service, and I want to send a byte array as a String, then get the original byte array.

I explain again, my server side has the role of encrypting a message, so I have a byte array.

 Cipher cipher = Cipher.getInstance("RSA");
 cipher.init(cipher.ENCRYPT_MODE,clefPrivee);
 byte[] cipherText= cipher.doFinal(msgEnOctets);

then to send this encrypted message, I send it as a String because I am sending an entire data frame

code :

cipherText.toString();

So I have the array as a string but nothing has changed.

How can I get my original painting back?

thanks

3 Answers 3

4

A common way to send byte array is to encode it in Base64 before sending it, on the other side when receiving the string it must be decoded the Base64 to get the original byte array. For example:

Sender:

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(cipher.ENCRYPT_MODE,clefPrivee);
byte[] cipherText= cipher.doFinal(msgEnOctets);
return Base64.getEncoder().encodeToString(cipherText);

Receiver:

public void getMessage(String message) {
    byte[] decodeMessage = Base64.getDecoder().decode(message);
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

3

Please do NOT use the conversion from @andy jason (https://stackoverflow.com/a/63489562/8166854) as a byte array (especially when used with data used for encryption) cannot get converted to a string and vice verse with new String(bytes, charset).

One method for a byte array -> String -> byte array conversion is to use the Base64-encoding:

result:

ByteToString and reverse test
bytes: ee99c01c47185dbd6b62dd9bcfed94d7

method as by comment andy jason
s: ��G]�kbݛ���
tab:   efbfbdefbfbd1c47185defbfbd6b62dd9befbfbdefbfbdefbfbd
bytes equal to tab: false

method with base64
s2: 7pnAHEcYXb1rYt2bz+2U1w==
tab2:   ee99c01c47185dbd6b62dd9bcfed94d7
bytes equal to tab2: true

code:

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

public class ByteToString {
    public static void main(String[] args) {
        System.out.println("https://stackoverflow.com/questions/63489517/string-of-byte-array-to-byte-array-rsa-and-java");
        System.out.println("ByteToString and reverse test");
        byte[] bytes = new byte[16];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(bytes);
        System.out.println("bytes: " + bytesToHex(bytes));

        // method by andy jason
        System.out.println("\nmethod as by comment andy jason");
        Charset charset = StandardCharsets.UTF_8;
        String s = new String(bytes, charset);
        System.out.println("s: " + s);
        byte [] tab = s.getBytes (charset);
        System.out.println("tab:   " + bytesToHex(tab));
        System.out.println("bytes equal to tab: " + Arrays.equals(bytes, tab));

        // method with base64
        System.out.println("\nmethod with base64");
        String s2 = Base64.getEncoder().encodeToString(bytes);
        System.out.println("s2: " + s2);
        byte[] tab2 = Base64.getDecoder().decode(s2);
        System.out.println("tab2:   " + bytesToHex(tab2));
        System.out.println("bytes equal to tab2: " + Arrays.equals(bytes, tab2));

    }
    private static String bytesToHex(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        return result.toString();
    }
}

Comments

-1

If you want to convert your byte array to String reversibly, you have to use the String constructor which expects a byte array:

String s = new String(bytes, charset);

Then to find your byte array, you have to be careful to use the same charset:

byte [] tab = s.getBytes (charset);

2 Comments

I'm sorry but your conversion will not work when using data in byte arrays that are not printable characters (especially when in use with encryption) - see my example with your code will fail most times.
Needs the space removed in the second line of code (before parentheses). Cannot edit as the resulting change would be less than six characters.

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.