5 Comments

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.

About these ads

About Gen2ly

<3's linux

5 comments on “Encrypting/Decrypting a File Easily With a Couple Bash Scripts

  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.

    INAME=$1
    ONAME=$2

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

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

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

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: