Encrypt files using GnuPG (GPG)

Matrix code on laptop screen

GnuPG is a complete and free implementation of the OpenPGP standard encryption. 

Installing GPG

GPG is available in Windows and many distributions of Linux, including a graphical user interface (GUI) that is easy to use. However, I prefer using the command line tools instead.

Red Hat / CentOS

sudo yum install gnupg

Ubuntu / Debian

sudo apt-get install gnupg

Note: Use gpg2 (version 2.x)

Mac OS X

GPG is best installed on MacOS using Homebrew. Once Homebrew is installed, install GPG.

brew install gnupg

GPG version

Check the version of the command line tools you are running or have installed

gpg --version

GPG Keys

Creating GPG keys

Generate your own GPG key pair which will be used to encrypt and decrypt files.

gpg --gen-key

Exporting public keys

Your public key is the one that you can share to whom ever you will receive your message from. In other words, other other person will use your public key to encrypt their message when they send to you. You will be able to decrypt that message using your private key.

gpg --export --armor user@gmail.com > pubkey.asc

Exporting secret keys

Keep your private key in a safe place and never share it with anyone. 

gpg --export-secret-keys --armor user@gmail.com > secretkey.asc

Importing keys

You can import others' public key or another private key

gpg --import publickey.asc

Listing public keys

gpg --list-keys

Listing secret keys

gpg --list-secret-keys

Encrypting a file

gpg -r "Name" -e secretfile.txt

Encrypting multiple files

gpg -r "Name" --encrypt-files $(ls $DIR/secretfile*.txt)

Decrypt a file to disk

gpg secretfile.txt.gpg

Deleting public keys

gpg --delete-key [key-ID]

Deleting secret keys

gpg --delete-secret-key [key-ID]

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.