Is it possible to encrypt a file using both AES-256 cipher, password and keyfile using OpenSSL?

I want to encrypt a file using OpenSSL.

After hours of testing I have found a command which may partially do what I want:

openssl enc -aes-256-cbc -salt -in input.txt -out output.txt

When I run the command above I am prompted for a password which is desirable in this scenario.

I am wondering if and how I can use the command above both with a password and a keyfile. I.e. a keyfile is used to encrypt the input.txt file and a password is also used. I have tested the -inkey flag already but it does not seem to work.

OS: Linux (custom)

  • -pass file:your_keyfilename -pass:yourpassword superuser.com/questions/724986/…
    – mootmoot
    Aug 13, 2018 at 9:08
  • That doesn't seem to work. openssl enc -aes-256-cbc -salt -in input.txt -out output.txt -pass file:user.key generates this error message: Error reading password from BIO Error getting password. If I add -pass:password at the end I also get an error message.
    – Vesa
    Aug 13, 2018 at 10:03
  • openssl enc -aes-256-cbc -salt -in input.txt -out output.txt -pass file:user.key -pass pass:password
    – mootmoot
    Aug 13, 2018 at 10:26
  • I made one test and it may work. Will test again as soon as possible. But is it possible to make openssl prompt for the password instead?
    – Vesa
    Aug 13, 2018 at 11:09
  • openssl enc -aes-256-cbc -salt -in qq.html -out qq.enc -pass file:user.key -pass stdin ;-)
    – mootmoot
    Aug 13, 2018 at 11:50

1 Answer 1


Whatever you do, don't try specifying multiple -pass arguments as @mootmoot suggests in the comments. Only the latest applies, as can be trivially seen by:

echo test | openssl enc -aes-256-cbc -salt -pass file:key -pass pass:password | openssl enc -d -aes-256-cbc -salt -pass pass:password

Assuming you're trying to require both the password and the keyfile to decrypt, the easiest way would be to simply chain them, though this will result in a slight size increase:

$ echo test | openssl enc -aes-256-cbc -salt | openssl enc -aes-256-cbc -salt -pass file:key >testout
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:

$ <testout openssl enc -d -aes-256-cbc -salt -pass file:key | openssl enc -d -aes-256-cbc -salt
enter aes-256-cbc decryption password:

You should be aware though that the key derivation openssl enc uses is quite terrible. 0.9.8 defaults to MD5, and while it lets you select another hash, it only allows one round of hash(password || salt). 1.1.0 changes the default hash to SHA256, but it still only allows a single round, so the improvement is very minor.

I've also seen this quote from an openssl developer referenced a few times:

At the end of the day, OpenSSL is a *library*, not an end-user product, and enc(1) and friends are developer utilities and "demo" tools.

It's from 2009, so I don't know how much that's changed since then (I believe the cms utility is ok), but I would recommend avoiding enc if possible.

  • You can specify -md sha1 or something of that sort on the older systems that use MD5 by default. It's still pretty terrible though since there's no slow KDF.
    – forest
    Aug 14, 2018 at 3:02
  • Versions back at least to 0.9.8 allow selecting the hash for password-based derivation, but the default is md5 through 1.0.2 versus sha256 in 1.1.0 (and soon 1.1.1). In either/any case it's only one iteration, which is the real problem, as you correctly say. Aug 14, 2018 at 4:04
  • @forest, dave_thompson_085 You're both correct, not sure why I missed that. Thanks for the catch. Aug 14, 2018 at 11:39
  • Version 1.1.1 has the iter option, which can be used to specify the number of sha256 iterations to use in the pbkdf2 key derivation. See openssl.org/docs/man1.1.1/man1/openssl-enc.html.
    – mti2935
    Apr 10, 2020 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.