Somewhere I read that sending unencrypted email is like sending postcards: anyone can potentially read them. This is not nice for privacy but becomes very dangerous when the content of the email or attached files contains secrets like passwords, access keys, etc. Anyone who can get hold of your email can also potentially access your systems.
For sending encrypted email I generally use Enigmail which is data encryption and decryption extension for the Thunderbird email client. I also used Mailvelope which is an add-on for Firefox and Chrome allowing to integrate encryption in webmail providers such as Gmail, Outlook, etc. These tools simplify the encryption/decryption process, especially if you are not familiar with it.
However, it has occurred to me to have to encrypt large files containing data dumps. The challenge with email extensions is that they don’t allow you to send email with such huge attachments. Plus, Mailvelope doesn’t allow to encrypt files larger than 25 MB. This is when knowing how to encrypt and decrypt a file via the command line comes in handy. You can easily upload a large encrypted file on an FTP server or cloud hosting service without worrying that the file will end in the wrong hands. As a bonus, an encrypted file is generally smaller than a non-encrypted file so the upload is also quicker.
The encryption process requires to first get the GPG public key from the person you want to send the encrypted file or email to. Once you have the recipient’s public key, you can encrypt a file with that key. You send the email or upload the file and then ask the recipient to decrypt it at their end using their GPG private key. I’m going to cover both processes. Note that this is also useful in order to encrypt the content of an email that you want to keep secret and send it as attachment in a non-encrypted email.
Generate GPG public and private keys
- Install gpg or gpg2 on Linux or MacOS. This is generally part of the standard packages, for example on Ubuntu:
Shell1sudo apt install gnupg2
- Generate a GPG key and follow the instructions. I recommend selecting RSA and RSA (default) as kind of key and 4096 as keysize of the key:
- You should now have two files in
.gnupg within your home directory (e.g.
12-- pubring.gpg: this is your public key-- secring.gpg: this is your private key
Verify your public key with:Shell1gpg2 --list-keys
Verify your private key with:Shell1gpg2 --list-secret-keys
Encrypt and decrypt files
You have received a public key from someone and you want to encrypt a file with their public key in order to transmit it securely. The file containing the public key will typically have an extension .gpg or .asc.
Import the public key (e.g.
someonekey.asc is the filename of the key):
Shell1gpg2 --import someonekey.asc
- Trust the public key (
email@example.com is the email associated with the key and should be shown as output of the import command):
Shell1gpg2 --edit-key firstname.lastname@example.org
You’ll get a prompt command>, type trust and select 5 = I trust ultimately. Type quit to exit.
- Encrypt the file with the public key of the user (replace the email address with the email address of the user associated to the public key):
Shell1gpg2 -e -r email@example.com mysecretdocument.txt
- This will generate an encrypted file
mysecretdocument.txt.gpg which is smaller than the original file. Transmit the encrypted file and tell the user to decrypt it at their end with the following command:
Shell1gpg2 -o mysecretdocument.txt -d mysecretdocument.txt.gpg
Stay safe and encrypt important emails and files!