为什么 RSA 加密文本给我相同文本的不同结果 [英] Why does RSA encrypted text give me different results for the same text

查看:23
本文介绍了为什么 RSA 加密文本给我相同文本的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RSA 加密使用 openSSL 加密数据,效果很好.我对 RSA 的理解是,用相同的公钥加密相同的数据总是会给你相同的结果(此处所述此处).

I am encrypting data with openSSL using RSA encryption, which works fine. My understanding of RSA is, that encrypting the same data with the same public key will always give you the same result (as stated here or here).

但是,使用 openssl 每次我重复加密时都会得到不同的结果.例如:

However, using openssl I get different results every time I repeat the encryption. For example:

➜  ~  echo '30' | openssl rsautl -encrypt -inkey pub.pem -pubin  | shasum
      11b6e058273df1ebe0be5e0596e07a6c51724ca0  -

➜  ~  echo '30' | openssl rsautl -encrypt -inkey pub.pem -pubin  | shasum
      05cb82595f7429ef196189f4e781088597d90eee  -

那么为什么输出不是唯一的呢?是因为我弄错了 RSA 加密还是因为 openssl 做了一些额外的魔法?

So why is the output not unique? Is it because I got the RSA encryption wrong or because openssl does some additional magic?

实际上,我正在尝试设计一个仅存储 RSA 加密数据的数据库.我想对加密信息的哈希和进行搜索,如果加密过程本身不是唯一的,这是不可能的.

Actually I am trying to design a database which stores only RSA encrypted data. I would like to do searches on the hashsums of the encrypted information, which is impossible if the encryption procedure by itself is not unique.

推荐答案

使用适当的填充方案实现安全的 RSA 加密,其中包括一些随机性.请参阅 PKCS#1OAEP 了解更多详情.

A secure RSA encryption is implemented with an appropriate padding scheme, which includes some randomness. See PKCS#1 or OAEP for more details.

RSA 加密加密用 '0' 和一串随机位填充的消息.在此过程中,随机字符串通过密码散列和异或运算隐藏"在密文中.在解密时,RSA 解密从密文中恢复随机字符串并使用它来恢复消息.这就是为什么您使用 openssl rsautl 对同一条短信获得不同结果的原因.

The RSA encryption encrypts message padded with '0's and and a string of random bit. In the process, the random string is "hidden" in the ciphertext by cryptographic hashing and XORing. On decryption, the RSA decryption recovers the random string from the ciphertext and use it to recover message. This is why you get different result with openssl rsautl for the same text message.

这篇关于为什么 RSA 加密文本给我相同文本的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆