我試圖用公鑰加密消息,並使用加密++用私鑰解密密這樣的外殼: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