This article is also posted on my blog, feel free refer to it for the latest revisions: gpg-101
GnuPG is a complete and free implementation of the OpenPGP standard as defined by RFC4880(also known as PGP). GnuPG allows you to encrypt and sign your data and communications; it features a versatile key management system, along with access modules for all kinds of public key directories. GnuPG, also known as GPG, is a command line tool with features for easy integration with other applications.
This article I will introduce the basic usage of GPG.
When it comes to GPG, you should know the PGP first. The PGP is a protocol that provides encryption and digital signature services(Pretty Good Privacy). The GPG is the implementation of the PGP protocol. PGP can support many kinds of encryption algorithms, such as AES, RSA, ECC, etc.
And you can add it to your Github to make every commit to your repository is signed.
Do not upload any message to the
public key servers
, it's not secure! Do not do any operation related topublic key servers
! In this article, I will not mention any operation related topublic key servers
! Thepublic key servers
is a centralized service, and will not delete any messages even you have revoked the public key.
A => Authentication (eg. ssh)
C => Certify (Only the primary key have this capability)
E => Encrypt
S => Sign (eg. sign the commit)
? => Unknown capability
sec => Secret Primary Key
ssb => Secret Subkey
pub => Public Primary Key
sub => Public Subkey
asc
pgp
Each key or subkey has a line of 10 groups of 4 characters. This is the SHA-1 hash of the entire key, which is 160 bits, 20 bytes, and is usually represented as 40 hexadecimal numbers. This fingerprint can be used to uniquely identify a key pair.
Format:
UID is the user id which contains the username, comment and email. Name (Comment) <Email>
When import a key, it will default to [unknown]
. You can check the fingerprint and the owner's claim to verify the key.
Trust levels:
ultimate
: Normally you should only ultimately trust your own keys. The root of the trust chain.full
: Full trust the key, also contains the keys signed by this key.marginal
: Trust the key, but not fully trust. If three people trust the key, then I trust it.never
: Never trust the key along with the keys signed by this key.brew install gpg
You have to do the following steps quickly. If the process is timeout, you will need to re-do the steps.
gpg --full-generate-key
# 1. Then select the key type, it's fine by default.
# 2. Then select the key expiration, I choose `3y`, because you can renew the key later, and that's make sure you cn still control the key. Expired keys are only invalid for new encryption and signing. But you can still decrypt and verify the existing information, just will be marked as expired.
# 3. Then enter you name(uid), I don't recommend you to use your real name, you can instead of your username.
# 4. Then enter your email, make sure to use the verified email in Github, it's highly recommended to use the `no-reply` email provided by Github to avoid spam.
# 5. Then enter the passphrase, it's used to encrypt the key.
# 6. After a rondom move, your key is generated.
It is recommended to generate a subkey for the key. And just use the primary key to sign the new subkeys.
Each subkey has its own application scenario.
# Enter the primary key interactive mode
gpg --edit-key yourNameInprimaryKey(uid or keyid)
gpg > addkey
# Then the process is the same as the previous steps. This time I choose the RSA(sign only).
# Btw, before generate the subkey, it will ask you to enter the passphrase of the primary key.
# After the subkey is generated, don't forget to save the key.
gpg > save
Imagine you forget the passphrase of the key, or you lose the control, you can use the revocation certificate to revoke the public key. If not, you will need to notify your friends that you don't use the key anymore. That will be a big problem. Thus it is necessary to generate a revocation certificate.
gpg --gen-revoke -ao revoke.pgp uid(or keyid)
# Make choices based on your situation
Then you will get a revoke.pgp
file, you can use it to revoke the key.
gpg --list-keys # list the public keys, you can also use `gpg -k`
gpg --list-secret-keys # list the secret keys, you can also use `gpg -K`
# The most common usage! -k or -K
gpg -K --with-fingerprint --with-subkey-fingerprint --keyid-format long
Besides, there are some parameters that you may need to use:
--with-fingerprint # print the fingerprint of the key
--with-subkey-fingerprint # print the fingerprint of the subkey
--with-sig-list # print the signature of keys
gpg -ao public-key.txt --export uid(or keyid) # export the public key
# It is better to add your secure path before the secret-key, it will export to the machine directly.
gpg -ao secret-key --export-secret-key primarykeyid! # export the primary secret key, remember to add the `!` to export the single key, or you will export the whole secret keys.
gpg -ao sign-subkey --export-secret-subkeys subkeyid! # export the sign sub secret key.[S]
gpg -ao encrypt-subkey --export-secret-subkeys encryptkeyid! # export the encrypt sub secret key.[E]
Besides, GPG private key exported as an ASCII armored version or its base64 encoding (often).
gpg --export-secret-key --armor keyid > secret-key.asc
After Export the keys, you can delete then from the machine.
gpg --delete-secret-keys uid(or keyid) # delete the secret key
gpg --delete-keys uid(or keyid) # delete the public key
As we know, the keys is stored in the machine in plaintext, it will not delete the keys completely, you can use the
wipe
or other tools to assist. But there is still the risk of restoring the keys. If you really want to generate and delete the keys in the most secure way, you can try Tails(boum.org).
Strongly discourage any operation related to
public key servers
!
gpg --import yourkeysfile(your secret key or others public key)
# output
# the `#` means the primary key is not imported, so it's safe.
# sec# rsa3072/keyid 2021-01-11 [SC]
# ...
# the `#` means the subkey is imported.
# ssb # rsa3072/keyid 2021-01-11 [E]
# Sign
# 1. generate the binary signature file
gpg --sign input.txt
# 2. generate the ASCII signature file
gpg --clearsign input.txt
# 3. generate the signature file and original file separately.
gpg --armor --detach-sign input.txt
# verify
gpg --verify input.txt.asc input.txt
# encrypt
# uid(or keyid) is the uid or keyid of the recipient which means you have to import the public key of the recipient in advance.
gpg --encrypt --recipient uid(or keyid) input.txt --output output.txt
# a simple version
gpg -se -o encrypt.txt -r uid(or keyid) input.txt
# decrypt
gpg --decrypt encrypt.txt --output decrypt.txt
Even you have revoke the key, if there is still someone sent you message by the outdated public key, you can decrypt the message as well as the hacker can. This operation import the revocation certificate which will make the whole keys invalid. The revoked key is only invalid for new encryption and signing. But you can still decrypt and verify the existing information, but it will be marked as revoked.
Imagine the scenario, Alice's secret key is leaked, she will send a key revocation certificate, but the distribution is not centralized, so she cannot make sure everyone has received the message. Besides, the key revocation certificate is need to be signed by the secret key of the Alice, so if the secret key is lost, she will not be able to revoke the key.
So once you have revoked the key, you should push the revoked public key to where you publish the key always, and notify your friends.
# revoke the primary key
# import the public key first
gpg --import gpg-linus.asc
# then import the revoke certificate which will make the public key invalid directly.
gpg --import revoke
# gpg -k to check the key is revoked. eg.[revoked: 2024-01-01]
# revoke the subkey
gpg --edit-key uid(or keyid)
# then select the subkey you want to revoke
gpg > list
gpg > key 1 # 1 is the index of the subkey
gpg > revoke
gpg > save
Refer to Github docs and Github docs
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。