Home > Unix/Linux > PGP key generation using gpg 1.4.5 on Linux

PGP key generation using gpg 1.4.5 on Linux

Step 1 – Confirm GPG version


$gpg -help
gpg (GnuPG) 1.4.5
Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Step 2 – Start generating gpg key


$ gpg --gen-key
gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Please select what kind of key you want:
(1) DSA and Elgamal (default)
(2) DSA (sign only)
(5) RSA (sign only)
Your selection? 5
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (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) <heinrichh@duesseldorf.de>"

Real name: <User ID for which the key is being generated>
Email address: <Valid mail ID>
Comment:
You selected this USER-ID:
"<User ID provided earlier> <Mail ID>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

You don't want a passphrase - this is probably a *bad* idea!
I will do it anyway. You can change your passphrase at any time,
using this program with the option "--edit-key".

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 276 more bytes)
....+++++
..+++++
gpg: key 193EAC92 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 2048R/193EAC92 2016-11-15
Key fingerprint = F7B1 F82D 8DA3 850B 5F8A 5905 B93D 5AF3 193E AC92
uid <User ID provided earlier> <Mail ID>

Note that this key cannot be used for encryption. You may want to use
the command "--edit-key" to generate a subkey for this purpose.

Step 3 – List your keys


$ gpg -k
/home/.gnupg/pubring.gpg
-----------------------------------------
pub 2048R/193EAC92 2016-11-15
uid <User ID provided earlier> <Mail ID>

Step 4 –  Export the public key in ASCII format


$ gpg --armor --output <User ID>-pub.asc --export '<User ID>'
$
$ ls -ltr
-rw-rw-r-- 1 xxx xxxyy 979 Nov 15 09:28 -pub.asc

$ cat <User ID>-pub.asc
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.5 (GNU/Linux)

mQENBFgrKJYBCADMiFYMrVbhl7HH/WLPqPN5SSSaxv5go92m9LjcLysU0Vd9+JTi
hck+8zy3wutY4Q6QK61Zlx72J/Va+4hcv8tMKaJjWfhEGbXV54yKNjNoSM20BtuO
AC5+Z4/2UWA3xh3S1JPRRiV3KDO0MTM2JdhH9sufkhpBsLdJd+jFq+X8o633hqAk
xK75ihNYyof0Pi8VqGJdmaEwgzy1/bXIYeH8wbivC7yx6Kg84oF5Znii50tP6Grq
JygEcnyVjrfS6frsnO4uIBQEuFFoqLGHBohWKQqwkRU6Sd6KTIFkdN6E/SE1XelF
EDMURzDczxsQYaNH1A5QRLNxAC2Zm+uCt76bABEBAAG0NHN2Y19Xb3JrZGF5X3Nm
dHAgPGFuYW5kLm1hbmRpbHdhci1zY0BhaXJsaXF1aWRlLmNvbT6JATYEEwECACAF
AlgrKJYCGwMGCwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRC5PVrzGT6skkMmB/4m
SxJJ/cgPBcMArpUpsargl/g4SOVuH1RKIBi+Sjqt1n7ioW3zfceUxEz0u3t76KaN
4+ndKrCxxCokL8xOu0Qgq1tQtdl5MiONwJ7hvxa459U7qjDE9joj/4WcnxZVIYhj
5+ZPxdR+dfiFmJDyIkln1wUnB8RmAov/hzNCvx50nmLJUIVM6C+e3VWyDqDx66Uh
vHUPpLY+wk/d+qyHeldk4nqa5z+A6Oh3ZB1qcc9h9mEd3tDHQLZiaiqXst0pFQB+
3X9QiSektAXrKSIuOf7Bky4DjDfpoCWuSDDpTaF1IIUTv3YrosbyRamAJ3fAu39x
ltW8BKpKP3nJWKmWmw7i
=EkgV
-----END PGP PUBLIC KEY BLOCK-----
$

How to copy PGP public and private key from one machine (Linux here) to other Linux.

Step# 1: Export private secret key on the source machine

$ gpg --export-secret-keys -a <key_ID> > myfilename_private_key.asc

key_ID – when you list the key using “gpg -k”

pub 4096R/AD761536 2017-03-29
uid <User_Name> <username@domain.com>
sub 4096R/B045ADCF 2017-03-29
AD761536 – this is the key_ID
Step# 2: Export public secret key

$ gpg --export -a <key_ID> > myfilename_public_key.asc

Step # 3: SCP these 2 files to the target server – where you want to copy the PGP keys (in my case it is other Linux server)
Step # 4: Import the private and public key copied in Step# 3 on the target machine.

$ gpg --import myfilename_private_key.asc
gpg: key ADC61536: secret key imported
gpg: key ADC61536: public key "<User_name> <username@domain.com>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
gpg: secret keys read: 1
gpg: secret keys imported: 1

$ gpg --import myfilename_public_key.asc <-- this is for public key
gpg: key ADC61536: "<User_name> <username@domain.com>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1

Step # 5: Change the trust level
Now you need to change the “trust” level of the private key thus imported to new server

$ gpg --edit-key username@domain.com
gpg (GnuPG) 1.4.18; Copyright (C) 2014 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.

Secret key is available.

pub 4096R/ADC61536 created: 2017-03-29 expires: never usage: SC
 trust: unknown validity: unknown
sub 4096R/B044ADFF created: 2017-03-29 expires: never usage: E
[ unknown] (1). <User_name> <username@domain.com>
gpg> trust
pub 4096R/ADC61536 created: 2017-03-29 expires: never usage: SC
 trust: unknown validity: unknown
sub 4096R/B033456FF created: 2017-03-29 expires: never usage: E
[ unknown] (1). <User_name> <username@domain.com>

Please decide how far you trust this user to correctly verify other users' keys
(by looking at passports, checking fingerprints from different sources, etc.)

 1 = I don't know or won't say
 2 = I do NOT trust
 3 = I trust marginally
 4 = I trust fully
 5 = I trust ultimately
 m = back to the main menu

Your decision? 5
Do you really want to set this key to ultimate trust? (y/N) y

pub 4096R/ADC61536 created: 2017-03-29 expires: never usage: SC
 trust: ultimate validity: unknown
sub 4096R/B033456FF created: 2017-03-29 expires: never usage: E
[ unknown] (1). <User_name> <username@domain.com>
Please note that the shown key validity is not necessarily correct
unless you restart the program.

gpg> quit

Now if you list the keys using “gpg -k”, you will find the PGP key ID and associated details are exactly the same as that of the source server.

Happy reading.

-Anand M

Advertisement
Categories: Unix/Linux Tags: , , ,
  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 )

Connecting to %s

%d bloggers like this: