How to encrypt a file with a password using OpenSSL


Reference of commands to encrypt a file with a password using OpenSSL.

Update 5 Aug 2020: with recent versions of openssl the -pbkdf2 flag should be used for secure password hashing. Omit this flag if using an earlier version of openssl that doesn’t support it, or even better upgrade to a version that supports it!

Encryption

To encrypt a file:

1
openssl enc -aes256 -pbkdf2 -salt -in file.txt -out file.txt.enc

You will be prompted to enter an encryption password. If you want to specify the password in the command use the -pass parameter, for example to encrypt with the password myPassword run:

1
openssl enc -aes256 -salt -pbkdf2 -in file.txt -out file.txt.enc -pass pass:myPassword

To list the available encryption algorithms run:

1
openssl list-cipher-commands

Decryption

To decrypt a file:

1
openssl enc -aes256 -d -pbkdf2 -in file.txt.enc -out file.txt

You will be prompted to enter the decryption password.

To specify the password in the command use the -pass parameter. For example to decrypt using the password myPassword run:

1
openssl enc -aes256 -pbkdf2 -d -in file.txt.enc -out file.txt -pass pass:myPassword

References