我试图用公钥加密消息,并使用加密++用私钥解密密这样的外壳:RSA加密和解密期间的RandomNumberGenerator要求?
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必须相同,对吗?那么,如何在流程之间分享?
使用NullRNG()为Unable to do RSA Encrption/Decryption using Crypto++ (isValidCoding is false)建议https://stackoverflow.com/users/608639/jww导致
std::exception NullRNG: NullRNG should only be passed to functions that don't need to generate random bytes.
我想我根本想念的东西。感谢提示和建议。
如果我在一个全局RNG的单元测试中使用这段代码,一切正常。
需要RNG,因为[OAEP填充](https://en.wikipedia.org/wiki/Optimal_asymmetric_encryption_padding)要求生成随机比特串(如果不使用随机值,例如NullRNG,则会失去RSA OAEP的一些主要安全属性)。我不确定为什么解码需要RNG,尽管随机字符串被算法恢复,所以不需要生成新的随机位。 – puzzlepalace
@puzzlepalace - *“我不知道为什么解码需要RNG,但随机字符串是由算法恢复的......” - 在私钥操作期间致盲。 – jww
是的,这将是,很好的捕获。 – puzzlepalace