GnuPG (GNU Privacy Guard) es una implementación completa y gratuita del estándar OpenPGP que proporciona cifrado de datos y comunicaciones, firmas digitales y gestión de claves.

¿Qué es GnuPG?

GnuPG es una herramienta de cifrado de código abierto que implementa el estándar OpenPGP (RFC 4880), permitiendo cifrar y firmar datos, así como gestionar claves criptográficas de forma segura.

Características Principales

Cifrado y Firmas

  • Cifrado Simétrico: AES, 3DES, Twofish, Blowfish
  • Cifrado Asimétrico: RSA, DSA, ECDSA, EdDSA
  • Funciones Hash: SHA-1, SHA-256, SHA-512, RIPEMD160
  • Compresión: ZIP, ZLIB, BZIP2

Gestión de Claves

  • Generación: Creación de pares de claves
  • Importación/Exportación: Intercambio de claves
  • Revocación: Revocación de claves
  • Web of Trust: Red de confianza

Estándares

  • OpenPGP: RFC 4880
  • S/MIME: Cifrado de correo
  • X.509: Certificados digitales
  • RFC 3156: MIME Security

Instalación y Configuración

Instalación en Linux

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

# Verificar instalación
gpg --version

Instalación en Windows

1
2
3
4
5
6
7
8
# Usando Chocolatey
choco install gnupg

# Descarga directa
# https://www.gnupg.org/download/

# Verificar instalación
gpg --version

Instalación en macOS

1
2
3
4
5
# Usando Homebrew
brew install gnupg

# Verificar instalación
gpg --version

Uso Básico

Generación de Claves

Clave RSA

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Generar par de claves RSA
gpg --full-generate-key

# Generar con parámetros específicos
gpg --batch --full-generate-key <<EOF
Key-Type: RSA
Key-Length: 4096
Subkey-Type: RSA
Subkey-Length: 4096
Name-Real: Juan Pérez
Name-Email: juan@example.com
Expire-Date: 2y
Passphrase: mi_contraseña_segura
EOF

Clave ECC

1
2
3
4
5
# Generar clave ECC
gpg --expert --full-generate-key

# Seleccionar opción 9 (ECC and ECC)
# Seleccionar curva (Curve 25519, P-256, P-384, P-521)

Gestión de Claves

Listar Claves

1
2
3
4
5
6
7
8
# Listar claves públicas
gpg --list-keys

# Listar claves privadas
gpg --list-secret-keys

# Listar con detalles
gpg --list-keys --with-colons

Exportar/Importar Claves

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Exportar clave pública
gpg --armor --export juan@example.com > public.key

# Exportar clave privada
gpg --armor --export-secret-keys juan@example.com > private.key

# Importar clave
gpg --import public.key

# Importar clave privada
gpg --import private.key

Revocar Clave

1
2
3
4
5
# Generar certificado de revocación
gpg --output revoke.asc --gen-revoke juan@example.com

# Aplicar revocación
gpg --import revoke.asc

Cifrado y Descifrado

Cifrado Simétrico

1
2
3
4
5
# Cifrar archivo simétricamente
gpg --symmetric --cipher-algo AES256 archivo.txt

# Descifrar archivo
gpg --decrypt archivo.txt.gpg > archivo.txt

Cifrado Asimétrico

1
2
3
4
5
6
7
8
# Cifrar para destinatario específico
gpg --encrypt --recipient juan@example.com archivo.txt

# Cifrar y firmar
gpg --encrypt --sign --recipient juan@example.com archivo.txt

# Descifrar archivo
gpg --decrypt archivo.txt.gpg > archivo.txt

Firmas Digitales

Firmar Documento

1
2
3
4
5
6
7
8
# Firmar archivo
gpg --sign archivo.txt

# Firmar con salida separada
gpg --detach-sign archivo.txt

# Firmar y cifrar
gpg --sign --encrypt --recipient juan@example.com archivo.txt

Verificar Firma

1
2
3
4
5
# Verificar firma
gpg --verify archivo.txt.sig archivo.txt

# Verificar firma adjunta
gpg --verify archivo.txt.gpg

Configuración Avanzada

Archivo de Configuración

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Crear archivo de configuración
mkdir -p ~/.gnupg
cat > ~/.gnupg/gpg.conf <<EOF
# Configuración GnuPG
default-key juan@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

Configuración de Agentes

1
2
3
4
5
6
7
8
9
# Iniciar agente GPG
gpg-agent --daemon

# Configurar agente
cat > ~/.gnupg/gpg-agent.conf <<EOF
default-cache-ttl 600
max-cache-ttl 7200
pinentry-program /usr/bin/pinentry-gtk-2
EOF

Aplicaciones Prácticas

Cifrado de Correo Electrónico

Thunderbird con Enigmail

1
2
3
4
# Instalar Enigmail en Thunderbird
# Configurar cuenta de correo
# Generar par de claves
# Configurar cifrado automático

Mutt

1
2
3
4
5
# Configurar Mutt con GPG
set crypt_use_gpgme=yes
set crypt_autosign=yes
set crypt_replysign=yes
set crypt_replyencrypt=yes

Cifrado de Archivos

Script de Cifrado

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/bash
# Script para cifrar archivos

if [ $# -eq 0 ]; then
    echo "Uso: $0 <archivo>"
    exit 1
fi

ARCHIVO=$1
DESTINATARIO="juan@example.com"

# Cifrar archivo
gpg --encrypt --sign --recipient $DESTINATARIO "$ARCHIVO"

echo "Archivo cifrado: ${ARCHIVO}.gpg"

Script de Descifrado

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash
# Script para descifrar archivos

if [ $# -eq 0 ]; then
    echo "Uso: $0 <archivo.gpg>"
    exit 1
fi

ARCHIVO=$1

# Descifrar archivo
gpg --decrypt "$ARCHIVO" > "${ARCHIVO%.gpg}"

echo "Archivo descifrado: ${ARCHIVO%.gpg}"

Backup Seguro

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/bash
# Script de backup cifrado

BACKUP_DIR="/home/usuario/backup"
ENCRYPTED_DIR="/home/usuario/backup_encrypted"
RECIPIENT="juan@example.com"

# Crear directorio de backup cifrado
mkdir -p "$ENCRYPTED_DIR"

# Cifrar archivos de backup
for file in "$BACKUP_DIR"/*; do
    if [ -f "$file" ]; then
        gpg --encrypt --recipient "$RECIPIENT" --output "$ENCRYPTED_DIR/$(basename "$file").gpg" "$file"
    fi
done

echo "Backup cifrado completado"

Web of Trust

Conceptos

  • Trust: Nivel de confianza en una clave
  • Validity: Validez de una clave
  • Signatures: Firmas de claves
  • Certification: Certificación de claves

Gestión de Confianza

1
2
3
4
5
6
7
8
9
# Firmar clave de otra persona
gpg --sign-key juan@example.com

# Verificar firmas de clave
gpg --check-sigs juan@example.com

# Editar confianza
gpg --edit-key juan@example.com
# Comando: trust

Subir Clave a Servidor

1
2
3
4
5
6
7
8
# Subir clave pública
gpg --send-keys --keyserver keys.openpgp.org 0x12345678

# Buscar clave en servidor
gpg --search-keys juan@example.com

# Recibir clave actualizada
gpg --recv-keys 0x12345678

Mejores Prácticas

Seguridad

  • Claves Fuertes: Usar claves de al menos 2048 bits
  • Contraseñas: Contraseñas seguras y únicas
  • Rotación: Rotar claves regularmente
  • Backup: Respaldar claves privadas

Gestión

  • Revocación: Mantener certificados de revocación
  • Expiración: Establecer fechas de expiración
  • Identificación: Verificar identidades antes de firmar
  • Actualización: Mantener GnuPG actualizado

Configuración

  • Configuración: Configuración segura
  • Agentes: Usar agentes de claves
  • Servidores: Usar servidores de claves confiables
  • Logs: Mantener logs de operaciones

Troubleshooting

Problemas Comunes

  • Claves: Problemas con claves
  • Firmas: Problemas con firmas
  • Cifrado: Problemas con cifrado
  • Agentes: Problemas con agentes

Herramientas de Diagnóstico

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Verificar configuración
gpg --version --verbose

# Verificar claves
gpg --list-keys --with-colons

# Verificar firmas
gpg --check-sigs

# Verificar configuración
gpg --dump-options

Conceptos Relacionados

Referencias