Encrypting/Decrypting a File Easily With a Couple Bash Scripts

Once in a while a person might like to encrypt a file for security purposes. In Linux it is real easy to create good encryption using openssl with the Triple-DES Cipher.:

openssl des3 -salt -in unencrypted-data.file \
-out encrypted-data.file.des3

After entering this command, openssl will ask for you to enter the password twice. And decryption is likewise:

openssl des3 -d -salt -in encrypted-data.file.des3 \
-out unencrypted-data.file
Warning: Make sure you don’t accidentaly reverse the file names in the decryption process or you’ll lose all your data!

Remembering this command though is the tricky bit so I decided to create a couple bash scripts that made the process thoughtless. I named the bash scripts “crypten” and “cryptde“.

#!/bin/bash
# crypten - a script to encrypt files using openssl

FNAME=$1

if [[ -z "$FNAME" ]]; then
    echo "crypten <name of file>"
    echo "  - crypten is a script to encrypt files using des3"
    exit;
fi

openssl des3 -salt -in "$FNAME" -out "$FNAME.des3"

The filename ends with .des3 to be easy to recognize.

#!/bin/bash
# cryptde - a script to decrypt files using openssl

FNAME=$1

if [[ -z "$FNAME" ]]; then
    echo "cryptde <name of file>"
    echo "  - cryptde is a script to decrypt des3 encrypted files"
    exit;
fi

openssl des3 -d -salt -in "$FNAME" -out "${FNAME%.[^.]*}"

I like to put my bash scripts in a ~/.bin folder. Don’t forget to make both files executable:

chmod +x crypten cryptde

This shouldhelp make encrypting/decrypting files easier.