OpenSSL错误:06065064:数字包络例程:EVP_DecryptFinal_ex:解密错误

时间:2017-01-06 18:17:41

标签: c++ encryption openssl

所以我有这段代码:

BlueSOD::Encryption::DecryptionData BlueSOD::Encryption::EncryptionFactory::OpenEnvelopeDecrypt(DecryptionWork && work)
{
    EVP_PKEY* privateKey = work.privateKey.get();
    auto eKey = (unsigned char*)work.aes_info.key.c_str();
    auto eIV = (unsigned char*)work.aes_info.iv.c_str();
    EVP_CIPHER_CTX_ptr cipherCtxPtr{ AcquireCipherCtx() };
    EVP_CIPHER_CTX* cipher = cipherCtxPtr.get();
    int status;

    status = EVP_OpenInit(cipher, m_Cipher, eKey, work.aes_info.key.size(), eIV, privateKey);
    CheckForError(status);

    auto decrypted = make_unique<unsigned char[]>(work.cipherText.size() + EVP_MAX_BLOCK_LENGTH);
    auto cipherTemp = (unsigned char*)work.cipherText.c_str();
    int amtDecrypted = 0;
    int bufferLength = 0;

    status = EVP_OpenUpdate(cipher, decrypted.get(), &amtDecrypted, cipherTemp, work.cipherText.size());
    CheckForError(status);
    bufferLength += amtDecrypted;

    //error occurs in both versions of the following code
    //status = EVP_OpenFinal(cipher, decrypted.get() + amtDecrypted, &amtDecrypted);
    status = EVP_OpenFinal(cipher, decrypted.get(), &amtDecrypted);
    CheckForError(status);
    bufferLength += amtDecrypted;

    DecryptionData data;
    data.plainText = CreateSecureString(decrypted.get(), bufferLength);
    return move(data);
}

当我在调试器decrypted中逐步执行它时,在调用之前保持正确的纯文本,但EVP_OpenFinal返回标题中的错误。上面的代码就是我最初的代码(并返回相同的错误)。

EVP_OpenFinal在调用前decrypted保存正确的纯文本时,~/.invoke返回错误的原因是什么?

0 个答案:

没有答案