Encrypted file on Linux using GPG

This page gives a real quick guided to using gpg on Linux to encrypt/decrypt a file such as a "password file" containing important passwords.
For full details of gpg see:  http://www.gnupg.org/(en)/documentation/howtos.html

The first thing you need to do is create a keyring if you have not done so.
Use the command:  gpg --key-gen
You can answer the questions with the defaults.

This will populate the directory  .gnupg  with "keyring" data.
Copy this to a USB drive and store in a safe place. With the keyring files and your passphrase you can decrypt data files. If you lose the keyring, or forget your passphrase you will not able to decrypt files.

You can create multiple IDs when generating keys. For this example assume that you supplied the name Fred Smith to the question asked when creating a key. You can now use an ID of "Fred" when encrypting and decrypting files.

You can encrypt a file like so:
gpg -e -a -r Fred < data > data.enc

Decrypt the file like so:
gpg -d < data.enc > data

Now assume your password file looks something like this:

!G gpg -e -a -r Fred
DB system user: dbadmin password: db123xy
lab system user: labusr      password: lab123ab
bank ID: fredsmith  pasword: hellofred

The first line of the file is blank and the second line contains:  !G gpg -e -a -r Fred
This second line is the vi sequence used to encrypt the file.
So put the cursor on the first line of the file, then just cut/paste the second line and your file will be encrypted. Write the file out and you are done.

When you want to decrypt the file you can just open it with the editor.
Type the command:  !G gpg -d

The file will now be decrypted. You can search for the desired password. Typically you will not be modifying the file so you can simple quite the editor. If you need to encrypt it again just execute the command on the second line of the file and save it.