GPG gives you two main ways to encrypt a file: protect it with a password, or encrypt it with someone’s public key. A password is handy when the file is for you, or when you can send the password through a separate secure channel. If the recipient has already given you a verified GPG public key, public-key encryption is usually the better fit.
The Short Version
Encrypt a file with a password:
gpg --symmetric document.pdfGPG will ask for a passphrase, then create document.pdf.gpg. To decrypt it:
gpg --output document.pdf --decrypt document.pdf.gpgEncrypt a file for someone who has a public key:
gpg --encrypt --recipient user@example.com document.pdfThe recipient uses their private key to decrypt the resulting file:
gpg --output document.pdf --decrypt document.pdf.gpgOne warning: Make sure you can decrypt the encrypted copy before you delete the original. If you lose the password or the required private key, you’ll usually lose access to the data for good.
What You’ll Need
- GPG installed on your computer
- The file you want to protect
- A strong passphrase for symmetric encryption, or the recipient’s verified public key
- A secure backup of your private key if you’re using public-key encryption
Installing GPG
Windows
Install Gpg4win from its official website. The default installation includes both the GPG command-line tools and Kleopatra, its graphical key manager. Once installation is finished, open PowerShell or Command Prompt and check the version:
gpg --versionmacOS
With Homebrew installed, run:
brew install gnupgUbuntu and Debian
sudo apt update
sudo apt install gnupgFedora
sudo dnf install gnupg2Encrypting a File with a Password
Symmetric encryption uses one passphrase for both encryption and decryption. There’s no GPG key pair to create, and no public key to exchange.
- Open a terminal in the folder where the file is stored.
- Run the symmetric encryption command.
- When prompted, enter a long passphrase that you don’t use anywhere else.
- Check that the new
.gpgfile exists.
gpg --symmetric financial-records.xlsxIf you want to set the encrypted file’s name yourself, use --output:
gpg --output protected-records.gpg --symmetric financial-records.xlsxNeed an ASCII-armored file for a text-oriented system? Add --armor:
gpg --armor --symmetric financial-records.xlsxThis will normally produce a file ending in .asc. ASCII armor makes the file larger, though, so ordinary binary .gpg output is generally a better choice for regular file storage.
Decrypting a Password-Protected File
Give GPG an output filename so it doesn’t write the decrypted data to the terminal:
gpg --output financial-records.xlsx --decrypt protected-records.gpgEnter the passphrase when GPG, or its pinentry window, asks for it. If something already exists at that output path, GPG may ask whether you want to overwrite it.
Encrypting a File with a Public Key
Public-key encryption works with two related keys. Anyone who has the public key can encrypt data for the owner. Decryption, however, requires the matching private key.
1. Create a Key Pair
If you don’t already have a key pair, start here:
gpg --full-generate-keyWork through the prompts to choose a supported algorithm, key size and expiration period, then provide an identity and passphrase. Setting an expiration date limits how long a forgotten or abandoned key remains useful. You can extend that date later.
2. Check the Available Keys
gpg --list-keysTo display a key fingerprint, which you’ll use to verify the owner’s identity, run:
gpg --fingerprint user@example.com3. Import the Recipient’s Public Key
gpg --import recipient-public-key.ascAfter importing it, compare the key’s complete fingerprint with the owner through a separate, trusted channel. Don’t treat the email address printed on the key as proof of ownership. It isn’t.
4. Encrypt the File
gpg --output confidential.zip.gpg --encrypt --recipient user@example.com confidential.zipIf more than one key matches that identity, use the complete fingerprint rather than the email address. You may also want the file to remain readable by you. In that case, add your own identity as a second recipient:
gpg --encrypt --recipient user@example.com --recipient me@example.com confidential.zipExporting Your Public Key
Other people need your public key if they’re going to encrypt files for you. Exporting it won’t expose your private key.
gpg --armor --export me@example.com > my-public-key.ascSend the resulting .asc file to the person encrypting the data. Then confirm your complete fingerprint through another trusted method, separately from the key file itself.
Handy GPG File Commands
| Task | Command |
|---|---|
| Encrypt with a password | gpg --symmetric file.txt |
| Encrypt for a recipient | gpg --encrypt --recipient user@example.com file.txt |
| Decrypt to a named file | gpg --output file.txt --decrypt file.txt.gpg |
| Import a public key | gpg --import public-key.asc |
| List public keys | gpg --list-keys |
| List private keys | gpg --list-secret-keys |
| Show a fingerprint | gpg --fingerprint user@example.com |
| Export a public key | gpg --armor --export user@example.com |
Mistakes That Cause Trouble
- Deleting the original too soon: Decrypt a test copy and inspect the restored file before removing anything.
- Sending the password with the attachment: Pass it along through a separate secure channel instead.
- Using a public key you haven’t verified: Compare the complete fingerprint directly with the key owner.
- Leaving yourself off the recipient list: With public-key encryption, the sender may be unable to read the encrypted file unless their own key is included.
- Putting passphrases in commands: Use GPG’s secure prompt. A passphrase typed into the command itself can end up in shell history or scripts.
- Expecting encryption to erase the source file: GPG makes an encrypted copy. It doesn’t securely delete the original.
Checking the Encrypted Copy
- Hold on to the original file for now.
- Decrypt the encrypted copy under a different output name.
- Open that restored file and make sure the contents are intact.
- For data that matters, compare cryptographic checksums for the original and decrypted files.
- Back up any private key you’ll need for future decryption.
Encryption keeps the contents confidential, but it’s no substitute for backups. Store encrypted backups somewhere separate, and record which key will be needed to recover them.
