我正在尝试使用 OpenSSL 实现 RSA 加密/解密。不幸的是,我的代码在解密过程中失败了。
我正在使用 Qt。所以这是我的代码:
QByteArray CryptRSA::rsaEncrypt(QByteArray input)
{
QByteArray result(RSA_size(rsaKey), '\0');
int encryptedBytes = RSA_public_encrypt(RSA_size(rsaKey) - 42, (unsigned char *)input.data(), (unsigned char *) result.data(), rsaKey, RSA_PKCS1_OAEP_PADDING);
if (encryptedBytes== -1)
{
qDebug() << "Error encrypting RSA Key:";
handleErrors();
return QByteArray();
}
else
{
return result;
}
}
QByteArray CryptRSA::rsaDecrypt(QByteArray input)
{
QByteArray result(RSA_size(rsaKey), '\0');
int decryptedBytes = RSA_private_decrypt(RSA_size(rsaKey) - 42, (unsigned char *)input.data(), (unsigned char *)result.data(), rsaKey, RSA_PKCS1_OAEP_PADDING);
if (decryptedBytes == -1)
{
qDebug() << "Error decrypting RSA Key.";
handleErrors();
return QByteArray();
}
else
{
result.resize(decryptedBytes);
return result;
}
}
这里是错误:
error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed
它失败了:
RSA_private_decrypt(RSA_size(rsaKey) - 42, (unsigned char *)input.data(),
(unsigned char *)result.data(), rsaKey, RSA_PKCS1_OAEP_PADDING);
我已经尝试了几件事,但我找不到我的错误。
最佳答案
error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed
如果 RSA_public_encrypt
成功,则将 result
数组的大小设置为 encryptedBytes
。为 RSA_private_decrypt
做类似的事情。
此外,不清楚您要使用 RSA_size(rsaKey) - 42
做什么。我觉得这很奇怪。我希望它是 input.size()
。但我猜你知道你在用你的数组做什么。
可能还有其他问题(比如公钥和私钥不匹配),但我们需要查看更多代码和参数才能判断。
此外,您应该使用 EVP_*
接口(interface)。参见 EVP Asymmetric Encryption and Decryption of an Envelope在 OpenSSL wiki 上。
关于c++ - OpenSSL RSA_private_decrypt() 失败,返回 "oaep decoding error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506611/