I can never remember how to do various things with gpg, despite using it for encryption and signing git commits. This post is my documentation.
Subkey management (keeping secret key safe)
I like to keep my master key in a safe place and away from my laptop. For daily operations, I use subkeys instead.
1. Generate a New Key (If You Don't Have One)
gpg --full-generate-keyChoose RSA and RSA and set an expiration date.
2. Create Subkeys
gpg --edit-key KEY_ID
gpg> addkey # S for signing, E for encryption, A for authentication
gpg > save3. Export master & subkeys
gpg --export-secret-keys KEY_ID > mykey.sec.gpg
gpg --export-secret-subkeys KEY_ID > mykey.subkeys.gpg(add --armor for text output)
Then, move these files to a secure location and remove the master secret key.
gpg --delete-secret-keys KEY_ID
gpg --import mykey.subkeys.gpg # If you accidentally delete the subkey secretsTo verify that the master key is missing:
gpg --list-secret-keysThe master key should be marked with #.
Update expiring keys
If the master key is expiring, you will need to re-import the secret key from your backup. If the subkeys are expiring (and you still have their secrets), you can run the following:
gpg --edit-key KEY_ID
gpg > key 1
gpg > key 2
gpg > expire
gpg > ...
gpg > savePublish updates to keyservers:
gpg --send-keys YOUR_KEY_IDI also re-export the master and subkeys and move them to a secure location.
Encrypting data
To encrypt a file:
gpg --encrypt --recipient YOUR_EMAIL --output encrypted-file.gpg file.txtTo decrypt:
gpg --decrypt --output decrypted-file.txt encrypted-file.gpgFor symmetric encryption (password-based):
gpg --symmetric --cipher-algo AES256 --output encrypted-file.gpg file.txtSigning things
Sign a File (Detached Signature)
gpg --detach-sign --armor file.txtVerify a signature:
gpg --verify file.txt.asc file.txtSigning git commits:
gpg --list-secret-keys --keyid-format=long
git config --global user.signingkey YOUR_SUBKEY_ID
git config --global commit.gpgsign true # sign all commits by default
git commit -S -m 'Signed commit'Hopefully this helps me in the future, and maybe you too!