openssl RSA_private_decrypt 和 RSA_private_encrypt 有什么区别?

What is the difference between openssl RSA_private_decrypt and RSA_private_encrypt?

我不是数学家(请原谅我在这个post!)中说的话,但据我所知RSA加密和解密应该是相同的操作: M ^ e mod n

其中 M 是我想要的 decrypt/encrypt,e 是指数,n 是 module(在我的例子中是私有的)。

对此的确认如下:当我想"sign"明文时,如果我进行以下任何操作,我将获得相同的签名:

RSA_private_encrypt(plain_text.size(), &plain_text[0], &encrypted[0], rsa.get(), RSA_NO_PADDING);

RSA_private_decrypt(plain_text.size(), &plain_text[0], &decrypted[0], rsa.get(), RSA_NO_PADDING);

我的问题是:RSA_private_encrypt 文档指出 "flen must be less than RSA_size(rsa) - 11 for the PKCS #1 v1.5 based padding modes, less than RSA_size(rsa) - 41 for RSA_PKCS1_OAEP_PADDING and exactly RSA_size(rsa) for RSA_NO_PADDING"。如果操作相同,为什么我只在加密情况下有此限制? 事实上,如果我尝试使用短于 RSA_SIZE(在我的例子中是 1024)的纯文本,解密操作会成功,而加密会失败。

请注意我没有使用填充(而且我知道这是不安全的)。

RSA_private_encrypt says:

RSA_private_encrypt() signs the flen bytes at from (usually a message digest with an algorithm identifier) using the private key rsa and stores the signature in to. to must point to RSA_size(rsa) bytes of memory.

同时 RSA_private_decrypt says:

RSA_private_decrypt() decrypts the flen bytes at from using the private key rsa and stores the plaintext in to. to must point to a memory section large enough to hold the decrypted data (which is smaller than RSA_size(rsa)). padding is the padding mode that was used to encrypt the data.

您引用的段落适用于文档中的 RSA_public_encrypt


如果不使用填充,则RSA_private_encryptRSA_private_decrypt将产生相同的操作。如果指定填充,则 RSA_private_encrypt 会在给定数据 之前 mod 平方求幂添加填充,而 RSA_private_decrypt 会尝试删除填充 after modular exponentiation,其中modular exponentiation表示RSA运算x<sup>c</sup> mod n 其中 x 是给定的数据,c 始终是私钥操作的私钥指数。