How to encrypt a file using openssl on Linux non-interactively?

How to encrypt a file using openssl on Linux non-interactively? The passphrase shall be provied to openssl in a programmatic way instead of requesting the user to input it, so that the command may work in a regular cron job or some batch jobs.

To encrypt file file.tgz and store it to file.tgz using aes-256-ebc encryption method with passphrase examplepass, the commands are as follows

export PASS=examplepass
openssl enc -aes-256-cbc -in file.tgz -out file.tgz.enc -pass env:PASS

Here, the passphrase is in a variable instead of being pass from the command line so that the other users can not see the passphrase during the encryption running.

To decrypt the file.tgz.enc to file.tgz, run

export PASS=examplepass
openssl enc -aes-256-cbc -d -in file.tgz.enc -out file.tgz -pass env:PASS

Leave a Reply

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