为什么RSA使用相同的密钥和消息产生不同的结果?

时间:2022-10-05 20:13:36

I will post my code. Sorry for the confusion.

我将发布我的代码。抱歉的混乱。

StringBuilder texto1 = new StringBuilder("LALALLA");
byte[] x = texto1.toString().getBytes();
try {
  Cipher cifrado = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  cifrado.init(Cipher.ENCRYPT_MODE, key1.getPublic());
  x = cifrado.doFinal(x);
  String texto;
  texto = new String(x, "UTF-8");
  JOptionPane.showInputDialog(publicKey.toString());
  String teste = "";
  for (int i = 0; i < x.length; i++) {
    teste += x[i];
  }
  jTextPane1.setText(teste);
  //cifrado.init(Cipher.DECRYPT_MODE, privatekey);
  byte[] y;
  // x= texto.getBytes();
  //y = cifrado.doFinal(texto.getBytes());
  //texto = new String(y,"UTF-8");
  jTextPane2.setText(x.toString());
} ...

That's the code in an action on a button. Everytime that I run this code, with the same keys, texto1 on encryption returns a different result like [B@52a0b1e1 or [B@3e55abb3

这是按钮上操作的代码。每次运行此代码时,使用相同的密钥,在加密上的texto1返回一个不同的结果,如[B@52a0b1e1或[B@3e55abb3]。

1 个解决方案

#1


6  

The toString() method of arrays in Java doesn't display the content of the array. Instead, it shows the component type and an identifier based on the location of the array in memory.

Java中数组的toString()方法不显示数组的内容。相反,它根据数组在内存中的位置显示组件类型和标识符。

If you want to see the content of the array, you have to iterate over its elements. And, in this case, you'll have to decide how you want to encode the byte elements to text. It looks like you are trying to do this with your variable teste, but I'd recommend something like this:

如果要查看数组的内容,就必须对其元素进行迭代。在这种情况下,您必须决定如何将字节元素编码为文本。看起来你试着用变量teste来做这个,但是我建议你这样做:

StringBuilder buf = new StringBuilder();
for (byte b : x) 
  buf.append(String.format("%02X", b));
String teste = buf.toString();

This will generate a hexadecimal representation of your ciphertext. You can't create a String from random 8-bit values as you attempt with the variable texto, because the bytes will generally not form valid UTF-8 encoding sequences. You'll end up with a lot of replacement characters in the text (�).

这将生成密码文本的十六进制表示。您不能在尝试使用变量texto时从随机的8位值创建字符串,因为字节通常不会形成有效的UTF-8编码序列。你会得到很多的替换字符在文本(�)。

With a hex (or base-64) encoding, you will see that the cipher text still varies randomly. That's because RSA's PKCS #1 padding schemes uses random data to pad the message before encryption. This deliberate feature of RSA prevents an attacker from recognizing when the same message is being sent.

使用十六进制(或base-64)编码,您将看到密码文本仍然是随机变化的。这是因为RSA的PKCS #1填充方案在加密前使用随机数据填充消息。RSA的这个精心设计的特性阻止了攻击者识别发送相同消息的时间。

#1


6  

The toString() method of arrays in Java doesn't display the content of the array. Instead, it shows the component type and an identifier based on the location of the array in memory.

Java中数组的toString()方法不显示数组的内容。相反,它根据数组在内存中的位置显示组件类型和标识符。

If you want to see the content of the array, you have to iterate over its elements. And, in this case, you'll have to decide how you want to encode the byte elements to text. It looks like you are trying to do this with your variable teste, but I'd recommend something like this:

如果要查看数组的内容,就必须对其元素进行迭代。在这种情况下,您必须决定如何将字节元素编码为文本。看起来你试着用变量teste来做这个,但是我建议你这样做:

StringBuilder buf = new StringBuilder();
for (byte b : x) 
  buf.append(String.format("%02X", b));
String teste = buf.toString();

This will generate a hexadecimal representation of your ciphertext. You can't create a String from random 8-bit values as you attempt with the variable texto, because the bytes will generally not form valid UTF-8 encoding sequences. You'll end up with a lot of replacement characters in the text (�).

这将生成密码文本的十六进制表示。您不能在尝试使用变量texto时从随机的8位值创建字符串,因为字节通常不会形成有效的UTF-8编码序列。你会得到很多的替换字符在文本(�)。

With a hex (or base-64) encoding, you will see that the cipher text still varies randomly. That's because RSA's PKCS #1 padding schemes uses random data to pad the message before encryption. This deliberate feature of RSA prevents an attacker from recognizing when the same message is being sent.

使用十六进制(或base-64)编码,您将看到密码文本仍然是随机变化的。这是因为RSA的PKCS #1填充方案在加密前使用随机数据填充消息。RSA的这个精心设计的特性阻止了攻击者识别发送相同消息的时间。