简体   繁体   English

字节数组到字节数组的字符串(rsa 和 java)

[英]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.我正在处理 web 服务,我想将字节数组作为字符串发送,然后获取原始字节数组。

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谢谢

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.发送字节数组的一种常见方法是在发送之前将其编码为 Base64,另一方面在接收字符串时必须将其解码为 Base64 以获得原始字节数组。 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);
    //...
}

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).不要使用来自@andy jason ( https://stackoverflow.com/a/63489562/8166854 ) 的转换作为字节数组(尤其是与用于加密的数据一起使用时)无法转换为字符串,反之亦然字符串(字节,字符集)。

One method for a byte array -> String -> byte array conversion is to use the Base64-encoding:字节数组 -> 字符串 -> 字节数组转换的一种方法是使用 Base64 编码:

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

If you want to convert your byte array to String reversibly, you have to use the String constructor which expects a byte array:如果要将字节数组可逆地转换为 String,则必须使用需要字节数组的 String 构造函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM