GNU Privacy Guard: Difference between revisions

From Leo's Notes
This page was last edited on 23 November 2015, at 08:19.
generating a new key
usage information
Line 1: Line 1:
GNU Privacy Guard (GPG or GnuPG) is a open source replacement for the original Pretty Good Privacy software suite. It is capable of encrypt and decrypt data.
GNU Privacy Guard (GPG or GnuPG) is a open source replacement for the original Pretty Good Privacy software suite. It is capable of encrypt and decrypt data.


== Creating a New Key ==
== Key Management ==
 
=== Creating a New Key ===
To generate a new key, run {{code| gpg --gen-key}}
To generate a new key, run {{code| gpg --gen-key}}
{{highlight|lang=text|code=
{{highlight|lang=text|code=
Line 16: Line 18:
}}
}}


Your default key can be set by setting the {{code|GPGKEY}} environment variable.
=== Listing Keys ===
{{code|gpg --list-keys}}
{{highlight|lang=bash|code=
# gpg --list-keys
/home/leo/.gnupg/pubring.gpg
----------------------------
pub  2048R/28AF4724 2015-11-23
uid                  Leo Leung <leo@steamr.com>
}}
=== Deleting Keys ===
Delete the private key using {{code|gpg --delete-secret-keys}}, and the public key using {{code|gpg --delete-keys}}.
{{highlight|lang=bash|code=
# gpg --delete-secret-keys 28AF4724
sec  2048R/28AF4724 2015-11-23 Leo Leung <leo@steamr.com>
Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y
# gpg --delete-keys 28AF4724
pub  2048R/28AF4724 2015-11-23 Leo Leung <leo@steamr.com>
Delete this key from the keyring? (y/N) y
}}
=== Adding another UID ===
Edit the key using {{code|gpg --edit-key}} and run then {{code|adduid}} command. Exit using the {{code|save}} command.


=== Sending a key to a key server ===
{{code|gpg --keyserver pgp.mit.edu --send-key ###}}


== Changing Private Key Passphrase ==
=== Changing Private Key Passphrase ===
{{highlight|lang=text|code=
{{highlight|lang=text|code=
$ gpg --edit-key [keyID]
$ gpg --edit-key [keyID]
Line 29: Line 64:
}}
}}


== Exporting Public Key ==
=== Exporting Public Key ===
  gpg --armor --export leo@steamr.com --output leo.steamr.pub
  gpg --armor --export leo@steamr.com --output leo.steamr.pub
== Basic Usage ==
=== Signing Files ===
There are a few types of signatures:
==== Binary Signature ====
The input file is compressed and its binary data is signed. GPG places the signature and compressed file contents into a {{code|.gpg}} file.
{| class="wikitable"
! Signing
| {{code|gpg --clearsign test.txt}}
|-
! Verifying
| {{code|gpg --verify test.txt.gpg}}
|-
! Retrieving
| {{code|gpg -d -o test.txt test.txt.gpg}}
|}
==== Clear Signature ====
The contents of the input file is signed in clear text. The signature and the contents of the input file are placed in clear text in a {{code|.asc}} file. This file can be viewed with any text editor and the signature and contents are fully visible.
{| class="wikitable"
! Signing
| {{code|gpg --clearsign test.txt}}
|-
! Verifying
| {{code|gpg --verify test.txt.asc}}
|-
! Retrieving
| {{code|gpg -d -o test.txt test.txt.asc}}
|}
==== Detached Signature ====
A signature for the input file is created and stored in a {{code|.sig}} file. Typically, the signature file is bundled with the input file with the same name except with the {{code|.sig}} file extension.
{| class="wikitable"
! Signing
| {{code|gpg --detach-sign test.txt}}
|-
! Verifying
| {{code|gpg --verify test.txt.sig test.txt}}
|}


== Automating Encrypted Backups using Asymmetric Keys ==
== Automating Encrypted Backups using Asymmetric Keys ==

Revision as of 08:19, 23 November 2015

GNU Privacy Guard (GPG or GnuPG) is a open source replacement for the original Pretty Good Privacy software suite. It is capable of encrypt and decrypt data.

Key Management

Creating a New Key

To generate a new key, run gpg --gen-key

# gpg --gen-key
gpg (GnuPG) 1.4.19; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)

Your default key can be set by setting the GPGKEY environment variable.

Listing Keys

gpg --list-keys

# gpg --list-keys
/home/leo/.gnupg/pubring.gpg
----------------------------
pub   2048R/28AF4724 2015-11-23
uid                  Leo Leung <leo@steamr.com>

Deleting Keys

Delete the private key using gpg --delete-secret-keys, and the public key using gpg --delete-keys.

# gpg --delete-secret-keys 28AF4724
sec  2048R/28AF4724 2015-11-23 Leo Leung <leo@steamr.com>

Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y

# gpg --delete-keys 28AF4724
pub  2048R/28AF4724 2015-11-23 Leo Leung <leo@steamr.com>

Delete this key from the keyring? (y/N) y

Adding another UID

Edit the key using gpg --edit-key and run then adduid command. Exit using the save command.

Sending a key to a key server

gpg --keyserver pgp.mit.edu --send-key ###

Changing Private Key Passphrase

$ gpg --edit-key [keyID]
Command> passwd
Enter passphrase: 
Enter the new passphrase for this secret key.
Enter passphrase:
Repeat passphrase:
Command> save

Exporting Public Key

gpg --armor --export leo@steamr.com --output leo.steamr.pub


Basic Usage

Signing Files

There are a few types of signatures:

Binary Signature

The input file is compressed and its binary data is signed. GPG places the signature and compressed file contents into a .gpg file.

Signing gpg --clearsign test.txt
Verifying gpg --verify test.txt.gpg
Retrieving gpg -d -o test.txt test.txt.gpg

Clear Signature

The contents of the input file is signed in clear text. The signature and the contents of the input file are placed in clear text in a .asc file. This file can be viewed with any text editor and the signature and contents are fully visible.

Signing gpg --clearsign test.txt
Verifying gpg --verify test.txt.asc
Retrieving gpg -d -o test.txt test.txt.asc

Detached Signature

A signature for the input file is created and stored in a .sig file. Typically, the signature file is bundled with the input file with the same name except with the .sig file extension.

Signing gpg --detach-sign test.txt
Verifying gpg --verify test.txt.sig test.txt

Automating Encrypted Backups using Asymmetric Keys

#!/bin/sh

HOSTNAME=`hostname`
DATE=`date +%Y-%m-%d`

# Get the public key to encrypt with
wget -O - http://leo.home.steamr.com/public/backup_key.pub | gpg --import

# Create archive, pipe to gpg to encrypt, then send to freebsd
tar -czpf - \
        /usr/share/sickbeard/ \
        /etc/init.d/sickbeard  \
        \
        /usr/share/SABnzbd/ \
        /etc/init.d/SABnzbd  \
        \
        /home/rt \
        /var/www/html/rutorrent \
        /usr/lib64/httpd/modules/mod_scgi.so \
        /etc/httpd/conf.d \
        /etc/httpd/conf/httpd.conf \
        \
        /etc/fstab \
        /etc/passwd \
        /etc/group \
        /etc/shadow \
| gpg --encrypt --yes --no-tty --trust-model always --recipient leo@steamr.com \
| ssh backup@freebsd "cd /storage/backups/archived/sickbeard; cat - > $HOSTNAME.$DATE.tar.gz.gpg"

# Requires mailx, with smtp=smtp://mail.home.steamr.com set in /etc/mail.rc
echo -e "Backup Completed on `date`.\nUptime: `uptime`\n\nProcesses:\n`ps -eaf`" | mail -s "Backup Completed on `hostname`" "leo@home.steamr.com"

# delete the public backup key
gpg --batch --yes --delete-key leo@steamr.com