Suppose you need to transfer a file/folder to someone without the risk of a third party intercepting it, either from an unsecured network or, for example, via a corporate e-mail program. In that case, it may be worth encrypting your document.
Several utilities, notably GnuPG
(https://gnupg.org/)and OpenSSL
(https://www.openssl.org/) are available on Mac/Linux to
do this.
While using OpenSSL to encrypt my documents, I encountered a recurring error when encrypting specific files (typically concerning files containing NUL bytes). I came across this topic, which explains why it’s better to use GnuPG to encrypt your data (I’m oversimplifying, I admit).
I present here two GnuPG-based encrypt and decrypt functions, running on Linux and macOS, that provide an easy-to-use command line solution for encrypting and decrypting files and folders, respectively.
❯ Installation
First and foremost, if the command has not been installed yet, we will start installing gnupg.
❯ On macOs
We will install gnupg using Homebrew. This package manager allows packages to be installed on macOS systems quickly and easily. More information about Homebrew is on its official website.
In case where brew
is not installed yet on your machine, let’s install it via the following command:
|
|
This install can take a moment, in particular because it will probably install a few additional Xcode dependencies.
Once the installation finished, to install gpg
:
|
|
❯ On Linux
gpg
is generally already installed. If this is not the case, you can install it via the package manager used by your
system (apt
, dnf
, yum
, pacman
, zypper
, …).
For apt
, the most popular package manager used in particular by the Debian-based operating systems like Debian itself,
Ubuntu and its variants, Linux-Mint, Raspbian, …
|
|
If for some reason, you want to install it by yourself, without using a package manager, or if you want to update it to a newer version, you can visit this page.
❯ Testing the installation
To test it, run the following command:
|
|
If gpg
is correctly installed, you should see its version, plus some extra information. For example, running this
command on my laptop gave me thee following result:
|
|
❯ Encryption alias
❯ In using gpg
Given a file named a_file
in input, the command to encrypt this file using the algorithm AES256 is the following:
|
|
This will display a screen, asking you to type a passphrase that will be necessary to decrypt this file
If the passphrase is not robust enough (a robust passphrase must contain at least 8 characters, with at least 1 digit
or special character), the same screen is displayed until you enter a robust enough passphrase or, depending on the
version of gpg
installed, a second screen is displayed, asking you confirmation for using this too weak passphrase:
For more information on the command gpg
, the available options, and the available algorithms for the encryption:
|
|
And for an abbreviated version, listing the most useful commands:
|
|
❯ A function wrapping the logic
Based on this command, below is a function that you can add directly on your .bashrc
, .bash_profile
, or any other
external document that you can include on one of them in doing a source of this
file (source ~/.file_containing_this_method.sh
):
|
|
❯ Usage
To encrypt a simple element (file or folder, named element_to_encrypt
):
|
|
It will ask you to give the element to encrypt. You can also give this element in parameter of the command line:
|
|
or as a value of the parameter --input
:
|
|
If the input that you provide is a folder, the function creates a zip archive before encrypting it (the command zip
is
required ; if zip
is not installed, the function displays a message asking to install it).
By default, the output is the name of the input suffixed by .gpg
. You can override in providing explicitly the name of
the output via the parameter --output
:
|
|
❯ Decryption alias
❯ In using gpg
Given an encrypted file named a_file
in input, the command to decrypt this file to a file named output_file
is the
following:
|
|
If this file has been encrypted locally, then the passphrase you give during its encryption is automatically used. Else, similar to the encryption command, a prompt asking for a passphrase is displayed.
❯ A function wrapping the logic
Based on this command, below is a function that you can add directly on your .bashrc
, .bash_profile
, or any other
external document that you can include on one of them in doing a source of this file:
|
|
❯ Usage
To decrypt an element:
|
|
This will display the path to the element to decrypt, and the path to the output element. You can also give the element in parameter of the command line:
|
|
Alternatively, you can give this element in value of the parameter --input
:
|
|
You can also give the path of the output element via the parameter --output
:
|
|
I’ve covered everything I wanted to say in this blog post. I hope you’ll find it useful!