字节数组的字符串转为字节数组 (RSA 和 Java)

6

我正在开发一个Web服务,想要将字节流作为字符串发送,并能够获取原始的字节流。

再解释一下,我的服务器端需要对消息进行加密处理,因此我有一个字节数组。

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

为了发送这个加密的消息,我将它作为字符串发送,因为我正在发送整个数据帧。

代码:

cipherText.toString();

我将数组作为字符串传入,但是没有任何变化。

如何恢复我的原始数据?

谢谢。

3个回答

4

在发送字节数组时,通常的方法是在发送之前对其进行Base64编码,在接收字符串时必须将其解码回原始的字节数组。例如:

发送方:

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

接收器:

public void getMessage(String message) {
    byte[] decodeMessage = Base64.getDecoder().decode(message);
    //...
}

3
不要使用@andy jason的转换(https://dev59.com/XLzpa4cB1Zd3GeqPSNGe#63489562)将字节数组转换为字符串,反之亦然,特别是在用于加密的数据中。
一种将字节数组-〉字符串-〉字节数组进行转换的方法是使用Base64编码:
结果:
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

代码:

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://dev59.com/XLzpa4cB1Zd3GeqPSNGe");
        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();
    }
}

-1
如果您想将字节数组可逆地转换为字符串,您必须使用期望一个字节数组的String构造函数:
String s = new String(bytes, charset);

然后要找到您的字节数组,您必须小心使用相同的字符集:

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

5
抱歉,当使用不可打印字符的字节数组数据(特别是在加密中使用)时,您的转换将无法运行-请参考我的示例,您的代码大多数情况下会失败。 - Michael Fehr
在第二行代码中需要删除空格(括号前的空格)。不能编辑,因为修改后的变化少于六个字符。 - Nate T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接