OpenSSL RSA_private_decrypt()失败,并显示“ oaep解码错误”。 [英] OpenSSL RSA_private_decrypt() fails with "oaep decoding error"

查看:273
本文介绍了OpenSSL RSA_private_decrypt()失败,并显示“ oaep解码错误”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenSSL实施RSA加密/解密。不幸的是,我的代码在解密期间失败。

I'm trying to implement RSA encryption/decryption using OpenSSL. Unfortunately my code fails during decryption.

我正在使用Qt。所以这是我的代码:

I'm using Qt. So here is my code:

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); 

我尝试了几件事,但是我找不到我的错误。

I have tried several things, but i can't find my mistakes.

推荐答案

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 做类似的操作。

If RSA_public_encrypt succeeds, then set the size of the result array to encryptedBytes. Do similar for RSA_private_decrypt.

此外,不清楚您要对 RSA_size(rsaKey)-42 。我觉得这很奇怪。我希望它是 input.size()。但是我猜你知道你在对数组做些什么。

Also, its not clear what you are trying to do with RSA_size(rsaKey) - 42. That looks very odd to me. I would expect it to be input.size(). But I'm guessing you know what you are doing with you array.

可能还有其他问题(例如公钥和私钥不匹配),但是我们

There could be other problems (like the public and private keys don't match), but we'd need to see more code and parameters to tell.

此外,您还应该使用 EVP _ * 接口。请参阅OpenSSL Wiki上的 EVP信封的不对称加密和解密

Also, you should use the EVP_* interfaces. See EVP Asymmetric Encryption and Decryption of an Envelope on the OpenSSL wiki.

这篇关于OpenSSL RSA_private_decrypt()失败,并显示“ oaep解码错误”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆