TL;DR
This guide shows you how to encrypt and decrypt messages using PGP (Pretty Good Privacy). It’s a way to keep your emails and files private. We’ll use the command line for this, as it gives you the most control.
Generating Key Pair
- Install GPG: If you don’t have it already, install GnuPG (GPG). On Debian/Ubuntu:
sudo apt update && sudo apt install gnupgOn macOS with Homebrew:
brew install gpg - Generate a Key: Run the following command. Replace “Your Name” and “your@email.com” with your details.
gpg --full-name "Your Name" --keytype rsa --keysize 4096 --email "your@email.com" - Set a Passphrase: You’ll be prompted to enter a strong passphrase. *Remember this!* It protects your private key.
GPG will then generate your key pair (public and private keys). This can take some time.
- List Your Keys: To see your generated keys:
gpg --list-keysThis will show you the Key ID, which you’ll need later. It looks something like
ABCDEF1234567890.
Encrypting a Message
- Export Public Key: Export your public key to a file:
gpg --armor --export "your@email.com" > public.key - Encrypt the File: To encrypt a file (e.g.,
message.txt) for someone, you need their public key.
Assuming you have their public key in a file calledrecipient_public.key:gpg --encrypt --recipient "Recipient's Email" --file message.txt --output encrypted.txtOr using the recipient’s Key ID:
gpg --encrypt --recipient ABCDEF1234567890 --file message.txt --output encrypted.txt - Send the Encrypted File: Send
encrypted.txtto the recipient securely (e.g., via email, but not plain text!).
Decrypting a Message
- Receive the Encrypted File: Obtain the encrypted file from the sender.
- Decrypt the File: Use your private key to decrypt it:
gpg --decrypt --file encrypted.txt --output decrypted.txt - Enter Passphrase: You’ll be prompted for the passphrase you set when generating your key.
- View Decrypted File: The decrypted content will be saved in
decrypted.txt.
Important Notes
- Key Security: Keep your private key *very* safe! Do not share it with anyone.
- Passphrase Strength: Use a strong, unique passphrase.
- Public Key Verification: Always verify the recipient’s public key before encrypting sensitive information to ensure you are communicating with the correct person. This is beyond the scope of this basic guide but crucial for cyber security.
- Revocation Certificate: Create a revocation certificate when generating your key, in case your private key is compromised.