【发布时间】:2017-06-21 15:43:41
【问题描述】:
我正在尝试使用公钥加密消息,并在 shell 中使用 crypto++ 使用私钥解密密码:
openssl rsautl -encrypt -inkey id_rsa.pub.pem -pubin -in message -out message.enc
和
openssl rsautl -decrypt -inkey id_rsa.pem -in message.enc -out message.dec
加密/解密在单独的应用程序中完成。我从https://www.cryptopp.com/wiki/RSA_Cryptography 的示例开始。 我的代码:
std::string publicEncrypt(std::string const& plain) {
auto cipher = std::string{};
CryptoPP::RSAES_OAEP_SHA_Encryptor e(getPublicKey());
CryptoPP::StringSource(plain, true,
new CryptoPP::PK_EncryptorFilter(CryptoPP::NullRNG(), e,
new CryptoPP::StringSink(cipher)));
return cipher;
}
std::string privateDecrypt(std::string const& cipher) {
auto decrypted = std::string{};
CryptoPP::RSAES_OAEP_SHA_Decryptor d(getPrivateKey());
CryptoPP::StringSource(cipher, true,
new CryptoPP::PK_DecryptorFilter(CryptoPP::NullRNG(), d,
new CryptoPP::StringSink(decrypted)));
return decrypted;
}
我的问题:
- 为什么 EncryptorFilter/DecryptorFilter 需要随机数生成器 (RNG)?
- RNG 必须与加密/解密相同,对吧?那么,进程之间如何共享呢?
按照https://stackoverflow.com/users/608639/jww in Unable to do RSA Encrption/Decryption using Crypto++ (isValidCoding is false) 的建议使用 NullRNG() 会导致
std::exception NullRNG: NullRNG should only be passed to functions that don't need to generate random bytes.
我想我从根本上错过了一些东西。感谢您的提示和建议。
如果我在具有全局 RNG 的单元测试中使用此代码,则一切正常。
【问题讨论】:
-
RNG 是必需的,因为OAEP padding 需要生成随机位串(如果您不使用随机值,例如 NullRNG,您将失去 RSA OAEP 的一些主要安全属性)。我不确定为什么解码需要RNG,因为随机字符串是由算法恢复的,所以不需要生成新的随机位。
-
@puzzlepalace - “我不知道为什么解码需要 RNG,因为随机字符串是由算法恢复的......” - 在私钥操作期间会出现盲点。
-
是的,就是这样,很好。
标签: c++ rsa public-key-encryption crypto++