hunk tomas
hunk tomas

Reputation: 61

String of byte array to byte array (rsa and java)

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

Upvotes: 6

Views: 161

Answers (3)

Michael Fehr
Michael Fehr

Reputation: 6424

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();
    }
}

Upvotes: 3

JUAN CALVOPINA M
JUAN CALVOPINA M

Reputation: 3984

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);
    //...
}

Upvotes: 4

andy jason
andy jason

Reputation: 45

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);

Upvotes: -1

Related Questions