Encrypt and decrypt a file with OpenSSL

I was recently looking for a way of encrypting files with a salted encryption algruthum, and found this can be done using OpenSSL. It’s really easy and much more secure that using a zip product.

You do this by using the following syntax:

openssl des3 -salt -in /path/FileToBeEncrypted -out /path/EncryptedFileName

eg

openssl enc -aes-256-cbc -md sha512 -pbkdf2 -salt -in ~/test.txt -out ~/test.txt.enc

The salt is equivlent to a password, you will need this to decrypt the file in the future.

To decrypt the file you need to add the -d switch]

eg

openssl enc -aes-256-cbc -md sha512 -pbkdf2 -d -salt -in ~/test.txt.enc -out ~/test.txt

I recently had to do use the OpenSSL decryption process in a script, and needed a way to pass the salt password without being prompted.  

My solution was to use the -pass switch and standard input.

echo <My Salt Password> | openssl enc -aes-256-cbc -md sha512 -pbkdf2 -salt -in ~/test.txt -out ~/test.txt.enc -pass stdin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.