gpt4 book ai didi

c++ - openssl RSA_private_decrypt 和 RSA_private_encrypt 有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:48 34 4
gpt4 key购买 nike

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

其中 M 是我想要解密/加密的内容,e 是指数,n 是模块(在我的例子中是私有(private)的)。

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

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 文档指出“对于基于 PKCS #1 v1.5 的填充模式,flen 必须小于 RSA_size(rsa) - 11,对于 RSA_PKCS1_OAEP_PADDING 小于 RSA_size(rsa) - 41,恰好是 RSA_size( rsa) 用于 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将在模幂和RSA_private_decrypt之前向给定数据添加填充将尝试在 模幂运算后删除填充,其中模幂运算表示 RSA 运算 x<sup>c</sup> mod n其中 x是给定的数据和 c始终是私钥操作的私有(private)指数。

关于c++ - openssl RSA_private_decrypt 和 RSA_private_encrypt 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766239/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com