C++ OpenSSL解密-EVP_DecryptFinal_ex失败
我使用这个解密函数来获取一个密码的纯文本值,这个密码是使用EVP AES 265 GCM加密的;我可以在rawOut中看到数据,但ret=EVP\u DecryptFinal\u exctx,rawOut,&len;返回0;你能解释一下原因吗?我也看到过在EVP_decryptofinal_ex代码中使用rawOut+len的源代码,我不确定为什么需要这样做,因为它会将指针移动到缓冲区的末尾C++ OpenSSL解密-EVP_DecryptFinal_ex失败,c++,c,c++11,openssl,public-key-encryption,C++,C,C++11,Openssl,Public Key Encryption,我使用这个解密函数来获取一个密码的纯文本值,这个密码是使用EVP AES 265 GCM加密的;我可以在rawOut中看到数据,但ret=EVP\u DecryptFinal\u exctx,rawOut,&len;返回0;你能解释一下原因吗?我也看到过在EVP_decryptofinal_ex代码中使用rawOut+len的源代码,我不确定为什么需要这样做,因为它会将指针移动到缓冲区的末尾 unsigned char* keyDecrypter(unsigned char* pszMaster
unsigned char* keyDecrypter(unsigned char* pszMasterKey)
{
ERR_load_crypto_strings();
int ret, len;
EVP_CIPHER_CTX* ctx;
unsigned char* rawOut = new unsigned char[48]; // ToDo Remove Hardcoded Value
Info info = m_header.processKeyInfo();
if (NULL == info.nonce)
return NULL;
if (!(ctx = EVP_CIPHER_CTX_new()))
return NULL;
if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, pszMasterKey, info.nonce))
return NULL;
if (!EVP_DecryptUpdate(ctx, NULL, &len, m_header.aad, m_header.aad_len))
return NULL;
if (!EVP_DecryptUpdate(ctx, rawOut, &len, m_header.encryptedValue, m_header.encryptedValueLen))
return NULL;
// Finalise the decryption. A positive return value indicates success,
// anything else is a failure - the plain text is not trustworthy.
ret = EVP_DecryptFinal_ex(ctx, rawOut, &len);
ERR_print_errors_fp(stderr);
EVP_CIPHER_CTX_free(ctx);
if (ret > 0)
{
return rawOut;
}
else
{
return NULL;
}
}
您需要将rawOut+len传递给EVP_DecryptFinal_ex。请参见本章末尾的示例:
还要注意,rawOut必须有足够的空间容纳m_header.aad_len+cipher_block_大小的字节。您可以使用EVP_CIPHER_block_size获得块大小。我已经阅读了文档,正如我提到的,我不确定为什么需要EXBUF+outlen,至于CIPHER_block_大小,谢谢,我错过了我将测试它的内容。谢谢你的回答,所以我尝试了这两个建议,但仍然没有从函数中获得积极的价值。谢谢你。
/* Buffer passed to EVP_EncryptFinal() must be after data just
* encrypted to avoid overwriting it.
*/
if(!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
{
/* Error */
return 0;
}
outlen += tmplen;