Encryption Explained: AES, RSA, and When to Use Each
A practical guide to symmetric vs asymmetric encryption, key sizes, and choosing the right approach for your application.
Two Types of Encryption
All modern encryption falls into two categories:
Symmetric (AES)
Same key encrypts and decrypts. Fast. Used for bulk data.
decrypt(ciphertext, key) → data
Asymmetric (RSA)
Key pair: public encrypts, private decrypts. Slower. Used for key exchange.
decrypt(ciphertext, privateKey) → data
In practice, most systems use both: RSA to securely exchange an AES key, then AES for the actual data encryption.
AES: The Workhorse
AES (Advanced Encryption Standard) is the most widely used encryption algorithm. It's what protects your HTTPS connections, encrypted drives, and password vaults.
| Key Size | Security Level | Use Case |
|---|---|---|
| AES-128 | Secure until ~2030+ | General use, slightly faster |
| AES-192 | Rarely used | Middle ground, not common |
| AES-256 | Recommended | High security, future-proof |
const crypto = require('crypto');
// Generate a random 256-bit key
const key = crypto.randomBytes(32); // 32 bytes = 256 bits
const iv = crypto.randomBytes(16); // Initialization vector
// Encrypt
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
let encrypted = cipher.update('secret data', 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag();
// Decrypt
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');RSA: For Key Exchange and Signatures
RSA uses a key pair: share your public key with everyone, keep your private key secret. Anyone can encrypt a message with your public key, but only you can decrypt it.
RSA Key Sizes
- * 2048 bits - Minimum acceptable, equivalent to ~112-bit symmetric
- * 3072 bits - Recommended for 2030+
- * 4096 bits - High security, slower operations
RSA is too slow for encrypting large amounts of data. Instead, it's used to:
- * Key exchange - Securely send an AES key to someone
- * Digital signatures - Prove a message came from you
- * Authentication - SSH keys, code signing
Hybrid Encryption in Practice
Here's how HTTPS, email encryption, and most secure systems actually work:
This gives you the best of both worlds: RSA's secure key exchange and AES's speed for bulk encryption.
Quick Decision Guide
| Scenario | Use |
|---|---|
| Encrypting files on disk | AES-256-GCM |
| Encrypting database fields | AES-256-GCM |
| Secure communication between two parties | RSA + AES (hybrid) |
| Signing JWTs | RS256 (RSA) or HS256 (HMAC) |
| SSH authentication | Ed25519 or RSA-4096 |
| Password hashing | bcrypt or Argon2 (not AES/RSA!) |
Common Mistakes to Avoid
Using ECB Mode
ECB (Electronic Codebook) encrypts identical blocks identically, leaking patterns. Always use GCM, CBC with HMAC, or another authenticated mode.
Reusing IVs/Nonces
Each encryption operation needs a unique IV. Reusing them can completely break the encryption. Generate randomly or use a counter.
Rolling Your Own Crypto
Use well-tested libraries (libsodium, OpenSSL, Web Crypto API). Cryptography is full of subtle pitfalls that experts miss.