π GPG: A Complete Guide to Key Generation, Encryption, and Signing
GPG (GNU Privacy Guard) is a powerful tool for creating cryptographic keys, encrypting and decrypting data, and digitally signing content. This tutorial will guide you through generating GPG keys, exporting and managing them, and using GPG to encrypt, decrypt, and sign messages.
π¦ Prerequisites
-
Linux (Debian/Ubuntu)
sudo apt updatesudo apt install gnupg -
MacOS
brew install gnupg -
Windows
Download and install:
- GPG (Gpg4win): https://gpg4win.org/
π Generating a GPG Key
To generate a GPG key, run the following command in your terminal:
gpg --full-generate-keyYouβll be prompted to answer a few questions:
- Key type: Choose
1for RSA and RSA. - Key size: Type
4096(for strong security). - Key expiration: Set it as you prefer (
0means it never expires). - Name and email: Provide your name and email (this can be any identity).
- Can use your Git identity (git config user.name, git config user.email).
- Passphrase: Set a secure passphrase to protect the key.
π Listing your GPG Keys
To list your private keys (those you own):
gpg --list-secret-keysTo list your public keys (those you or others can share):
gpg --list-keysShow your key fingerprint:
gpg --fingerprint <your_email_address or key_id>π Exporting your GPG Key
You can export your public key to share it with others or upload it to a key server.
gpg --armor --export <your_email_address or key_id> > public.keyβ οΈ Export and Backup Your Private Key (Careful!)
Only do this if you absolutely need to (e.g., for transferring to another device). Protect this file as if it were your master password.
gpg --armor --export-secret-keys <your_email_address or key_id> > private-key.ascTo re-import it later:
gpg --import private-key.ascπ₯ Creating a Revocation Certificate (If Compromised or Lost)
First, generate a revocation certificate (you should do this right after creating the key):
gpg --output revoke-cert.asc --gen-revoke <your_email_address or key_id>If you lose control of your key or forget the passphrase, you can use this file to revoke the key.
To apply the revocation:
gpg --import revoke-cert.ascThen, delete the key:
gpg --delete-secret-key <your_email_address or key_id>(Optional) Delete the public key as well:
gpg --delete-key <your_email_address or key_id>π Encrypt and Decrypt Messages
Encrypt a message:
gpg --output message.txt.asc --encrypt --armor --recipient <recipient_email_address> message.txtDecrypt a message:
gpg --decrypt --output message.txt < encrypted-message.txt.gpgβοΈ Signing and Verifying Messages
Sign a File (Creates ASCII-armored Signed File)
gpg --output message.txt.asc --sign --armor message.txtDetached Signature
gpg --output message.txt.asc --detach-sign --armor message.txtVerify a Signed File
gpg --verify message.txt.ascVerify a Detached Signature
gpg --verify message.sig message.txtClearsign a Message (inline)
gpg --output message.txt.asc --clearsign --armor message.txtβοΈ Uploading Your Public Key to a Keyserver
Uploading your public key allows others to find it and send you encrypted messages or verify your signatures.
# Using keys.openpgp.orggpg --send-keys --keyserver hkps://keys.openpgp.org <your_email_address or key_id>
# Or Ubuntuβs keyservergpg --send-keys --keyserver keyserver.ubuntu.com <your_email_address or key_id>π‘ Tips
- Always back up your GPG keys securely.
- Use a password manager to store your passphrase.
- Upload your public key to a keyserver if you want others to find and use it:
# Upload your key to a public keyservergpg --keyserver hkps://keys.openpgp.org --send-keys <your_key_id>
