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“.

# crypten - a script to encrypt files using openssl


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

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

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

# cryptde - a script to decrypt files using openssl


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

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.

About Gen2ly

<3's linux

Posted on 2009-01-12, in Linux. Bookmark the permalink. 5 Comments.

  1. Never knew you could do that. That’s pretty cool. I wonder if I can teach people at work to do it.

  2. First off, you need to make a nice theme for your wordpress. It is nearly IMPOSSIBLE to find posts on your site. I had to use google to search your site. I could not find an “archives” with alist of your posts. Secondly, your front page is TOO BIG. You should incorporate the “more . . .” feature, which trims your home-page posts to make them shorter. It will fill your home-page with more, but shorter, posts, which makes SEO better, and easier to skim the home-page for interesting (relevant) posts. The user can then click the MORE … link to read more on that particular post.

    Excellent content on this site.

    Now onto business ;-)

    in bash do;
    gpg -c your_file.txt

    It asks for passphrase. This is 3-DES (default, but can be changed, read “man gpg”). IMO, gpg easier to learn than openssl, plus can do public-key encryption (i do not know if openssl is capable of that)

  3. Exactly what your blog says – helpful linux tidbits! Good one, Gen2ly – works perfectly. Here’s a variation I made to optionally pass the filename of the encrypted file as a second argument, otherwise it overwrites the original file.


    if [[ -z “$INAME” ]]; then
    echo “crypten []”
    echo ” – crypten is a script to encrypt files using des3″

    if [[ -z “$ONAME” ]]; then
    openssl des3 -salt -in “$INAME” -out “$INAME”
    openssl des3 -salt -in “$INAME” -out “$ONAME”

  4. heylookltsme

    Thanks for this! I was looking for an easy way to quickly encrypt files and this works perfectly. One thing I added was a line to remove the original file after encrypting and the encrypted file after decrypting. That’ll keep your directories nice and clean.

  5. it can’t encrypt more than 40 characters. why?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: