Key Takeaways
- Use AES-256 for symmetric encryption — it's the industry standard
- Use RSA-2048 or higher (preferably 4096) for asymmetric encryption
- Never use MD5 or SHA-1 for security — use SHA-256 or SHA-3 instead
- Use bcrypt, scrypt, or Argon2 for password hashing — never plain SHA/MD5
- Always use authenticated encryption (AES-GCM) to prevent tampering
Test Your Knowledge
Latest from the Blog
Cryptography Quick Reference
Essential guide to encryption algorithms, hashing functions, and when to use each. Covers symmetric encryption (AES), asymmetric encryption (RSA, ECC), and secure hashing.
When to Use What
| Use Case | Algorithm | Why |
|---|---|---|
| Encrypting data at rest | AES-256-GCM | Fast, authenticated encryption |
| Encrypting files | AES-256-GCM or ChaCha20-Poly1305 | Symmetric, efficient for large data |
| Password storage | Argon2id, bcrypt, scrypt | Slow hash, memory-hard |
| Data integrity/checksums | SHA-256, SHA-3 | Fast, collision-resistant |
| Digital signatures | Ed25519, ECDSA, RSA-PSS | Non-repudiation, authenticity |
| Key exchange | X25519, ECDH, DH | Establish shared secret |
| TLS/HTTPS | TLS 1.3 (auto-negotiates) | Uses all of the above |
| Message authentication | HMAC-SHA256 | Verify integrity + authenticity |
| Random token generation | CSPRNG (secrets module) | Cryptographically secure random |
| JWT signing | RS256, ES256, EdDSA | Asymmetric for verification |
Symmetric Encryption
Same key for encryption and decryption. Fast, used for bulk data.
Algorithm Comparison
| Algorithm | Key Size | Status | Use Case |
|---|---|---|---|
| AES-256-GCM | 256 bits | Recommended | General purpose, authenticated |
| AES-128-GCM | 128 bits | Acceptable | Still secure, faster |
| ChaCha20-Poly1305 | 256 bits | Recommended | Mobile, no AES hardware |
| AES-CBC + HMAC | 256 bits | Acceptable | Legacy systems (use GCM if possible) |
| AES-ECB | Any | NEVER USE | Reveals patterns in data |
| 3DES | 168 bits | Deprecated | Legacy only, migrate away |
| DES, RC4, Blowfish | Various | Broken | Never use |
AES-256-GCM Example (Python)
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
def encrypt_aes_gcm(plaintext: bytes, key: bytes) -> tuple[bytes, bytes]:
"""Encrypt with AES-256-GCM. Returns (nonce, ciphertext)."""
nonce = os.urandom(12) # 96-bit nonce for GCM
aesgcm = AESGCM(key)
ciphertext = aesgcm.encrypt(nonce, plaintext, associated_data=None)
return nonce, ciphertext
def decrypt_aes_gcm(nonce: bytes, ciphertext: bytes, key: bytes) -> bytes:
"""Decrypt AES-256-GCM ciphertext."""
aesgcm = AESGCM(key)
return aesgcm.decrypt(nonce, ciphertext, associated_data=None)
# Usage
key = os.urandom(32) # 256-bit key
message = b"Secret message"
nonce, encrypted = encrypt_aes_gcm(message, key)
decrypted = decrypt_aes_gcm(nonce, encrypted, key)
AES-256-GCM Example (Node.js)
const crypto = require('crypto');
function encryptAesGcm(plaintext, key) {
const iv = crypto.randomBytes(12); // 96-bit IV for GCM
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
let encrypted = cipher.update(plaintext, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag();
return { iv: iv.toString('hex'), encrypted, authTag: authTag.toString('hex') };
}
function decryptAesGcm(encrypted, key, iv, authTag) {
const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(iv, 'hex'));
decipher.setAuthTag(Buffer.from(authTag, 'hex'));
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// Usage
const key = crypto.randomBytes(32); // 256-bit key
Asymmetric Encryption
Public key encrypts, private key decrypts. Used for key exchange and signatures.
Algorithm Comparison
| Algorithm | Key Size | Status | Use Case |
|---|---|---|---|
| RSA-OAEP | 2048+ bits | Acceptable | Encryption, legacy compatibility |
| RSA-PSS | 2048+ bits | Recommended | Digital signatures |
| ECDSA (P-256) | 256 bits | Recommended | Signatures, TLS |
| Ed25519 | 256 bits | Preferred | Signatures, SSH keys |
| X25519 | 256 bits | Preferred | Key exchange |
| ECDH (P-256) | 256 bits | Recommended | Key exchange |
| RSA PKCS#1 v1.5 | Any | Deprecated | Padding oracle vulnerable |
| DSA | Any | Deprecated | Use ECDSA or Ed25519 |
Equivalent Security Levels
| Security (bits) | Symmetric | RSA/DH | ECC |
|---|---|---|---|
| 80 (weak) | 80-bit | 1024-bit | 160-bit |
| 112 (legacy) | 112-bit | 2048-bit | 224-bit |
| 128 (standard) | 128-bit (AES-128) | 3072-bit | 256-bit (P-256) |
| 192 (high) | 192-bit | 7680-bit | 384-bit (P-384) |
| 256 (ultra) | 256-bit (AES-256) | 15360-bit | 512-bit (P-521) |
Ed25519 Signing Example (Python)
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
# Generate key pair
private_key = Ed25519PrivateKey.generate()
public_key = private_key.public_key()
# Sign message
message = b"Important document"
signature = private_key.sign(message)
# Verify signature
try:
public_key.verify(signature, message)
print("Signature valid")
except InvalidSignature:
print("Signature invalid")
# Export keys
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
Hashing Algorithms
One-way functions. Used for integrity, checksums, and password storage.
Hash Algorithm Comparison
| Algorithm | Output Size | Status | Use Case |
|---|---|---|---|
| SHA-256 | 256 bits | Recommended | General purpose, checksums |
| SHA-384 | 384 bits | Recommended | Higher security requirement |
| SHA-512 | 512 bits | Recommended | Faster on 64-bit systems |
| SHA-3 (256/512) | 256/512 bits | Recommended | Alternative to SHA-2 |
| BLAKE2b | Up to 512 bits | Recommended | Fast, secure alternative |
| BLAKE3 | 256 bits | Recommended | Very fast, parallelizable |
| SHA-1 | 160 bits | Deprecated | Collision attacks exist |
| MD5 | 128 bits | Broken | Non-security only (checksums) |
Password Hashing (Different!)
| Algorithm | Status | Parameters |
|---|---|---|
| Argon2id | Preferred | m=65536, t=3, p=4 |
| bcrypt | Excellent | cost=12+ |
| scrypt | Good | N=2^17, r=8, p=1 |
| PBKDF2-SHA256 | Acceptable | 600,000+ iterations |
Key Management Best Practices
Key Generation
# Python - Secure random key generation
import secrets
import os
# Generate random bytes (use for symmetric keys)
key_256 = secrets.token_bytes(32) # 256-bit key
key_128 = os.urandom(16) # 128-bit key
# Generate random tokens (for API keys, session tokens)
api_key = secrets.token_urlsafe(32) # URL-safe base64
hex_token = secrets.token_hex(32) # Hex string
# NEVER use random module for cryptography
import random # NOT cryptographically secure!
Key Storage Guidelines
- Never hardcode keys in source code
- Use HSM/KMS - AWS KMS, Azure Key Vault, HashiCorp Vault
- Encrypt keys at rest with master key
- Rotate keys regularly - annually minimum, immediately on compromise
- Use envelope encryption - encrypt data key with master key
- Separate environments - different keys for dev/staging/prod
- Audit key access - log all key operations
Envelope Encryption Pattern
# Envelope encryption with AWS KMS
import boto3
kms = boto3.client('kms')
# Generate data key (DEK)
response = kms.generate_data_key(
KeyId='alias/my-master-key',
KeySpec='AES_256'
)
plaintext_key = response['Plaintext'] # Use to encrypt data
encrypted_key = response['CiphertextBlob'] # Store with encrypted data
# To decrypt: first decrypt the DEK with KMS, then decrypt data
decrypted_key = kms.decrypt(
CiphertextBlob=encrypted_key
)['Plaintext']
TLS 1.3 Cipher Suites
Recommended Cipher Suites
# TLS 1.3 (automatic - these are the only options)
TLS_AES_256_GCM_SHA384
TLS_CHACHA20_POLY1305_SHA256
TLS_AES_128_GCM_SHA256
# TLS 1.2 (if 1.3 not available)
ECDHE-ECDSA-AES256-GCM-SHA384
ECDHE-RSA-AES256-GCM-SHA384
ECDHE-ECDSA-CHACHA20-POLY1305
ECDHE-RSA-CHACHA20-POLY1305
ECDHE-ECDSA-AES128-GCM-SHA256
ECDHE-RSA-AES128-GCM-SHA256
# Nginx configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
Cipher Suites to Avoid
- Anything with
NULL- No encryption - Anything with
EXPORT- Weak 40-bit keys - Anything with
DESor3DES- Broken/deprecated - Anything with
RC4- Broken - Anything with
MD5- Broken - Anything without
ECDHEorDHE- No forward secrecy - Anything with
CBCmode - Padding oracle vulnerabilities
Quick Decision Guide
| Question | Answer |
|---|---|
| What symmetric cipher? | AES-256-GCM |
| What asymmetric algorithm? | Ed25519 (signatures), X25519 (key exchange) |
| What hash function? | SHA-256 (general), Argon2id (passwords) |
| What RSA key size? | 3072+ bits (2048 minimum) |
| What ECC curve? | P-256 (NIST) or Curve25519 |
| What TLS version? | TLS 1.3 (1.2 minimum) |
| Random numbers? | secrets module (Python), crypto.randomBytes (Node) |
| Key rotation frequency? | Annually, or on compromise |
Related Resources
Need Detailed Cryptography Guides?
For comprehensive tutorials and implementation guides:
Visit FixTheVuln.com →FixTheVuln Store
Get the CompTIA Security+ Study Planner
Fillable PDF study planners with domain trackers, weekly schedules, and progress tracking. Available in Standard, ADHD-Friendly, Dark Mode, and 4-Format Bundle.
CompTIA Security+ Planner60+ certifications available — from $5.99
CyberFolio
Choosing your next cert? Track them all in one place.
Build a shareable cybersecurity portfolio that highlights your certifications, projects, and skills — free.
Build Your Portfolio →