Installing and Using GnuPG for File Encryption and Decryption

Wheather you need to encrypt file data for your project depends on the sensitivity of the data and the potential risks of it being accessed by unauthorized users.

The tool I recommend for encryption and decryption is GnuPG.

GnuPG (GNU Privacy Guard) is a powerful tool for encrypting and decrypting data, making it a valuable asset for protecting sensitive information in your projects.
Here's a comprehensive guide on installing and using GnuPG for file encryption and decryption:

Installation GnuPG

For most Linux distributions, you can install GnuPG using your package manager. For example:

On Debian/Ubuntu: 

sudo apt-get update
sudo apt-get install gnupg

On Fedora: 

sudo dnf install gnupg

On macOS: 

brew install gnupg

Windows:

You can download and install Gpg4win from the official website: download

Generate a Key Pair

First, you need to generate a key pair (public and private keys).
Open a terminal and run the following command to generate a key pair:

gpg --full-generate-key

You will be prompted to choose the type of key you want, the key size, and the expiration date. A common choice is RSA and RSA (default), a key size of 2048 or 4096 bits, and an expiration date based on your needs. You'll also need to provide your name, email address, and a passphrase to protect your private key.

List Your Keys

To see a list of your generated keys, use:

gpg --list-keys

Note the key ID of the key you just created. It will look something like ABCDE12345.

Exporting the Public Key

You can share your public key with others so they can encrypt files they send to you.

gpg --export -a "[email protected]" > publickey.asc

Import the Public Key

After received public key you need import it before encrypt file:

gpg --import publickey.key

Encrypt a File

To encrypt a file with a public key use the following command:

gpg --recipient "[email protected]" --encrypt filename.txt

This will create a file named filename.txt.gpg.
or encrypt to custom name file:

gpg --output custom_filename.txt.gpg --encrypt --recipient [email protected] filename.txt

Replace [email protected] with the email address associated with your public key and file_to_encrypt.txt with the name of the file you want to encrypt. custom_filename.txt.gpg is the output encrypted file.

Decrypt the Received File

To decrypt a file with your private key:

gpg --output decryptedfile.txt --decrypt filename.txt.gpg

Automating with Scripts

For automation, you can create scripts to handle these commands.

Here’s an example script to encrypt a file:

#!/bin/bash
# Encrypt a file
recipient="[email protected]"
inputfile="file-to-encrypt.txt"
outputfile="${inputfile}.gpg"

gpg --recipient "$recipient" --encrypt "$inputfile"
echo "File encrypted to $outputfile"

And a script to decrypt a file:

#!/bin/bash
# Decrypt a file
inputfile="file-to-decrypt.txt.gpg"
outputfile="decryptedfile.txt"

gpg --output "$outputfile" --decrypt "$inputfile"
echo "File decrypted to $outputfile"

Make sure to make your scripts executable with command:

chmod +x encrypt.sh decrypt.sh

Now you have a basic setup to use GnuPG for encrypting and decrypting files.