EVP_DecryptUpdate 是有可能需要重复调用的。网上都没有这块内容。绕了点弯路
bool bRet = true;
std::string plaintext = ciphertext;
plaintext.resize(ciphertext.length());
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx)
{
return false;
}
if (!EVP_DecryptInit_ex(ctx,
EVP_aes_128_ecb(), nullptr,
(const unsigned char*)key.data(), nullptr))
{
return false;
}
int totalOut = 0;
int totalIn = 0;
while (totalIn < ciphertext.length())
{
int out = 0;
if (!EVP_DecryptUpdate(ctx,
(unsigned char*)(plaintext.data() + totalOut), &out,
(unsigned char*)(ciphertext.data() + totalIn), AES128_BLOB_SIZE))
{
return false;
}
totalOut += out;
totalIn += AES128_BLOB_SIZE;
}
if (!EVP_DecryptFinal_ex(ctx, (unsigned char*)(plaintext.data() + totalOut), &out))
{
return false;
}
totalOut += out;
plaintext.resize(totalOut);
std::cout << totalOut << " " << plaintext << endl;