Generate your key

  1. Run following command in your shell,
    gpg --gen-key
    Now program will ask you to choose couple of options, use following preferences

  2. Please select what kind of key you want: 1 RSA and RSA (default)

  3. What keysize do you want? (2048) 4096

  4. Key is valid for? (0) 0

  5. Is this correct? (y/N) y

  6. Now enter name, email and comment message.

  7. Change (N)ame, (C)omment, (E)-mail or (O)kay/(Q)uit? o

  8. Finally, enter a passphrase to protect your secret key.

Edit your key

We can later edit key to use other options. e.g Lets set our key to use stronger hashes. 1. Edit key using following command,
gpg --edit-key test@example.com

  1. Now set hash preferences as follows,
    gpg> setpref SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP

  2. Really update the preferences? (y/N) y

  3. Enter your passphrase

  4. Save new preferences by command,
    gpg> save

Make available your key

There are 2 ways to make available your key to other users

  1. Give them manually
    Use following command,
    gpg --armor --export test@example.com
    You will get your public key. Copy and paste it and send to other user.

  2. Upload to key server
    Uploading public key to key server can be done by two ways.
    First is using forms available on server and second is using gpg interface.
    For later try following command,
    gpg --list-keys test@example.com
    Grab your key id from the output and upload to keyserver like http://pgp.mit.edu,
    gpg --send-keys --keyserver pgp.mit.edu <key-id>

Importing other keys

  1. Import other user's keys
    We can import keys of other users with multiple ways.
  2. from text file
    if someone sends you text file containing his public key, import it as,
    gpg --import <pub_key_file>

  3. from key server
    There are some popular key serves which host public keys. One of such server is http://pgp.mit.edu Here you can search particular user's key as follows,
    gpg --keyserver pgp.mit.edu --search-keys <string>

  4. Validate key
    The easy way to validate person's identification is match fingerprint of key
    gpg --fingerprint test@example.com

  5. Sign imported key
    Sign imported key as,
    gpg --sign-key test@example.com

  6. Optionally you can send back signed key

Using gpg key

To encrypt message using your key use following command,
gpg --encrypt --sign --armor -r test@example.com <filename>

To decrypt file,
gpg <filename>

Creating revocation certificate

There is always possibility that your master key-pair may get lost. (and may be stolen if you are unfortunate). If this happen, you must tell other people to not use your public key. This can be done using revocation certificate.
Generate revocation certificate using following command,
gpg --output \<test@example.com\>.gpg-revocation-certificate --gen-revoke test@example.com
Store it safe somewhere separately from master key-pair

Some useful commands

  1. List available keys
    gpg --list-keys

  2. Update key information
    gpg --refresh-keys