There are two kinds of passwords in the world. One is to prevent your little sister from reading your files. The other is to prevent the authorities from reading your documents.
— Bruce Schneier, Applied Cryptography
In 2021, use PGP in a more modern way (part 1)
– PGP basis
I. Introduction to PGP
What is?
PGP, which stands for Pretty Good Privacy, is a software designed to encrypt information and protect Privacy. Now when I say “PGP”, I basically mean the OpenPGP standard.
Can do?
It can be used to encrypt and sign messagesAnd make you seem geeky
Edward Snowden used PGP to exchange emails with journalist Laura Poitras in the documentary Citizen Four. The following is a screenshot from the film:
PGP ensures that a message is sent by someone you trust, undeciphered by anyone but the two of you, and that the message is transmitted without any modification of a single punctuation mark or byte in between.
Can be used toMake your Git Commit Log look betterVerify the Git Commiter identity
Github determines that submitters rely only on the user.name and user.email Settings of the Git client to determine their identity, and does not verify authenticity, meaning that you can forge anyone’s commit record in your repository.
For details, see Shock! Someone is pretending to be me on GitHub!
Can be used toPut it in your blog profile as a status symbolIncrease safe ways to contact you
Instead of SSH
After obtaining the PGP key, you can use GPG-Agent to replace OpenSSH Agent for SSH operations. However, the replacement does not increase the security of SSH.
On the upside, it’s probably easier to use Yubikey(a hardware-encrypted smart card) for SSH.
Used to…
Let me know in the comments or email if you have any other fun uses.
Why is it safe?
PGP currently supports algorithms
- Asymmetric algorithms: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
- Symmetric algorithms: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128, CAMELLIA192, CAMELLIA256
- Hash algorithm: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
- Compression algorithm: Uncompressed, ZIP, ZLIB, BZIP2
Unless a quantum computer lands, 2048-bit RSA encryption is currently unbreakable.
PGP, OpenPGP, GnuPG and GPG
We need to know the difference between PGP, OpenPGP, GnuPG and GPG:
- PGP (” Pretty Good Privacy “) was the name of the original commercial software
- OpenPGP is an IETF standard compatible with the original PGP tools
- GnuPG (Gnu Privacy Guard) is free software that implements the OpenPGP standard
- GnuPG’s command-line tool is called “GPG”
OpenPGP is an open standard defined on top of PGP, enabling PGP technology to be implemented by any company or individual without a license fee. The draft standard states that OpenPGP provides a data integrity service that gives users the ability to view, verify, generate, and write encrypted information, keys, and signatures. OpenPGP implements these functions through encryption, digital signature, compression, and Radix-64 transformation.
GnuPG is a complete implementation of OpenPGP protocol. In addition to providing data encryption, decryption and signature services according to OpenPGP protocol, it also provides complete secret key management functions and implements many optional encryption or compression algorithms in the protocol.
Today, the word “PGP” is almost universally used to refer to the open OpenPGP standard rather than the original commercial software, so “PGP” and “OpenPGP” are interchangeable.
The words “GnuPG” and “GPG” should only be used when referring to tools, not to the output they produce or the OpenPGP functionality they implement. For example:
- PGP (not GnuPG or GPG) key
- PGP (not GnuPG or GPG) signature
- PGP (not GnuPG or GPG) public key server
history
From wiki:
Philip R. Zimmermann created the first version of PGP in 1991, The name Pretty Good Privacy was inspired by Ralph’s Pretty Good Grocery, radio anchor Garrison Keillor’s fictional Grocery in a city called Lake Wobegon.
The first version of the software contained a symmetric key algorithm of Zimmerman’s own design, the same name as the Saturday Night Live sketch BassOmatic. A longtime anti-nuclear activist, Zimmerman created PGP encryption so that anyone with similar tendencies could safely use BBS and store messages and files securely. It is not licensed for non-commercial use, costs nothing, and comes with complete source code for all distributions.
In an article published on June 5, 2001, entitled “THE 10th Anniversary of PGP” [2], Zimmerman described his initial development of PGP:
One day in 1991, I sent the first version of PGP to a few of my friends so I could upload it to the Internet. I first sent it to Allan Hoeltje, who sent it to Peacenet, an ISP for grassroots political organizations, particularly the Peace Movement. Political activists around the world had access to Peacenet. I then uploaded it to Kelly Goen, who then forwarded the source code to a Usenet newsgroup that distributes it. At my request, he changed the Usenet permission to “U.S. Only.” Kelly also posted PGP on many BBS around the country. I can’t remember if it was June 5th or June 6th when I first posted it online. It’s scary to say, but back in 1991, I knew very little about Usenet newsgroups. I didn’t know the “US only” tag was just a “suggestion” and basically had no effect on the spread of the post. At the time, I thought the hashtag would control the spread of the post. I didn’t know how to post on a newsgroup, or even what a newsgroup was.
PGP spread on the Internet and gained many adherents around the world. PGP users and supporters also include dissidents in totalitarian states (several moving letters to Mr Zimmerman were published, some of which were included in evidence before the US Congress). Supporters of civil libertarianism in other parts of the world (see Mr Zimmerman’s evidence at various hearings) and “free communications” activists, who call themselves crypto enthusiasts, propagandize and distribute.
Ii. Quick start of GPG
The preparatory work
It is recommended that you first follow the tutorial and practice on any machine. Wait until you’re comfortable with it, then read the PGP key pair you’re really using:
The installation
🍎 Mac:
brew install gpg
Copy the code
🐧 Linux:
Each distribution generally has GnuPG installed by default.
🏁 Windows:
Download address
🌐 OpenBSD:
pkg_add gnupg
Copy the code
generate
Generate master key
# step 0
gpg --full-gen-key
# 'GPG --gen-key' is not recommended here
# step 1GPG (GnuPG) 2.2.20; Copyright (C) 2020 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) (14) Existing key from card Your selection?# default
# step 2
RSA keys may be between 1024 and 4096 bits long.
What keysize doyou want? (3072).The RSA key length should not be less than 2048 bits. Of course, the larger the input number, the safer the encryption and decryption speed will be slower
# step 3
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) 2y
I chose 2y because you can change your expiration date at any time before expiration to ensure that you still have control over the key
# step 4
Key expires at Wed 11 Jan 2023 05:50:53 PM CST
Is this correct? (y/N) y
# sure
# step 5
GnuPG needs to construct a user ID to identify your key.
Real name: linus Do not type your real name if you want to keep your privacy
Email address: [email protected]
Comment: Note can be left blank
If you are not going to use PGP to authenticate your Git record, you can enter your email address as you like. It does not need to be your email address, or even a real email address, as long as the recipient of your message knows it. Privacy leakage is a serious problem. Once you set it up and publish it to a public key server, it will never be deleted 😅
# step 6
You selected this USER-ID:
"linus <[email protected]>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
Enter o for confirmation
# step 7┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ do enter the passphrase to │ │ protect your new key │ │ │ │ Passphrase: ________________________________________ │ │ │ │ < OK > < Cancel > │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Enter a complex password and confirm
# step 8
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.
# Move your mouse randomly, the more random you are, the more secure your key will be
# Step 9 is done
gpg: key 99F583599B7E31F1 marked as ultimately trusted
gpg: revocation certificate stored as '/root/.gnupg/openpgp-revocs.d/705358AB85366CAB05C0220F99F583599B7E31F1.rev'
public and secret key created and signed.
pub rsa3072 2021-01-11 [SC]
705358AB85366CAB05C0220F99F583599B7E31F1 # your key ID
uid linus <[email protected]>
sub rsa3072 2021-01-11 [E] # This is the subkey automatically generated for encryption. E stands for Encrypt encryption
Copy the code
Here are some common abbreviations:
A => Authentication
C => Certify
E => Encrypt
S => Sign
? => Unknown capability
sec => Secret Key
ssb => Secret SuBkey
pub => Public Key
sub => Public Subkey
Copy the code
Generating subkey
You should use the subkey for daily use. Do not use the master key except to issue new subkeys.
You are advised to generate sub-keys independently for different environments and uses without interfering with each other.
# step 0
gpg --edit-key linus # or key ID
Step 1 Enter the GPG interactive interfaceGPG (GnuPG) 2.2.20; Copyright (C) 2020 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. sec rsa3072/99F583599B7E31F1 created: 2021-01-11 expires: never usage: SC trust: ultimate validity: ultimate ssb rsa3072/6FE9C71CFED44076 created: 2021-01-11 expires: never usage: E [ultimate] (1). linus <[email protected]>C# step 2
gpg> addkey
Please select what kind of key you want:
(3) DSA (sign only)
(4) RSA (sign only)
(5) Elgamal (encrypt only)
(6) RSA (encrypt only)
(14) Existing key from card
Your selection? 4
Select a subkey (sign only) based on your usage.
# select the same key as the master key
After the generation is complete
sec rsa3072/99F583599B7E31F1
created: 2021-01-11 expires: never usage: SC
trust: ultimate validity: ultimate
ssb rsa3072/6FE9C71CFED44076
created: 2021-01-11 expires: never usage: E
ssb rsa3072/FDB960B857D397F6
created: 2021-01-11 expires: never usage: S
[ultimate] (1). linus <[email protected]>
# last step
gpg> save # Remember to save, there is nothing to exit directly
Copy the code
Generating a revocation certificate
If you forget the password to your master key, or lose control of it (lost, taken away), there is no way to prove that you are no longer using it without revoking your credentials, other than notifying your friends one by one, which is a disaster.
# step 0
gpg --gen-revoke -ao revoke.pgp linus # uid or key ID
# step 1
sec rsa3072/99F583599B7E31F1 2021-01-11 linus <[email protected]>
Create a revocation certificate for this key? (y/N) y
Please select the reason for the revocation:
0 = No reason specified
1 = Key has been compromised
2 = Key is superseded
3 = Key is no longer used
Q = Cancel
(Probably you want to select 1 here) 3
# Follow the instructions to complete the process
Copy the code
The revoke. PGP generated is the revoke certificate. With this revoke certificate, you can invalidate a public key without a password, so keep it well, preferably with a duplicate of the master key.
List the key
List all public keys and subpublic keys
gpg --list-keys
# list all keys and subkeys
gpg --list-secret-keys
# simplify commands
gpg -k
gpg -K
# output
sec rsa3072 2021-01-11 [SC]
705358AB85366CAB05C0220F99F583599B7E31F1
uid [ultimate] linus <[email protected]>
ssb rsa3072 2021-01-11 [E]
ssb rsa3072 2021-01-11 [S]
Copy the code
This does not list the id of the subkey and prints no fingerprint information, which is not secure. So when you look at the key you should
- add
--keyid-format long
Output long ID - add
--fingerprint
Output Fingerprint Information
Such as
gpg --fingerprint -K --keyid-format long
# output
sec rsa3072/0x99F583599B7E31F1 2021-01-11 [SC] Long ID #
Key fingerprint = 7053 58AB 8536 6CAB 05C0 220F 99F5 8359 9B7E 31F1 # Fingerprint information
uid [ultimate] linus <[email protected]>
ssb rsa3072/0x6FE9C71CFED44076 2021-01-11 [E] The # slash is followed by the subkey ID
ssb rsa3072/0xFDB960B857D397F6 2021-01-11 [S]
Copy the code
Security Settings
–keyid-format long –fingerprint –fingerprint — vim ~/.gnupg/ GPG
# ~/.gnupg/gpg.conf
keyid-format 0xlong
with-fingerprint
Copy the code
The backup
gpg -ao public-key.txt --export linus Export public key
# Note that the last word is "!" Otherwise, all subkeys will be exported
gpg -ao secret-key --export-secret-key 99F583599B7E31F1! You are advised to replace secret-key with the backup file path of your encrypted device and import it directly into the device
gpg -ao sign-subkey --export-secret-subkeys FDB960B857D397F6! Private key [S]
gpg -ao encrypt-subkey --export-secret-subkeys 6FE9C71CFED44076! [E] [E] [E] [E] [E] [E] [E] [E
Don't forget to back up the undo credentials you just created as well
Copy the code
delete
After the backup, to clean the local key, first delete:
gpg --delete-secret-keys linus # delete private Key, UID can also be replaced by subkey ID, primary Key ID
gpg --delete-keys linus # delete public key
$HOME/.gnupg = $HOME/.gnupg = $HOME/.gnupg
Copy the code
Since gPG-generated private keys are stored in clear text on your disk, a single RM or right-click delete will not completely delete them. Use the wipe tool. If you’re using SSD and don’t have full encryption enabled, you can’t completely delete it.
Tails (Boum.org) distribution is especially recommended to generate the key for the main use. The system comes with tools such as PGP and Paper Key to ensure that the entire network is down, and the system restarts to erase all content, without the trouble of erasing the key.
The import
# Import from fileGPG --import [key file]The subkey file you just backed up, or someone else's public key
Importing from a public key server is not recommended. The usage is described in the public Key Server section
# Practice importing your own subkeys
# output
sec# rsa3072/0 x99f583599b7e31f1 / SC # 2021-01-11 SEC behind with # # show master key is not import, is safe
Key fingerprint = 7053 58AB 8536 6CAB 05C0 220F 99F5 8359 9B7E 31F1 # Fingerprint information
uid [unknown] linus <[email protected]>
ssb # rsa3072/0 x6fe9c71cfed44076 [E] # 2021-01-11 # # indicating the sub key has been imported
Copy the code
Signature and verification
Here only how to sign and verify other files, for other public key signature and verify in the public key release and exchange chapter explained.
The first way is to generate binary signature files
gpg --sign input.txt We can also add --output
The second way is to generate an ASCII signature
gpg --clearsign input.txt
# 3, separate the signature from the original text (the first two signature files contain all the original text, so the volume is relatively large)
gpg --armor --detach-sign input.txt # Armor generation will now be binary
Verify the signature file
gpg --verify demo.txt.asc demo.txt
Copy the code
encryption
# encryption:
# RecipientSpecifies the public key ID of the recipient
gpg --recipient {keyid/uid} --output encrypt.txt --encrypt input.txt
You can also add --armor, etc
# I prefer to use it
gpg -se -o encrypt.txt -r {keyid/uid} input.txt
# s stands for signature and e stands for encryption
# o is to output the result to the file encrypt.txt
# r is followed by the recipient's UID or key ID. The recipient's public key must have been imported
# input. TXT is the file you want to encrypt
# decryption:
gpg --decrypt encrypt.txt --output decrypt.txt
# can also
gpg -d encrypt.txt Output to the terminal for direct viewing
Copy the code
Publish and exchange
The exchange of public keys is the weak point of all asymmetric encryption algorithms. The so-called modern use mode is mainly reflected in the exchange and release of keys, which will be discussed separately later.
Do not publish your public key to a public key server until you have read and understood this series.
undo
Since PGP does not provide any means of notifying other users of revocation information, he cannot guarantee that no one will use a revoked key that has become insecure.
Your lost private key can still be used by attackers to decrypt encrypted messages sent by people who haven’t updated your public key. Revoke subkey and update public key. If someone encrypts information with the old public key, you can still decrypt it, but so can an attacker, which is extremely insecure.
For example, if A’s private key is stolen, she will issue A key Revocation certificate, but because the distribution of this key is informal and will take A lot of time and tongue, there is no guarantee that every user in the key ring who has A public key will receive it. Since A must sign the revoked certificate with her private key, if A also loses the private key, she cannot revoke it. Key revocation is considered to be the weakest link in the whole system.
Therefore, after you have revoked the key, please publish the revoked public key to the place where you always publish the public key, and notify others as much as possible.
Revoke master key
gpg --import gpg-linus.asc # Import your public key on a new computer
gpg: key 99F583599B7E31F1: "linus <[email protected]>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1
gpg --import revoke # import your backup revocation credentials, which will directly cause the key to be unavailable
gpg: key 99F583599B7E31F1: "linus <[email protected]>" revocation certificate imported
gpg: Total number processed: 1
gpg: new key revocations: 1
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 1 trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: depth: 1 valid: 1 signed: 0 trust: 1-, 0q, 0n, 0m, 0f, 0u
gpg: next trustdb check due at 2021-09-29
gpg -k Revoke the key
pub rsa3072 2021-01-11 [SC] [revoked: 2021-01-11]
705358AB85366CAB05C0220F99F583599B7E31F1
uid [ revoked] linus <[email protected]>
Copy the code
Revoke subkey
gpg --edit-key linus
gpg > list # List all your subkeys
gpg > key {n} # Select the number of the subkey you want to destroy
gpg > revkey
gpg > save Be sure to save before exiting, otherwise all changes will not take effect
Copy the code