jump to navigation

Linux Encrypt Files/Decrypt Files – GPG Interactive/Non Interactive Modes November 6, 2010

Posted by Tournas Dimitrios in Linux.
trackback

GNU Privacy Guard (GnuPG or GPG) is a free software alternative to the PGP suite of cryptographic software. This is quick guide, howto Encrypt and Decrypt files on Linux with password using GPG.
This guide deals with both the interactive mode and the non interactive mode. Interactive mode is useful when the purpose is encrypt some files on the command line. Non interactive mode is useful when the purpose is encrypt files using scripts. This guide also deals with single file encryption/decryption and multiple files encryption/decryption simultaneously.

Encrypt File on Linux using GPG :

  • Interactive Mode
gpg -c filename.txt 
## OR ## 
gpg --symmetric filename.txt

Enter passphrase: [Enter your passphrase here]
Repeat passphrase: [Repeat your passphrase here]
// Now a filename.txt.gpg is created in the same dir
  • Non interactive mode (script-mode)
gpg --yes --passphrase=[your passphrase] -c filename.txt

Decrypt File on Linux using GPG

  • Interactive Mode
gpg filename.txt.gpg 

gpg: CAST5 encrypted data
Enter passphrase: [Enter your passphrase here]
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected
  • Non Interactive Mode (script-mode)

gpg –yes –passphrase=[your passphrase] filename.txt.gpg

Quick Example Howto Use GPG on Command Line (Bash) Scripts

Following example is really simple backup from just created directory and files. Then script encrypts tar.gz package and remove original tar.gz file. After encryption file is safe to copy example to another server via FTP or so.

#!/bin/bash

BACKUP_DIR=/tmp/example-backup-dir
OUTPUT_FILE=/tmp/example-backup-dir.tar.gz
PASSPHRASE=my_secret_password

if [ -d $BACKUP_DIR ]; then
        rm -r $BACKUP_DIR
fi

mkdir $BACKUP_DIR

for i in {1..5}
do
   echo "Testing $i" > $BACKUP_DIR/file-$i.txt
done

tar -pczf $OUTPUT_FILE $BACKUP_DIR

gpg --yes --passphrase=$PASSPHRASE -c $OUTPUT_FILE

rm $OUTPUT_FILE

Comments»

No comments yet — be the first.

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