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

huangapple go评论69阅读模式
英文:

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

问题

我正在开发一个网络服务,我想将字节数组作为字符串发送,然后再获取回原始字节数组。

我再次解释一下,我的服务器端的角色是加密一条信息,所以我有一个字节数组。

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

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

代码:

cipherText.toString();

这样我就得到了一个字符串形式的数组,但什么都没有改变。

我如何才能恢复到原始的字节数组呢?

谢谢

英文:

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

答案1

得分: 4

以下是您要求的翻译内容:

发送字节数组的常见方法是在发送之前将其编码为Base64格式,在接收端接收字符串后,必须对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);
    //...
}
英文:

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

答案2

得分: 3

请不要使用来自 @andy jason(https://stackoverflow.com/a/63489562/8166854)的转换作为字节数组(特别是在用于加密的数据)转换为字符串,反之亦然,不能使用 new String(bytes, charset) 进行转换。

一种将字节数组 -> 字符串 -> 字节数组的方法是使用 Base64 编码:

结果:

ByteToString 和反向测试
bytes: ee99c01c47185dbd6b62dd9bcfed94d7

根据评论和yuanmingzhu
s: ��G]�kbݛ���
tab:   efbfbdefbfbd1c47185defbfbd6b62dd9befbfbdefbfbdefbfbd
字节是否等于tab:false

使用Base64的方法
s2: 7pnAHEcYXb1rYt2bz+2U1w==
tab2:   ee99c01c47185dbd6b62dd9bcfed94d7
字节是否等于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://stackoverflow.com/questions/63489517/string-of-byte-array-to-byte-array-rsa-and-java");
        System.out.println("ByteToString 和反向测试");
        byte[] bytes = new byte[16];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(bytes);
        System.out.println("bytes: " + bytesToHex(bytes));

        // 根据评论和yuanmingzhu
        System.out.println("\n根据评论和yuanmingzhu");
        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("字节是否等于tab: " + Arrays.equals(bytes, tab));

        // 使用Base64的方法
        System.out.println("\n使用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("字节是否等于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();
    }
}
英文:

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

答案3

得分: -1

如果您想可逆地将字节数组转换为字符串,您必须使用期望字节数组的String构造函数:

String s = new String(bytes, charset);

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

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

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

huangapple
  • 本文由 发表于 2020年8月19日 22:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63489517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定