OpenSSL RSA_private_decrypt()因“oaep decode error”失败

时间:2021-06-11 18:26:39

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

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

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

我正在使用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;
    }
}

Here is the error:

这是错误:

error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed

It fails in:

它失败了:

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.

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

1 个解决方案

#1


1  

error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed

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

如果RSA_public_encrypt成功,则将结果数组的大小设置为encryptedBytes。做类似的RSA_private_decrypt。

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.

此外,还不清楚你要用RSA_size(rsaKey)做什么--42。这看起来很奇怪。我希望它是input.size()。但是我猜你知道你在做什么。

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.

可能存在其他问题(例如公钥和私钥不匹配),但我们需要查看更多代码和参数。

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

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

#1


1  

error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed

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

如果RSA_public_encrypt成功,则将结果数组的大小设置为encryptedBytes。做类似的RSA_private_decrypt。

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.

此外,还不清楚你要用RSA_size(rsaKey)做什么--42。这看起来很奇怪。我希望它是input.size()。但是我猜你知道你在做什么。

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.

可能存在其他问题(例如公钥和私钥不匹配),但我们需要查看更多代码和参数。

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

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