GnuPG (GNU Privacy Guard) is a complete and free implementation of the OpenPGP standard that provides data and communication encryption, digital signatures, and key management.
What is GnuPG? GnuPG is an open source encryption tool that implements the OpenPGP standard (RFC 4880), allowing encryption and signing of data, as well as secure management of cryptographic keys.
Main Characteristics Encryption and Signatures Symmetric Encryption : AES, 3DES, Twofish, BlowfishAsymmetric Encryption : RSA, DSA, ECDSA, EdDSAHash Functions : SHA-1, SHA-256, SHA-512, RIPEMD160Compression : ZIP, ZLIB, BZIP2Key Management Generation : Key pair creationImport/Export : Key exchangeRevocation : Key revocationWeb of Trust : Trust networkStandards OpenPGP : RFC 4880S/MIME : Email encryptionX.509 : Digital certificatesRFC 3156 : MIME SecurityInstallation and Configuration Linux Installation 1
2
3
4
5
6
7
8
9
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install gnupg
# CentOS/RHEL
sudo yum install gnupg2
# Verify installation
gpg --version
Windows Installation 1
2
3
4
5
6
7
8
# Using Chocolatey
choco install gnupg
# Direct download
# https://www.gnupg.org/download/
# Verify installation
gpg --version
macOS Installation 1
2
3
4
5
# Using Homebrew
brew install gnupg
# Verify installation
gpg --version
Basic Usage Key Generation RSA Key 1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Generate RSA key pair
gpg --full-generate-key
# Generate with specific parameters
gpg --batch --full-generate-key <<EOF
Key-Type: RSA
Key-Length: 4096
Subkey-Type: RSA
Subkey-Length: 4096
Name-Real: John Doe
Name-Email: john@example.com
Expire-Date: 2y
Passphrase: my_secure_password
EOF
ECC Key 1
2
3
4
5
# Generate ECC key
gpg --expert --full-generate-key
# Select option 9 (ECC and ECC)
# Select curve (Curve 25519, P-256, P-384, P-521)
Key Management List Keys 1
2
3
4
5
6
7
8
# List public keys
gpg --list-keys
# List private keys
gpg --list-secret-keys
# List with details
gpg --list-keys --with-colons
Export/Import Keys 1
2
3
4
5
6
7
8
9
10
11
# Export public key
gpg --armor --export john@example.com > public.key
# Export private key
gpg --armor --export-secret-keys john@example.com > private.key
# Import key
gpg --import public.key
# Import private key
gpg --import private.key
Revoke Key 1
2
3
4
5
# Generate revocation certificate
gpg --output revoke.asc --gen-revoke john@example.com
# Apply revocation
gpg --import revoke.asc
Encryption and Decryption Symmetric Encryption 1
2
3
4
5
# Encrypt file symmetrically
gpg --symmetric --cipher-algo AES256 file.txt
# Decrypt file
gpg --decrypt file.txt.gpg > file.txt
Asymmetric Encryption 1
2
3
4
5
6
7
8
# Encrypt for specific recipient
gpg --encrypt --recipient john@example.com file.txt
# Encrypt and sign
gpg --encrypt --sign --recipient john@example.com file.txt
# Decrypt file
gpg --decrypt file.txt.gpg > file.txt
Digital Signatures Sign Document 1
2
3
4
5
6
7
8
# Sign file
gpg --sign file.txt
# Sign with separate output
gpg --detach-sign file.txt
# Sign and encrypt
gpg --sign --encrypt --recipient john@example.com file.txt
Verify Signature 1
2
3
4
5
# Verify signature
gpg --verify file.txt.sig file.txt
# Verify attached signature
gpg --verify file.txt.gpg
Advanced Configuration Configuration File 1
2
3
4
5
6
7
8
9
10
11
12
# Create configuration file
mkdir -p ~/.gnupg
cat > ~/.gnupg/gpg.conf <<EOF
# GnuPG configuration
default-key john@example.com
keyserver hkp://keys.openpgp.org
keyserver-options auto-key-retrieve
personal-cipher-preferences AES256 AES192 AES
personal-digest-preferences SHA512 SHA384 SHA256
cert-digest-algo SHA512
default-preference-list SHA512 SHA384 SHA256 AES256 AES192 AES
EOF
Agent Configuration 1
2
3
4
5
6
7
8
9
# Start GPG agent
gpg-agent --daemon
# Configure agent
cat > ~/.gnupg/gpg-agent.conf <<EOF
default-cache-ttl 600
max-cache-ttl 7200
pinentry-program /usr/bin/pinentry-gtk-2
EOF
Practical Applications Email Encryption Thunderbird with Enigmail 1
2
3
4
# Install Enigmail in Thunderbird
# Configure email account
# Generate key pair
# Configure automatic encryption
Mutt 1
2
3
4
5
# Configure Mutt with GPG
set crypt_use_gpgme= yes
set crypt_autosign= yes
set crypt_replysign= yes
set crypt_replyencrypt= yes
File Encryption Encryption Script 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
# Script to encrypt files
if [ $# -eq 0 ] ; then
echo "Usage: $0 <file>"
exit 1
fi
FILE= $1
RECIPIENT= "john@example.com"
# Encrypt file
gpg --encrypt --sign --recipient $RECIPIENT " $FILE"
echo "Encrypted file: ${ FILE} .gpg"
Decryption Script 1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# Script to decrypt files
if [ $# -eq 0 ] ; then
echo "Usage: $0 <file.gpg>"
exit 1
fi
FILE= $1
# Decrypt file
gpg --decrypt " $FILE" > " ${ FILE%.gpg} "
echo "Decrypted file: ${ FILE%.gpg} "
Secure Backup 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
# Encrypted backup script
BACKUP_DIR= "/home/user/backup"
ENCRYPTED_DIR= "/home/user/backup_encrypted"
RECIPIENT= "john@example.com"
# Create encrypted backup directory
mkdir -p " $ENCRYPTED_DIR"
# Encrypt backup files
for file in " $BACKUP_DIR" /*; do
if [ -f " $file" ] ; then
gpg --encrypt --recipient " $RECIPIENT" --output " $ENCRYPTED_DIR/ $( basename " $file" ) .gpg" " $file"
fi
done
echo "Encrypted backup completed"
Web of Trust Concepts Trust : Level of trust in a keyValidity : Key validitySignatures : Key signaturesCertification : Key certificationTrust Management 1
2
3
4
5
6
7
8
9
# Sign another person's key
gpg --sign-key john@example.com
# Verify key signatures
gpg --check-sigs john@example.com
# Edit trust
gpg --edit-key john@example.com
# Command: trust
Upload Key to Server 1
2
3
4
5
6
7
8
# Upload public key
gpg --send-keys --keyserver keys.openpgp.org 0x12345678
# Search key on server
gpg --search-keys john@example.com
# Receive updated key
gpg --recv-keys 0x12345678
Best Practices Security Strong Keys : Use keys of at least 2048 bitsPasswords : Secure and unique passwordsRotation : Rotate keys regularlyBackup : Backup private keysManagement Revocation : Maintain revocation certificatesExpiration : Set expiration datesIdentification : Verify identities before signingUpdate : Keep GnuPG updatedConfiguration Configuration : Secure configurationAgents : Use key agentsServers : Use trusted key serversLogs : Maintain operation logsTroubleshooting Common Problems Keys : Key problemsSignatures : Signature problemsEncryption : Encryption problemsAgents : Agent problems 1
2
3
4
5
6
7
8
9
10
11
# Verify configuration
gpg --version --verbose
# Verify keys
gpg --list-keys --with-colons
# Verify signatures
gpg --check-sigs
# Verify configuration
gpg --dump-options
OpenSSL - Complementary cryptographic libraryPKI - Infrastructure that complements GnuPGRSA - Algorithm implemented in GnuPGAES - Algorithm implemented in GnuPGECC - Algorithm implemented in GnuPGHash Functions - Algorithms implemented in GnuPGTLS/SSL - Protocol that complements GnuPGCISO - Role that oversees GnuPGGeneral Cybersecurity - Discipline that includes GnuPGSecurity Breaches - Incidents that affect GnuPGAttack Vectors - Attacks against GnuPGIncident Response - Process that includes GnuPGSIEM - System that monitors GnuPGSOAR - Automation that manages GnuPGEDR - Tool that protects GnuPGFirewall - Device that complements GnuPGVPN - Connection that can use GnuPGDashboards - Visualization of GnuPG metricsLogs - GnuPG operation logsReferences