Skip to content

Instantly share code, notes, and snippets.

@arcolife
Last active August 4, 2018 00:54
Show Gist options
  • Save arcolife/0ccf6e5b522559f98af76ae762bc3879 to your computer and use it in GitHub Desktop.
Save arcolife/0ccf6e5b522559f98af76ae762bc3879 to your computer and use it in GitHub Desktop.
gpg keys generation / import / encryption with someone's public key

Import someone's public GPG key

# Down his public key and saving it to the file key.asc.
# Then use the following command to add it to your keyring:
$ gpg --import <path to key.asc>

# ..check if imported successfully:
$  gpg --list-keys

Encrypt with newly imported key and their email ID

# Ex: Encrypt a credentials file with:
$ gpg -e -r   <his RH email>@redhat.com   <path to credentials file>

generate a GPG key

However if you don't have a GPG key generated yet (you could tell by looking listing out keys or merely looking for ~/.gnupg folder on your machine)

$ gpg --gen-key
gpg (GnuPG) 1.4.11; Copyright (C) 2010 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 selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
         0 = key does not expire
        = key expires in n days
      w = key expires in n weeks
      m = key expires in n months
      y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
 
You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) "
 
Real name: Linuxaria admin
Email address: admin@linuxaria.com
Comment: 
You selected this USER-ID:
    "Linuxaria admin "
 
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.
 
gpg: gpg-agent is not available in this session
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
 
Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 26 more bytes)
...........+++++
........+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key A7B8B4DD marked as ultimately trusted
public and secret key created and signed.
 
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   4096R/A7B8B4DD 2013-01-11
      Key fingerprint = AF7B 310A 57FF 0524 91A6  E483 83F7 FE98 A7B8 B4DD
uid                  Linuxaria admin 
sub   4096R/E427331B 2013-01-11

Traceback courtesy:

print GPG key in ASCII format

Precautionary telltales:

For example, this could be pasted into github's GPG settings in account, for commits to be signed with your GPG keys. Be careful though, if you loose your laptop / private key, the verification in future will be invalidated.

$ gpg --list-secret-keys --keyid-format LONG

/home/username/.gnupg/secring.gpg
---------------------------------
sec   4096R/CD16D23A5E9E2793 2016-06-20
uid                          Blah Blah <blah@gmail.com>
ssb   2048R/5EAD76E6D501D3FD 2016-06-20

$ gpg --armor --export CD16D23A5E9E2793 

-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBFdn/lYBEADBEhVIjHRu6OPzKhASzS035PZ9mCq79gYSVU+WyEw5gddPfrZN
CNA/5TWsFdU3asysgp0WadTY9S2ZI9E6PgTAIc5R/XpIYMf7dZGEp4gHDBFFaVvf
...
...
wOc769IF4b8bLTAd1bP6/gDA6kdrpLhGb7P9fHExrHHcEJ845/SI69RRvw==
=Q5My
-----END PGP PUBLIC KEY BLOCK----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment