Guide · Encryption
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.
encrypt(data, key) → ciphertext
decrypt(ciphertext, key) → dataAsymmetric (RSA)
Key pair: public encrypts, private decrypts. Slower. Used for key exchange.
encrypt(data, publicKey) → ciphertext
decrypt(ciphertext, privateKey) → dataIn 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');Need a key right now? Generate an AES key — created locally, never transmitted.
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 bitsMinimum acceptable, equivalent to ~112-bit symmetric3072 bitsRecommended for 2030+4096 bitsHigh security, slower operationsRSA 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
Working with key pairs? Generate an RSA key pair — created locally, never transmitted.
Hybrid Encryption in Practice
Here's how HTTPS, email encryption, and most secure systems actually work:
- Alice generates a random AES key (session key)
- Alice encrypts the AES key with Bob's RSA public key
- Alice encrypts her message with the AES key
- Alice sends: [RSA-encrypted AES key] + [AES-encrypted message]
- Bob decrypts the AES key with his RSA private key
- Bob decrypts the message with the AES key
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!) |