How to Encrypt and Decrypt Files with GPG (Gpg4win/Kleopatra) from the Command Line

I needed a PGP encryption process to transfer files securely. Here are the GPG command lines I use to encrypt and decrypt a file, which work with keys managed in Kleopatra (Gpg4win).

Prerequisite: Trust the Recipient’s Key

Sometimes GPG won’t encrypt to a key until you’ve marked it as trusted:

gpg --edit-key email@****.com
trust
5        (select 5 if you ultimately trust the key)
save

Encrypt a File

gpg -r (NameOfCert) -o (NewEncryptedFile.pgp) -e (FileToBeEncrypted.pdf)

Decrypt a File

gpg --batch --yes --passphrase (your passphrase) -o (UnencryptedOutput.xml) -d (EncryptedFileInput.pgp)
gpg: encrypted with 4096-bit RSA key, ID ********, created 2014-10-20
"Certificate Name, Description, Etc. <Email Address>"

The --batch --yes --passphrase options let you decrypt non-interactively, which is handy in scripts. Keep in mind that a passphrase on the command line can show up in shell history.