๐ AES Symmetric Encryption: One Key, Both Ways
Understand AES-256-GCM internals, PBKDF2 key derivation, and when to choose symmetric encryption
What Is Symmetric Encryption?
Symmetric encryption is like a single key that both locks and unlocks โ encryption and decryption use the same key. You encrypt data with a password, and the recipient must use the exact same password to decrypt it. This is fundamentally different from RSA asymmetric encryption, which uses two different keys (public and private).
AES (Advanced Encryption Standard) is the most widely used symmetric encryption algorithm in the world. Published by NIST in 2001, it has stood over two decades of cryptanalysis with no effective attacks discovered.
AES-256-GCM: Why This Combination?
AES-256 = 256-bit Key Length
AES comes in three key sizes: 128-bit, 192-bit, and 256-bit. Larger is more secure. A 256-bit key has 2ยฒโตโถ possible combinations โ a number larger than the estimated atoms in the observable universe. Even with a quantum computer running Grover's algorithm, AES-256 retains 128-bit equivalent security, which is still considered safe.
GCM = Galois/Counter Mode
AES itself is just a "block cipher" โ it can only encrypt 128 bits (16 bytes) at a time. To encrypt data of arbitrary length, you need a mode of operation. GCM is the most recommended mode today because it provides three properties in one:
- Confidentiality: Encrypted data cannot be read
- Integrity: If data is tampered with in transit, decryption automatically detects it
- Authentication: Confirms the ciphertext was created with the correct key
| Mode | Confidentiality | Integrity | Recommended? |
|---|---|---|---|
| ECB (Electronic Codebook) | โ Serious flaws | โ | Never use |
| CBC (Cipher Block Chaining) | โ | โ Needs HMAC | Legacy only |
| CTR (Counter) | โ | โ | Fair |
| GCM (Galois/Counter) | โ | โ Built-in | โ Recommended |
PBKDF2: Turning Passwords Into Keys
Your password might be something like "hello123" โ a short string. But AES-256 requires exactly 256 bits of key material. You can't use the password directly โ it's too short and not random enough. The solution is a Key Derivation Function (KDF).
PBKDF2 (Password-Based Key Derivation Function 2) transforms any-length passwords into fixed-length strong keys through:
- Salt: 16 random bytes, ensuring the same password produces a different key every time
- Iterations: 100,000 rounds of repeated hashing, dramatically increasing brute-force cost
- Hash function: SHA-256 as the underlying hash
// PBKDF2 key derivation
const keyMaterial = await crypto.subtle.importKey('raw', passwordBytes, 'PBKDF2', false, ['deriveKey']);
const aesKey = await crypto.subtle.deriveKey(
{ name: 'PBKDF2', salt: randomSalt, iterations: 100000, hash: 'SHA-256' },
keyMaterial,
{ name: 'AES-GCM', length: 256 },
false,
['encrypt', 'decrypt']
);
100,000 iterations means: even if an attacker obtains your ciphertext and salt, each password guess requires 100,000 SHA-256 computations. At 1 billion hashes per second, cracking an 8-character random password would still take centuries.
What Is the IV (Initialization Vector)?
The IV is 12 random bytes that ensure the same plaintext + same key produces different ciphertext every time. Without an IV, an attacker could see "this ciphertext matches the previous one, so the content must be the same" โ a potentially catastrophic information leak in military and business contexts.
In GCM mode, the IV is typically 12 bytes (96 bits). It must be randomly generated and unique per encryption. The IV does not need to be secret โ it's commonly stored or transmitted alongside the ciphertext.
When Should You Use AES Symmetric Encryption?
- ๐ Encrypting local files / databases: Fast, ideal for large data volumes
- ๐ฌ End-to-end encrypted chat: Signal and WhatsApp use AES to encrypt message content
- ๐ HTTPS data transfer: After TLS handshake establishes a shared key, the actual data stream uses AES
- ๐ Combined with RSA: Use RSA to encrypt the AES key, use AES to encrypt the actual content โ the best of both worlds
- ๐ฆ Config encryption: Encrypt API keys, database passwords, and other sensitive configuration
๐ Try AES encryption now
AES-256-GCM, PBKDF2 key derivation, Base64 output, all client-side
๐ Open AES Encryption ToolFAQ
Q: Will quantum computers break AES?
Grover's algorithm reduces brute-forcing a 256-bit key from 2ยฒโตโถ to 2ยนยฒโธ. But 2ยนยฒโธ (roughly 3.4ร10ยณโธ) is still an astronomically large number โ brute-forcing it is infeasible for the foreseeable future. AES-256 is considered "post-quantum secure." By contrast, RSA and ECC are not safe against quantum attacks.
Q: Why does GCM require a 12-byte IV?
GCM can accept IVs of any length, but 12 bytes (96 bits) is optimal. For other lengths, GCM internally performs an extra GHASH computation to pad the IV, adding computational overhead. NIST explicitly recommends 12-byte IVs.
Q: How many times can I reuse the same key?
AES-GCM can theoretically encrypt up to 2ยณยฒ messages (about 4.3 billion) under a single key, as long as the IV never repeats. In practice, rotate keys periodically. Crucially: if you ever reuse the same IV with the same key to encrypt two different messages, GCM's security collapses โ an attacker may recover the authentication key.