Symmetric vs Asymmetric Encryption Explained
On this page
Encryption basics: two problems, two tools
Encryption keeps data confidential. The hard part isn't the math, which is solved; it's who has the keys and using the primitives correctly. Two families of algorithms solve different halves of the problem.
Symmetric encryption: AES and ChaCha20
A picture for the two families: symmetric encryption is a lockbox where both people have a copy of the same key. Asymmetric encryption is a mailbox with a slot: anyone can drop a letter in (the public key), but only the owner can open it (the private key).
One secret key encrypts and decrypts. AES is the standard; ChaCha20 is the common alternative, fast on devices without AES hardware.
- Fast: gigabytes per second with hardware support.
- Problem: both sides need the same key, and you must get it to them secretly.
Use an authenticated mode: AES-GCM or ChaCha20-Poly1305. They encrypt and also produce a tag that detects any change to the ciphertext. Older modes like CBC without a separate MAC, and especially ECB, are mistakes.
Authenticated encryption with AES-GCM in Node
Encrypting a bank account number with AES-256-GCM in Node:
const key = crypto.randomBytes(32);
function encrypt(plaintext, aad) {
const iv = crypto.randomBytes(12); // unique per message; never reuse with the same key
const c = crypto.createCipheriv('aes-256-gcm', key, iv);
c.setAAD(Buffer.from(aad)); // authenticated but not encrypted, e.g. a record ID
const ct = Buffer.concat([c.update(plaintext, 'utf8'), c.final()]);
return { iv, ct, tag: c.getAuthTag() };
}ciphertext: MGxeLG8v2yJb1MnQnMRxzsSMPq5B/0ccjMylFu149BQ=
decrypted : IBAN DE89 3704 0044 0532 0130 00
1 bit flipped in ciphertext -> Unsupported state or unable to authenticate data
moved to another record -> Unsupported state or unable to authenticate dataFlipping a single bit makes decryption fail rather than return altered data. The additional authenticated data (AAD), here customer:482, binds the ciphertext to its record: copying an encrypted value into another customer's row is detected too.
Rules: never reuse an IV (nonce) with the same key in GCM, which is catastrophic; let the library generate it, as above. Why catastrophic: GCM turns the key and nonce into a stream of random-looking bytes and combines it with the message. Two messages encrypted with the same key and nonce use the same stream, so combining the two ciphertexts cancels it out and leaves the two plaintexts mixed together, which is often enough to read both. Reuse also lets an attacker forge valid tags.
Asymmetric encryption: RSA and elliptic curves
A key pair: a public key anyone can have, a private key only the owner keeps. Data encrypted with the public key can only be decrypted with the private key. Algorithms: RSA, and elliptic-curve schemes like X25519 for key agreement and Ed25519 for signatures (Day 17).
- Solves key distribution: publish the public key; no shared secret needed in advance.
- Slow, and limited in how much data it can handle directly.
Hybrid encryption: how TLS combines both
TLS, PGP, Signal and cloud key management all do the same thing: use asymmetric cryptography to agree on or wrap a random symmetric key, then encrypt the bulk data with that symmetric key. In TLS 1.3, both sides use ephemeral X25519 key agreement to derive session keys, which also gives forward secrecy (Day 18). In envelope encryption (Day 19), a key management service protects small data keys that encrypt your data.
Cryptography rules for developers
- Don't build your own crypto or combine primitives yourself. Use high-level libraries: libsodium, Google Tink, your platform's AEAD APIs, or managed services (KMS).
- Keys from secure randomness, never from passwords directly (use a KDF such as Argon2 or HKDF).
- Current algorithms: AES-GCM, ChaCha20-Poly1305, X25519, Ed25519, RSA 3072+ with OAEP for encryption. Avoid DES, 3DES, RC4, MD5, SHA-1 and ECB mode.
- Plan for change ("crypto agility"): record algorithm and key versions with ciphertexts. NIST published post-quantum standards in 2024 (ML-KEM, ML-DSA), and TLS libraries already offer hybrid post-quantum key agreement.
Check yourself
GCM is authenticated encryption: it detects any modification of the ciphertext, while CBC alone doesn't and is open to tampering attacks.
They use asymmetric cryptography during the handshake to authenticate the server and agree on a random symmetric session key, then encrypt all data with that key using a fast cipher like AES-GCM. This is called hybrid encryption.
Decryption failed with an authentication error instead of returning modified plaintext. GCM's authentication tag covers the ciphertext, so any change is detected before the application sees any data.
Two messages would be encrypted with the same keystream, so their ciphertexts can be combined to reveal the plaintexts, and tags can be forged.
Subtle mistakes in padding, timing, nonce handling or randomness break security without any visible error; the code still appears to work. Use well-reviewed libraries with high-level APIs, like libsodium or the platform's crypto module.