Encrypting & Decrypting files and directories using OpenSSL

Encrypt & Decrypt a single file
===============================
To encrypt the file img1.jpg to the encrypted file ing1.jpg.enc use the following
You will be promoted to type your encryption password, do not forget it!

openssl aes-256-cbc -a -salt -in img1.jpg -out img1.jpg.enc

To decrypt the encrypted file img1.jpg.enc back to img1.jpg type following
You will be prompted for the decryption password that you set above.

openssl aes-256-cbc -d -a -in img1.jpg.enc -out img1.jpg

Encrypt & Decrypt multiple files within one directory
=====================================================
To encrypt all files in one folder with a password that you set form the command line

for f in * ; do [ -f $f ] && openssl aes-256-cbc -a -salt -in $f -out $f.enc -k PASSWORD ; done

To encrypt all files in one folder with a password that you set in the file author.txt and then remove all the original JPG files and the author.txt file itself for obvious reasons!

for f in * ; do [ -f $f ] && openssl aes-256-cbc -a -salt -in $f -out $f.enc -pass file:author.txt ; done ; rm *.JPG ; rm *.txt

To encrypt all files in one folder with a password set in the command line and then erase the bash history and remove all tar files. We remove the bash history so that your password is not retrievable by simply pressing the up arrow key!

for f in * ; do [ -f $f ] && openssl aes-256-cbc -a -salt -in $f -out $f.enc -k PASSWORD ; done ; rm *.tar ; history -c && history -w

Encrypt & Decrypt all files recursively from parent directory
=============================================================
Encrypt all files recursively with a password set from the command line and then erase the bash history and remove all the original tar files.
This assumes that the files to be encrypted are tar files, you can of course run the command on any type of file extension.

for f in **/*.tar ; do [ -f $f ] && openssl aes-256-cbc -a -salt -in $f -out $f.enc -k PASSWORD ; done ; rm **/*.tar ; history -c && history -w
Print Friendly, PDF & Email

More Like This


Categories


Linux Security

Tags


Comments
  • Tyler Zemskow March 29, 2017 at 4:26 am

    Thanx ! This article is very useful

  • Post a comment