Home > best practices > Using GPG for Public-Key Cryptography

Using GPG for Public-Key Cryptography

Wikipedia has an accessible introduction of the subject. The GNU Privacy Manual is a good resource for detailed documentation.

Export Public Keys

To allow us to encrypt files with the public key of our correspondent, we first need to have access to their public key. Users may export their public key as follows:

% gpg --export --armor musa@persia.com \

Import Public Keys

We may import public keys into our public keyring as follows. Doing so will enable us to encrypt data using this key.

% gpg --import \<musa@persia.com\>.public.gpg-key

Delete Public Keys

We may remove the public key from our public-key keyring by using name.

% gpg --delete-key musa@persia.com

List Public Keys

We can see the public keys listed in our public-key keyring using --list-keys

% gpg --list-keys
pub   4096R/9E520AE7 2016-09-15
uid                  Umar Kalim <umar@vt.edu>
sub   4096R/38C5C69A 2016-09-15
sub   4096R/5DE83AAF 2016-09-15

pub   4096R/B7AC8904 2016-09-25
uid                  Musa Al-Khwarizmi <musa@persia.com>
sub   4096R/4E74D6FE 2016-09-25
sub   4096R/7756B7E1 2016-09-25

Encrypting Files

To encrypt data we need to use the recipient’s public key. We may identify the recipient using the -r or the --recipient option followed by the recipients name or email. The --encrypt option highlights our intent to encrypt. By default gpg outputs encrypted content in binary format. To generate output in ASCII-armor format, we use the --armor option.

% gpg --encrypt --armor -r musa@persia.com file-1.pdf

Signing and Encrypting Files

By signing the contents, the recipient can guarantee that the contents were signed by our primary key.

% gpg --encrypt --sign --armor -r musa@persia.com file-1.pdf

Decrypting Files

Assuming that the private key associated with the public key used to encrypt the data is in our keyring, we may decrypt the encrypted contents as follows.

% gpg --decrypt file-1.pdf.asc > output.pdf
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: