2020-06-18 10:59:51 -04:00
<!--
2021-03-25 06:38:48 -04:00
Title: How to encrypt, sign and decrypt messages using GnuPG on macOS
2020-10-02 09:42:15 -04:00
Description: Learn how to encrypt, sign and decrypt messages using PGP on macOS.
2020-06-18 10:59:51 -04:00
Author: Sun Knudsen < https: / / github . com / sunknudsen >
2021-01-10 10:45:20 +00:00
Contributors: Sun Knudsen < https: / / github . com / sunknudsen > , Albert < https: / / github . com / farwel >
2020-09-10 10:07:07 -04:00
Reviewers:
2020-06-18 10:59:51 -04:00
Publication date: 2020-06-18T00:00:00.000Z
2020-11-10 07:13:35 -05:00
Listed: true
2023-09-06 19:23:39 -04:00
Pinned:
2020-06-18 10:59:51 -04:00
-->
2021-03-25 06:38:48 -04:00
# How to encrypt, sign and decrypt messages using GnuPG on macOS
2020-06-18 10:59:51 -04:00
2023-09-06 19:28:21 -04:00
[](https://www.youtube.com/watch?v=mE8fL5Fu8x8 "How to encrypt, sign and decrypt messages using GnuPG on macOS")
2020-06-25 13:56:31 -04:00
2022-04-27 09:30:49 -04:00
> Heads-up: this is a “getting started” guide. Learn how to harden GnuPG [here](../how-to-generate-and-air-gap-pgp-private-keys-using-gnupg-tails-and-yubikey).
2021-03-25 06:38:48 -04:00
2021-11-05 09:07:15 -04:00
## Requirements
2021-12-29 10:00:15 -05:00
- Computer running macOS Big Sur or Monterey
2021-11-05 09:07:15 -04:00
2021-03-25 06:38:48 -04:00
## Caveats
- When copy/pasting commands that start with `$` , strip out `$` as this character is not part of the command
2020-06-18 10:59:51 -04:00
2020-12-31 10:35:49 -05:00
## Setup guide
2020-06-18 10:59:51 -04:00
2020-08-10 19:35:31 -04:00
### Step 1: install [Homebrew](https://brew.sh/)
2020-06-18 10:59:51 -04:00
2021-11-05 09:40:41 -04:00
```console
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
$ uname -m | grep arm64 & & echo 'export PATH=$PATH:/opt/homebrew/bin' >> ~/.zshrc & & source ~/.zshrc
2020-06-18 10:59:51 -04:00
```
2020-09-10 10:10:51 -04:00
### Step 2: disable Homebrew analytics
2020-06-18 10:59:51 -04:00
```shell
brew analytics off
```
2020-08-10 19:35:31 -04:00
### Step 3: install [GnuPG](https://gnupg.org/)
2020-06-18 10:59:51 -04:00
```shell
brew install gnupg
```
2020-08-10 19:35:31 -04:00
### Step 4: generate PGP key pair
2020-06-18 10:59:51 -04:00
2020-08-05 17:14:52 -04:00
```console
2020-06-18 10:59:51 -04:00
$ gpg --full-generate-key
2021-12-29 10:00:15 -05:00
gpg (GnuPG) 2.3.4; Copyright (C) 2021 Free Software Foundation, Inc.
2020-06-18 10:59:51 -04:00
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
2020-12-11 08:39:56 -05:00
gpg: directory '/Users/sunknudsen/.gnupg' created
gpg: keybox '/Users/sunknudsen/.gnupg/pubring.kbx' created
2020-06-18 10:59:51 -04:00
Please select what kind of key you want:
2021-12-29 10:00:15 -05:00
(1) RSA and RSA
2020-06-18 10:59:51 -04:00
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
2021-12-29 10:00:15 -05:00
(9) ECC (sign and encrypt) *default*
(10) ECC (sign only)
2020-06-18 10:59:51 -04:00
(14) Existing key from card
2021-12-29 10:00:15 -05:00
Your selection? 9
Please select which elliptic curve you want:
(1) Curve 25519 *default*
(4) NIST P-384
(6) Brainpool P-256
2020-06-18 10:59:51 -04:00
Your selection? 1
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) 0
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.
Real name: John Doe
Email address: john@example .net
Comment:
You selected this USER-ID:
"John Doe < john @example .net > "
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
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.
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.
2020-12-11 08:39:56 -05:00
gpg: /Users/sunknudsen/.gnupg/trustdb.gpg: trustdb created
gpg: directory '/Users/sunknudsen/.gnupg/openpgp-revocs.d' created
2021-12-29 10:00:15 -05:00
gpg: revocation certificate stored as '/Users/sunknudsen/.gnupg/openpgp-revocs.d/1ADDDBA409558A8E80A4DF381535F6A0BB6BD636.rev'
2020-06-18 10:59:51 -04:00
public and secret key created and signed.
2021-12-29 10:00:15 -05:00
pub ed25519 2021-12-29 [SC]
1ADDDBA409558A8E80A4DF381535F6A0BB6BD636
2020-06-18 10:59:51 -04:00
uid John Doe < john @example .net >
2021-12-29 10:00:15 -05:00
sub cv25519 2021-12-29 [E]
2020-06-18 10:59:51 -04:00
```
2022-04-27 09:30:49 -04:00
### Step 5: back up `~/.gnupg` folder (learn how [here](../how-to-back-up-and-encrypt-data-using-rsync-and-veracrypt-on-macos))
2020-06-18 10:59:51 -04:00
2021-07-21 19:48:34 -04:00
> Heads-up: files stored in `~/.gnupg` include private keys which, if lost, results in loosing one’ s cryptographic identity (safeguard backup mindfully).
2020-06-18 10:59:51 -04:00
2020-12-03 06:29:52 -05:00
👍
2020-06-18 10:59:51 -04:00
---
## Usage guide
2021-12-29 10:00:15 -05:00
### Export PGP public key
> Heads-up: replace `john@example.net` and `johndoe` with email and name from [step 4](#step-4-generate-pgp-key-pair).
2020-06-18 10:59:51 -04:00
```shell
2021-12-29 10:00:15 -05:00
gpg --armor --export john@example .net > ~/johndoe.asc
2020-06-18 10:59:51 -04:00
```
2021-12-29 11:40:32 -05:00
### Import Sun’ s PGP public key using key server…
2020-06-18 10:59:51 -04:00
2021-03-25 06:38:48 -04:00
```console
2021-12-29 10:00:15 -05:00
$ gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x8C9CA674C47CA060
gpg: key 8C9CA674C47CA060: public key "Sun Knudsen < hello @sunknudsen .com > " imported
2021-03-25 06:38:48 -04:00
gpg: Total number processed: 1
gpg: imported: 1
2020-06-18 10:59:51 -04:00
```
2021-03-25 06:38:48 -04:00
imported: 1
2020-06-18 10:59:51 -04:00
2021-03-25 06:38:48 -04:00
👍
2021-12-29 11:40:32 -05:00
### …or using PGP public key URL
2021-03-25 06:38:48 -04:00
2021-12-29 10:00:15 -05:00
> Heads-up: verify [web of trust](#verify-suns-pgp-public-key-using-web-of-trust) to list missing keys.
2021-03-25 06:38:48 -04:00
```console
$ curl https://sunknudsen.com/sunknudsen.asc | gpg --import
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
2021-12-29 10:00:15 -05:00
100 2070 100 2070 0 0 1881 0 0:00:01 0:00:01 --:--:-- 1899
gpg: key 8C9CA674C47CA060: 1 signature not checked due to a missing key
gpg: key 8C9CA674C47CA060: public key "Sun Knudsen < hello @sunknudsen .com > " imported
2021-03-25 06:38:48 -04:00
gpg: Total number processed: 1
gpg: imported: 1
2021-12-29 10:00:15 -05:00
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
2020-06-18 10:59:51 -04:00
```
2021-03-25 06:38:48 -04:00
imported: 1
👍
2021-12-29 10:00:15 -05:00
### Verify Sun’ s PGP public key using fingerprint
2020-06-18 10:59:51 -04:00
2020-08-05 17:14:52 -04:00
```console
2020-06-18 10:59:51 -04:00
$ gpg --fingerprint hello@sunknudsen .com
2021-12-29 10:00:15 -05:00
pub ed25519 2021-12-28 [C]
E786 274B C92B 47C2 3C1C F44B 8C9C A674 C47C A060
2020-06-18 10:59:51 -04:00
uid [ unknown] Sun Knudsen < hello @sunknudsen .com >
2021-12-29 10:00:15 -05:00
sub ed25519 2021-12-28 [S] [expires: 2022-12-28]
sub cv25519 2021-12-28 [E] [expires: 2022-12-28]
sub ed25519 2021-12-28 [A] [expires: 2022-12-28]
2020-06-18 10:59:51 -04:00
```
2021-12-29 10:00:15 -05:00
Open https://sunknudsen.com/, https://github.com/sunknudsen/pgp-public-key and https://www.youtube.com/sunknudsen/about and make sure above fingerprint (“E786 274B C92B 47C2 3C1C F44B 8C9C A674 C47C A060”) matches published fingerprints.
2020-06-18 10:59:51 -04:00
👍
2021-12-29 10:00:15 -05:00
### Verify Sun’ s PGP public key using web of trust
> Heads-up: `0xC1323A377DE14C8B` is Sun’ s [legacy](https://github.com/sunknudsen/pgp-public-key/tree/master/legacy) public key.
```console
2021-12-29 11:40:32 -05:00
$ gpg --list-signatures hello@sunknudsen .com
pub ed25519 2021-12-28 [C]
E786274BC92B47C23C1CF44B8C9CA674C47CA060
uid [ unknown] Sun Knudsen < hello @sunknudsen .com >
sig 3 8C9CA674C47CA060 2021-12-28 Sun Knudsen < hello @sunknudsen .com >
sig 3 C1323A377DE14C8B 2021-12-28 [User ID not found]
sub ed25519 2021-12-28 [S] [expires: 2022-12-28]
sig 8C9CA674C47CA060 2021-12-28 Sun Knudsen < hello @sunknudsen .com >
sub cv25519 2021-12-28 [E] [expires: 2022-12-28]
sig 8C9CA674C47CA060 2021-12-28 Sun Knudsen < hello @sunknudsen .com >
sub ed25519 2021-12-28 [A] [expires: 2022-12-28]
sig 8C9CA674C47CA060 2021-12-28 Sun Knudsen < hello @sunknudsen .com >
2021-12-29 10:00:15 -05:00
```
### Paste, encrypt and sign message (enter line break and `ctrl+d` to quit edit mode)
2020-06-18 10:59:51 -04:00
2020-08-05 17:14:52 -04:00
```console
2021-03-12 14:08:02 -05:00
$ gpg --encrypt --sign --armor --output ~/Desktop/encrypted.asc --recipient john@example .net --recipient hello@sunknudsen .com
2021-12-29 10:00:15 -05:00
gpg: F56809CDE05DB014: There is no assurance this key belongs to the named user
2020-06-18 10:59:51 -04:00
2021-12-29 10:00:15 -05:00
sub cv25519/F56809CDE05DB014 2021-12-28 Sun Knudsen < hello @sunknudsen .com >
Primary key fingerprint: E786 274B C92B 47C2 3C1C F44B 8C9C A674 C47C A060
Subkey fingerprint: F375 2162 E3A4 3F6E 2762 D50B F568 09CD E05D B014
2020-06-18 10:59:51 -04:00
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
This is a test!
```
2020-08-10 19:35:31 -04:00
### Decrypt message to stdout and decode quoted-printable characters
2020-06-18 10:59:51 -04:00
2020-08-05 17:14:52 -04:00
```console
2021-01-10 09:49:15 +01:00
$ gpg --decrypt ~/Desktop/encrypted.asc | perl -MMIME::QuotedPrint -0777 -nle 'print decode_qp($_)'
2021-12-29 10:00:15 -05:00
gpg: encrypted with cv25519 key, ID F56809CDE05DB014, created 2021-12-28
2020-06-18 10:59:51 -04:00
"Sun Knudsen < hello @sunknudsen .com > "
2021-12-29 10:00:15 -05:00
gpg: encrypted with cv25519 key, ID F9220AB453F9B6E3, created 2021-12-29
2020-06-18 10:59:51 -04:00
"John Doe < john @example .net > "
2021-12-29 10:00:15 -05:00
gpg: Signature made Wed 29 Dec 08:24:05 2021 EST
gpg: using EDDSA key 1ADDDBA409558A8E80A4DF381535F6A0BB6BD636
2020-06-18 10:59:51 -04:00
gpg: Good signature from "John Doe < john @example .net > " [ultimate]
This is a test!
```
2021-03-25 06:38:48 -04:00
Good signature
👍
2020-08-10 19:35:31 -04:00
### Clear passphrase from GnuPG cache
2020-06-18 10:59:51 -04:00
```shell
gpg-connect-agent reloadagent /bye
```
2021-03-14 09:15:46 -04:00
2022-01-08 15:33:18 -05:00
> Heads-up: when sending [encrypted messages](#paste-encrypt-and-sign-message-enter-line-break-and-ctrld-to-quit-edit-mode), don’ t forget to include your [public key](#export-pgp-public-key).