Security

How to Encrypt a File with GPG (Password or Public Key)

To encrypt a file with a password, run gpg -c report.txt; GPG asks for a passphrase and writes report.txt.gpg, encrypted with AES-256 by default. To encrypt a file for a specific person, import their public key and run gpg -e -r [email protected] report.txt; only their private key can decrypt it.

Decrypt either kind with gpg -o report.txt -d report.txt.gpg. All commands and output below were run with GnuPG 2.4.5, in a throwaway keyring, with the example identity Alex Example <[email protected]>.

Install GPG

  • Linux: usually installed already; check with gpg --version. Otherwise install the gnupg package.
  • Windows: Git for Windows includes gpg in Git Bash. For a standalone install with a GUI, use Gpg4win from gpg4win.org.
  • macOS: install GnuPG with a package manager such as Homebrew (brew install gnupg).
$ gpg --version
gpg (GnuPG) 2.4.5-unknown
libgcrypt 1.9.4-unknown

Option 1: encrypt a file with a passphrase (gpg -c)

gpg -c report.txt

-c is short for --symmetric, which the GnuPG manual describes as "Encrypt with a symmetric cipher using a passphrase". GPG opens a passphrase prompt (in the terminal or a small Pinentry window), asks for it twice, and writes report.txt.gpg next to the original. The original file is not deleted.

The default cipher is AES-256; you can see it when decrypting. Example output:

$ gpg -o report.txt -d report.txt.gpg
gpg: AES256.CFB encrypted data
gpg: encrypted with 1 passphrase

Use this when you are protecting your own files, or when you can give the passphrase to the other person through a separate channel (a phone call, not the same email as the file). The file is only as strong as the passphrase: a random passphrase of several words, or one from a password generator, is what makes it safe. See password entropy explained for how long is long enough.

A wrong passphrase doesn't print "wrong password". It looks like this:

gpg: AES256.CFB encrypted data
gpg: encrypted with 1 passphrase
gpg: decryption failed: Bad session key

Option 2: encrypt a file for someone's public key (gpg -e)

Public-key encryption avoids sharing a secret at all: you encrypt with the recipient's public key, and only their private key can decrypt. See public-key cryptography explained for why this works.

1. Get the recipient's public key

The recipient exports it and sends it to you:

gpg --armor --export [email protected] > alex.asc

You import it:

gpg --import alex.asc

Before trusting it, compare its fingerprint with the recipient over another channel: run gpg --fingerprint [email protected] and check that both of you see the same 40 hexadecimal characters (printed in groups of four, such as 544B 312D 848B AF37 1C56 E79F CFED 0104 E2A2 BA7D). Anyone can create a key with any name and email address on it.

2. Encrypt

gpg -e -r [email protected] report.txt

-e is --encrypt ("Encrypt data to one or more public keys") and -r is --recipient. The output is again report.txt.gpg. To send to several people, repeat -r; each recipient can decrypt with their own key.

Add yourself as a recipient too (-r with your own email address), or you won't be able to open the file you just encrypted: it is encrypted to the recipients' keys only.

If GPG can't find a key for the address, it stops. Example output with lookups limited to the local keyring:

gpg: [email protected]: skipped: No public key
gpg: report.txt: encryption failed: No public key

Import the key first, or check the spelling of the address with gpg --list-keys.

3. The recipient decrypts

$ gpg -o report.txt -d report.txt.gpg
gpg: encrypted with cv25519 key, ID 217BC2DC8C943BDF, created 2026-10-26
      "Alex Example <[email protected]>"

GPG finds the matching private key automatically and asks for that key's passphrase, if it has one.

Create your own key pair

If you need to receive encrypted files, generate a key:

gpg --quick-generate-key "Alex Example <[email protected]>" default default 2y

The manual describes --quick-generate-key as "a simple command to generate a standard key with one user id". Passing default for the algorithm and usage creates a primary key plus an encryption subkey; the last argument sets the expiry (here two years; never disables it). For more control, use gpg --full-generate-key, which asks about every option. With GnuPG 2.4.5 the defaults produced this:

pub   ed25519 2026-10-26 [SC] [expires: 2028-10-25]
sub   cv25519 2026-10-26 [E]

That is an Ed25519 primary key for signing and certifying ([SC]) and a Curve25519 subkey for encryption ([E]).

GPG also writes a revocation certificate into openpgp-revocs.d inside its home directory. Keep a copy somewhere safe: it is how you tell others to stop using the key if you lose it.

Useful options

OptionWhat it does
-o file / --outputWrite to a named file. Without it, -d prints the decrypted data to the terminal.
-a / --armorText output (-----BEGIN PGP MESSAGE-----) instead of binary; file ends in .asc. Use it to paste into email or chat.
-s with -eSign and encrypt, so the recipient can verify who sent it: gpg -se -r [email protected] report.txt
-R / --hidden-recipientEncrypt without writing the recipient's key ID into the file
--encrypt-filesEncrypt several files in one command
--no-symkey-cacheDon't cache the passphrase used with -c

When a signed file is decrypted, GPG verifies the signature at the same time. Example output (trimmed):

gpg: using EDDSA key 544B312D848BAF371C56E79FCFED0104E2A2BA7D
gpg: Good signature from "Alex Example <[email protected]>" [ultimate]

Why decrypting didn't ask for a passphrase

GPG hands passphrases to gpg-agent, which caches them for a while. The manual notes that a passphrase used with --symmetric is cached unless you pass --no-symkey-cache. So if you encrypt a file and decrypt it a minute later, it may open with no prompt, which can make you think the encryption didn't work. To test properly, clear the cache first:

gpgconf --kill gpg-agent

Encrypting folders and checking integrity

GPG works on single files, so archive a folder first: tar czf project.tar.gz project/ then gpg -c project.tar.gz. If you publish the encrypted file for download, give people a SHA-256 checksum so they can confirm the download is complete; the larely checksum tool computes one in the browser.

If decryption still fails

  • decryption failed: No secret key: the file was encrypted to a key you don't have. The line above it names the key, for example gpg: encrypted with ECDH key, ID 217BC2DC8C943BDF. Compare that ID with gpg --list-secret-keys --keyid-format long, or ask the sender which key they used.
  • decryption failed: Bad session key on a -c file: wrong passphrase. Check the keyboard layout and Caps Lock.
  • no valid OpenPGP data found: the file isn't GPG-encrypted, or it was damaged in transit, for example an armored message reformatted by an email client.

Frequently asked questions

What is the difference between gpg -c and gpg -e?
gpg -c (--symmetric) encrypts with a passphrase: anyone who knows the passphrase can decrypt, and no keys are involved. gpg -e (--encrypt) encrypts to one or more public keys: only the holders of the matching private keys can decrypt, and no shared secret has to be sent to them.
Why did gpg decrypt my file without asking for the passphrase?
gpg-agent caches passphrases for a while after you use them, including the passphrase from a gpg -c encryption. That is why decrypting right after encrypting often doesn't prompt. Add --no-symkey-cache when encrypting to skip the cache, or run gpgconf --kill gpg-agent to clear it.
How do I encrypt a whole folder with GPG?
GPG encrypts single files, so archive the folder first and encrypt the archive: tar czf project.tar.gz project/ followed by gpg -c project.tar.gz. On Windows, create a .zip first and encrypt that.
Does gpg delete the original file after encrypting?
No. gpg writes a new file such as report.txt.gpg and leaves report.txt untouched. Delete the original yourself if you only want the encrypted copy, keeping in mind that normal deletion doesn't securely erase data.

Sources