1

I have read a link, and I can encode/decode data by C API correctly.

However, I have a file that is generated by openssl command line:

openssl aes-256-cbc -in plain.txt -out encrypted.txt
> enter password from stdin

Want to decode the output by using C API. The code is below:

unsigned char key[] = "password";
unsigned char iv[] = "";  // I don't know what is the default iv.
EVP_CipherInit(&ctx, EVP_aes_256_cbc(), key, iv, 0);

The output is wrong. Is there anyone know how to fix it? Maybe I called the wrong function? eg: EVP_PBE_CipherInit? I'm totally lost in the document and source code of openssl.

Any advice is welcome. Thank you in advance.

Community
  • 1
  • 1
ShenLei
  • 567
  • 1
  • 5
  • 17

1 Answers1

0

The openssl manpage says the key and iv values are generated from the password if not supplied. Also it gives a -P option to print the salt, key and iv value. (This is the key that needs to be passed to the function, not the password).

The salt value can be obtained from the file header. Then the key and iv has to regenerated using the same.

This stackexchange page gives a very detailed answer.

daisy
  • 22,498
  • 29
  • 129
  • 265
Sreekumar R
  • 194
  • 1
  • 10