【发布时间】:2020-01-01 13:36:13
【问题描述】:
我正在使用 POCO C++ API 并使用 CRYPTO 模块进行加密和解密。我遇到了解密无法正常工作并重新调整以下错误的问题:
RSA_padding_check_PKCS1_type_2:pkcs decoding error; error:04065072:rsa
routines:rsa_ossl_private_decrypt:padding check failed"
我正在使用 RSA 密钥/对。密钥对的生成
void Support::GenerateKeyPair(std::string& PublicKey, std::string& PrivateKey)
{
Poco::Crypto::RSAKey key(Poco::Crypto::RSAKey::KL_1024, Poco::Crypto::RSAKey::EXP_SMALL);
std::ostringstream strPub;
std::ostringstream strPriv;
key.save(&strPub, &strPriv); // EmailEncryptionPassphrase);
PublicKey = strPub.str();
PrivateKey = strPriv.str();
}
这些密钥被放置在一个文件中 - 并将作为加密/解密活动的字符串显式检索 - 即不直接使用文件的加密/解密
我的加密操作如下:
std::istringstream iPub(PublicKey);
Poco::Crypto::RSAKey MulaRSAKey(&iPub);
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher* MulaCipherImpl = factory.createCipher(MulaRSAKey);
//encrypt using RSA (keys prepped with RSA)
EncryptedData = MulaCipherImpl->encryptString(Data, Poco::Crypto::Cipher::Encoding::ENC_NONE);
同样——公钥是从配置文件中检索的字符串。此操作没有失败 - 加密似乎正在工作。
我解密数据如下:
std::istringstream iPub(Keys.PublicKey);
std::istringstream iPriv(Keys.PrivateKey);
Poco::Crypto::RSAKey MulaRSAKey(&iPub, &iPriv); // EmailEncryptionPassphrase);
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher* MulaCipherImpl = factory.createCipher(MulaRSAKey);
DecryptedData = std::make_shared<std::string>(MulaCipherImpl->decryptString(EncryptData,
Poco::Crypto::Cipher::Encoding::ENC_NONE));
这是发生故障的地方。解密因我上面提到的填充错误而失败。解密操作还使用配置文件中的密钥对字符串。
我不确定密钥对是否有效,因此我使用 RSA 密钥(在线)测试仪来验证密钥对 - 并且测试能够加密和解密。所以看起来密钥是有效的。
我能够将编码从 Poco::Crypto::Cipher::Encoding::ENC_NONE 更改为 Poco::Crypto::Cipher::Encoding::ENC_BINHEX 并且解密确实通过了 - 生成了一个十六进制输出 - 但是确实通过了。令人困惑的方面是加密操作仍在使用 Poco::Crypto::Cipher::Encoding::ENC_NONE。我的理解是加密和解密应该使用相同的编码。这虽然没有说明为什么基于 NONE 的编码在解密时失败。
如果有人能澄清什么配置不正确 - 将是一个很大的帮助。
谢谢 彼得
【问题讨论】:
-
你为什么要创建一个指向 std::string 的共享指针?无论如何,它实际上是指向 char* 缓冲区的共享指针?
标签: c++ rsa poco-libraries