Symmetric encryption is great if you're just using GPG locally for your own stuff, but what if you want to share an encrypted file with someone, while ensuring that they can decrypt it? With symmetric encryption, you'd need to find a secure way to transmit the passphrase for the file to the file's recipient. In doing so, there will always be the risk that some third party could intercept the passphrase, and could then get into your stuff. Here's where asymmetric encryption comes to the rescue. To demonstrate, I'm going to create a file, encrypt it, and send it to my buddy Frank to decrypt.
To begin, both Frank and I have to create a key set, as we've already shown you. Next, each of us needs to extract our public keys, and send them to each other. We'll extract the key into an ASCII text file:
cd .gnupg
gpg --export -a -o donnie_public-key.txt
donnie@ubuntu:~/.gnupg$ ls -l
total 36
-rw-rw-r-- 1 donnie donnie 1706 Oct 27 18:14 donnie_public-key.txt
. . .
frank@ubuntu:~/.gnupg$ ls -l
total 36
-rw-rw-r-- 1 frank frank 1714 Oct 27 18:18 frank_public-key.txt
Normally, the participants in this would either send their keys to each other through an email attachment, or by placing the keys in a shared directory. In this case, Frank and I will receive each other's public key files, and place them into our respective .gnupg directories. Once that's done, we're ready to import each other's keys:
donnie@ubuntu:~/.gnupg$ gpg --import frank_public-key.txt
gpg: key 4CFC6990: public key "Frank Siamese (I am a cat.) <frank@any.net>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
donnie@ubuntu:~/.gnupg$
frank@ubuntu:~/.gnupg$ gpg --import donnie_public-key.txt
gpg: key 9FD7014B: public key "Donald A. Tevault <donniet@something.net>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
frank@ubuntu:~/.gnupg$
Now for the good stuff. I've created a super-secret message for Frank, and will asymmetrically encrypt it (-e) and sign it (-s). (Signing the message is the verification that the message really is from me, rather than from an impostor.):
donnie@ubuntu:~$ gpg -s -e secret_stuff_for_frank.txt
You need a passphrase to unlock the secret key for
user: "Donald A. Tevault <donniet@something.net>"
2048-bit RSA key, ID 9FD7014B, created 2017-10-27
gpg: gpg-agent is not available in this session
You did not specify a user ID. (you may use "-r")
Current recipients:
Enter the user ID. End with an empty line: frank
gpg: CD8104F7: There is no assurance this key belongs to the named user
pub 2048R/CD8104F7 2017-10-27 Frank Siamese (I am a cat.) <frank@any.net>
Primary key fingerprint: 4806 7483 5442 D62B B9BD 95C1 9564 92D4 4CFC 6990
Subkey fingerprint: 9DAB 7C3C 871D 6711 4632 A5E0 6DDD E3E5 CD81 04F7
It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes.
Use this key anyway? (y/N) y
Current recipients:
2048R/CD8104F7 2017-10-27 "Frank Siamese (I am a cat.) <frank@any.net>"
Enter the user ID. End with an empty line:
donnie@ubuntu:~$
So, the first thing I had to do was to enter the passphrase for my private key. Where it says to enter the user ID, I entered frank, since he's the intended recipient of my message. But, look at the line after that, where it says, There is no assurance this key belongs to the named user. That's because I still haven't trusted Frank's public key. We'll get to that in a bit. The last line of the output again says to enter a user ID, so that we can designate multiple recipients. But, Frank is the only one I care about right now, so I just hit the Enter key to break out of the routine. This results in a .gpg version of my message to Frank:
donnie@ubuntu:~$ ls -l
total 8
. . .
-rw-rw-r-- 1 donnie donnie 143 Oct 27 18:37 secret_stuff_for_frank.txt
-rw-rw-r-- 1 donnie donnie 790 Oct 27 18:39 secret_stuff_for_frank.txt.gpg
donnie@ubuntu:~$
My final step is to send Frank his encrypted message file, by whatever means available.
When Frank receives his message, he'll use the -d option to view it:
frank@ubuntu:~$ gpg -d secret_stuff_for_frank.txt.gpg
You need a passphrase to unlock the secret key for
user: "Frank Siamese (I am a cat.) <frank@any.net>"
2048-bit RSA key, ID CD8104F7, created 2017-10-27 (main key ID 4CFC6990)
gpg: gpg-agent is not available in this session
gpg: encrypted with 2048-bit RSA key, ID CD8104F7, created 2017-10-27
"Frank Siamese (I am a cat.) <frank@any.net>"
This is TOP SECRET stuff that only Frank can see!!!!!
If anyone else see it, it's the end of the world as we know it.
(With apologies to REM.)
gpg: Signature made Fri 27 Oct 2017 06:39:15 PM EDT using RSA key ID 9FD7014B
gpg: Good signature from "Donald A. Tevault <donniet@something.net>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: DB0B 31B8 876D 9B2C 7F12 9FC3 886F 3357 9FD7 014B
frank@ubuntu:~$
Frank enters the passphrase for his private key, and he sees the message. At the bottom, he sees the warning about how my public key isn't trusted, and that there's no indication that the signature belongs to the owner. Well, since Frank knows me personally, and he knows for a fact that the public key really is mine, he can add my public key to the trusted list:
frank@ubuntu:~$ cd .gnupg
frank@ubuntu:~/.gnupg$ gpg --edit-key donnie
gpg (GnuPG) 1.4.20; 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.
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u
pub 2048R/9FD7014B created: 2017-10-27 expires: never usage: SC
trust: ultimate validity: ultimate
sub 2048R/9625E7E9 created: 2017-10-27 expires: never usage: E
[ultimate] (1). Donald A. Tevault <donniet@something.net>
gpg>
The last line of this output is the command prompt for the gpg shell. Frank is concerned with trust, so he'll enter the command, trust:
gpg> trust
pub 2048R/9FD7014B created: 2017-10-27 expires: never usage: SC
trust: unknown validity: unknown
sub 2048R/9625E7E9 created: 2017-10-27 expires: never usage: E
[ unknown] (1). Donald A. Tevault <donniet@something.net>
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
Frank has known me for quite a while, and he knows for a fact that I'm the one who sent the key. So, he chooses option 5 for ultimate trust. Once Frank logs out and logs back in, that trust will take effect:
frank@ubuntu:~$ gpg -d secret_stuff_for_frank.txt.gpg
You need a passphrase to unlock the secret key for
user: "Frank Siamese (I am a cat.) <frank@any.net>"
2048-bit RSA key, ID CD8104F7, created 2017-10-27 (main key ID 4CFC6990)
gpg: gpg-agent is not available in this session
gpg: encrypted with 2048-bit RSA key, ID CD8104F7, created 2017-10-27
"Frank Siamese (I am a cat.) <frank@any.net>"
This is TOP SECRET stuff that only Frank can see!!!!!
If anyone else see it, it's the end of the world as we know it.
(With apologies to REM.)
gpg: Signature made Fri 27 Oct 2017 06:39:15 PM EDT using RSA key ID 9FD7014B
gpg: Good signature from "Donald A. Tevault <donniet@something.net>"
frank@ubuntu:~$
With no more warning messages, this looks much better. At my end, I'll do the same thing with Frank's public key.
What's so very cool about this is that even though the whole world may have my public key, it's useless to anyone who isn't a designated recipient of my message.
sudo apt install gnupg-agent