The output length of the openssl aes EVP_DecryptUpdate method is inconsistent with the data length. What is the situation? Thank you c++ decrypt source
static int Decrypt(
std::vector<uint8_t> & en_data,
int en_data_len,
std::vector<uint8_t> & decrypt_data,
unsigned char *key,
unsigned char *iv,
const int type
)
{
std::vector<uint8_t> buffer(en_data_len + EVP_MAX_BLOCK_LENGTH);
int outl = 0, ret;
int total_len = 0;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
const EVP_CIPHER *cipher = EVP_aes_128_cbc();
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, 16, NULL);
ret = EVP_DecryptInit_ex(ctx, cipher,NULL, key, iv);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16);
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16);
EVP_DecryptUpdate(ctx, buffer.data(), &outl, en_data.data(), en_data_len);
total_len = outl;
EVP_DecryptFinal_ex(ctx, buffer.data() + outl, &outl);
total_len += outl;
buffer.resize(total_len);
EVP_CIPHER_CTX_free(ctx);
decrypt_data.swap(buffer);
return outl;
}
Debug execution to EVP_DecryptUpdate(ctx, buffer.data(), &outl, en_data.data(), en_data_len); Get outl =576(0x240) but the actual data length of buffer.data() is 588(0x24C) And after executing "EVP_DecryptUpdate()" in debug mode, manually modify outl = 0x24C, decrypting the data is ok
0x00000220: FE 7F 98 EB 63 BE B2 94 CB F0 5A F9 26 B5 7A 46 | ....c.....Z...zF
0x00000230: 47 F1 75 0F 2A EE 18 BE E5 0A E3 09 3C 6D F5 D8 | G.u..........m..
0x00000240: 41 27 33 2F 06 E4 22 00 18 FB DC 33 04 04 04 04 | A.3........3....
0x00000250: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
But it is not clear how to solve the mismatch between the data length obtained after EVP_DecryptUpdate and the actual length sample call
vector<uint8_t> key = Helper::ReadFile("d:\\test\\key_data");
vector<uint8_t> source = Helper::ReadFile("d:\\test\\send_data");
std::vector<unsigned char> decrypt_data;
int ret = Decrypt(source, source.size(), decrypt_data, key.data(), iv.data(), AES_TYPE);
Thanks Full encrypted data reference "send_data" [https://github.com/pxysea/aessample][1]
User contributions licensed under CC BY-SA 3.0