admin管理员组

文章数量:1124165

I can not use the output of EVP_DecodeBlock as iv and the ciphertext for decryption use EVP_CIPHER_CTX.

When I initialise the iv or ciphertext by manually writing (with actual values and not 00's):

unsigned char iv[16] = {0x00, 0x00 ...};
unsigned char cipher[16] = {...};

and use them as

EVP_DecryptInit(ctx, EVP_aes_256_cbc(), key, iv));
EVP_DecryptUpdate(ctx, out, &len, cipher, 16); //out is declared as unsigned char out[2048]

I am able to get the required result using evp_decryptfinal. However, when I have the iv and cipher generated using a variable string as:

auto iv_stdstring = ((ciphers[i]["data"][j].toString()).split(".")[1])    //ciphers is a QJsonArray
                              .split("|")[0]
                              .toStdString();
      auto iv_cstring = iv_stdstring.c_str();
      unsigned char iv[16];
      EVP_DecodeBlock(
          iv, reinterpret_cast<unsigned char *>(const_cast<char *>(iv_cstring)),
          strlen(iv_cstring));

The EVP_DecryptFinal call errors out. Weirdly enough, the same code actually works in a different .cpp file when I compile it manually using g++, but doesn't seem to work in qt; The only difference in that .cpp file is that instead of using the auto keyword I just generated the cstring like this:

const char *iv_cstring = "(24 char long iv)";

but even this does not work in the qt code. I also printed the hex characters in the arrays and they match with what's needed. I am very lost on how the same code is working in one instance and not the other.

本文标签: cCan39t use B64 decoded output as IVciphertext in aes256cbc decryptionStack Overflow