I downloaded the openssl-1.0.2l.tar.gz
source package from https://www.openssl.org/source/ and made a fresh x64 build for Windows. I use the openssl
application to encrypt a file using the following command:
openssl enc -aes-128-cbc -a -salt -in data.txt -kfile key.txt -out encrypted.txt -p
Now, I would like to consume the encrypted file in a .NET application (written in C#). I read the encrypted file (which is encoded using Base64
, because of the -a
switch), decode it, and extract the first 16 bytes in order to get the salt that was generated by OpenSSL... this works fine so far; the salt is prefixed with Salted__
, the following 8 bytes are the actual salt value.
What I have learned so far is that OpenSSL reads the first line of the given key file and uses that string for the passphrase. The actual key and initialization vector gets derived from the passphase, the salt and some hashing, which is not officially documented.
The -p
switch gave me the key and initialization vector that is used for the encryption, but I would like to know, how I can reproduce that data from the known passphrase and the salt... Everything I have tried gives me key and vector data that is different from what the openssl application gave me.
Of course, I already found similar questions (and answers) at stackoverflow and crypto.stackexchange, but none of the solutions seem to work, or are related to aes-256-cbc
... not sure, if that makes a difference?
What needs to be done to properly derive the key and initialization vector?