From 7ed517a56140059e04413c89c5540aa2aeaae42b Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 7 Jul 2020 10:12:06 +0800 Subject: [PATCH] fix problem on aes. --- src/utils/aes_cbc_encryptor.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/aes_cbc_encryptor.cpp b/src/utils/aes_cbc_encryptor.cpp index 7de5b60..bcf627c 100644 --- a/src/utils/aes_cbc_encryptor.cpp +++ b/src/utils/aes_cbc_encryptor.cpp @@ -10,7 +10,7 @@ void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) { auto *encrypt_buffer = reinterpret_cast(malloc(c_len)); - EVP_EncryptInit_ex(e_ctx, nullptr, nullptr, nullptr, nullptr); + EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len, reinterpret_cast(data.data()), data.length()); EVP_EncryptFinal_ex(e_ctx, encrypt_buffer + c_len, &f_len); @@ -21,14 +21,14 @@ void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) { encrypted_data.append(reinterpret_cast(encrypt_buffer), len); - free(encrypt_buffer); + EVP_CIPHER_CTX_reset(e_ctx); } void Net::AESCBCEncryptor::decrypt(string &data, const string &encrypt_data) { int p_len = encrypt_data.length(), f_len = 0; auto *plain_buffer = static_cast(malloc(p_len)); - EVP_DecryptInit_ex(e_ctx, nullptr, nullptr, nullptr, nullptr); + EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); EVP_DecryptUpdate(e_ctx, plain_buffer, &p_len, reinterpret_cast(encrypt_data.data()), encrypt_data.length()); EVP_DecryptFinal_ex(e_ctx, plain_buffer + p_len, &f_len); @@ -39,7 +39,7 @@ void Net::AESCBCEncryptor::decrypt(string &data, const string &encrypt_data) { data.append(reinterpret_cast(plain_buffer), len); - free(plain_buffer); + EVP_CIPHER_CTX_reset(e_ctx); } void Net::AESCBCEncryptor::generate_random_key_data() {