使用openssl解密AES-GCM文件

时间:2013-01-11 14:51:37

标签: openssl aes encryption aes-gcm

我目前正在尝试使用openssl解密给定的文本。我尝试使用那里给出的示例制作我自己的代码: Late authentication in OpenSSL GCM decryption 但我最终还是有不好的结果。我的解密功能如下:

void aes_decrypt(EVP_CIPHER_CTX ctx, unsigned char *pCipherText,
    int pCipherTextLen, int AADLen, unsigned char* pKey, unsigned char* pIv,
    unsigned char* pMac, int MacLen) {
int bytesProcessed = 12;
int dec_success;

}
unsigned char * pOut = malloc(pCipherTextLen);
unsigned char * pAAD = malloc(AADLen);
unsigned char * pClearText = malloc(pCipherTextLen);

// setting cipher, key and iv
EVP_DecryptInit(&ctx, EVP_aes_256_gcm(), pKey, pIv);
// setting tag
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, 24, NULL);
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, 16, pMac);
// adding Additional Authenticated Data (AAD)
EVP_DecryptUpdate(&ctx, NULL, &bytesProcessed, pAAD, AADLen);
// decrypting data
EVP_DecryptUpdate(&ctx, pClearText, &bytesProcessed, pCipherText,
        pCipherTextLen);
// authentication step
dec_success = EVP_DecryptFinal(&ctx, pOut, &bytesProcessed);
free(pOut);
free(pMac);
free(pAAD);
free(pClearText);
}

以前通过阅读文本文件给出了除AAD以外的所有数据(我有加密数据列表,使用的密钥/ Iv,MAC和解密后预期的结果) 经过几次实验后,出现以下问题: - 结果与预期的结果不同 - 修改MAC不会影响结果(明文) - 抑制AAD不会影响结果。

我真的不知道为什么它不起作用。 如果你有任何想法,提示或具体的例子,那将是一个很大的帮助

祝你好运

1 个答案:

答案 0 :(得分:1)

问题解决了。该计划提供的AAD是错误的