How Encryption Works

Intermediate
8 min read· Security

Encryption transforms readable data (plaintext) into unreadable ciphertext using a mathematical key. Only someone with the correct key can decrypt it back. Modern systems use two complementary types: symmetric encryption (one key encrypts and decrypts — fast, ideal for bulk data) and asymmetric encryption (a public key encrypts, a private key decrypts — slower, but solves the key distribution problem). TLS, HTTPS, end-to-end messaging, and disk encryption all combine these two types together.

Think of it like a lockbox system

Symmetric encryption is a lockbox with one key — whoever has the key can lock or unlock it. Fast and simple, but how do you securely hand someone the key the first time? Asymmetric encryption solves this: you publish an open padlock (public key) that anyone can use to lock a box for you. Only you have the key (private key) to open it. Nobody who locks a box for you can unlock it themselves. This is how two strangers on the internet securely exchange a symmetric key for the first time.

Step by Step

1 / 6

Key Concepts

AES (Advanced Encryption Standard)

The world's most widely used symmetric cipher. AES-128, AES-192, and AES-256 refer to key sizes. AES-GCM (Galois/Counter Mode) is the recommended mode — it provides both encryption and authentication (ensures ciphertext hasn't been tampered with). Used in TLS 1.3, disk encryption (FileVault, BitLocker), and password managers.

RSA

The most widely known asymmetric algorithm. Security based on the difficulty of factoring the product of two large primes. RSA-2048 or RSA-4096 are common key sizes. Used in TLS certificates, SSH key pairs, and code signing. Being replaced in many applications by ECC (smaller keys, faster operations, same security).

Elliptic Curve Cryptography (ECC)

Asymmetric cryptography based on the mathematics of elliptic curves. ECDSA and ECDH are common algorithms. A 256-bit ECC key provides security equivalent to a 3072-bit RSA key. Smaller keys mean faster handshakes and less bandwidth — why TLS 1.3 and modern SSH default to ECC.

End-to-End Encryption (E2EE)

Only the communicating endpoints can decrypt messages. Not even the service provider (WhatsApp, Signal) can read them. Implemented using asymmetric key exchange (users exchange public keys) and symmetric encryption for messages. The Signal Protocol (used by WhatsApp, Signal, iMessage) adds forward secrecy: each message uses a different key, so compromising one key doesn't expose past messages.

Forward Secrecy (Perfect Forward Secrecy)

A property where session keys are not derived from long-term private keys. ECDH generates a new ephemeral key pair per session. Even if the server's long-term private key is stolen years later, past sessions cannot be decrypted — each session's key material is discarded after use. TLS 1.3 mandates forward secrecy.

Hashing vs Encryption

Encryption is reversible (with the key). Hashing is a one-way function — you cannot reverse it. Use encryption when you need to retrieve the original data. Use hashing when you only need to verify data (passwords, integrity checks). Never store passwords as encrypted values (can be decrypted if the key leaks) — always use a password hashing function (bcrypt, Argon2).

Bcrypt / Argon2 (Password Hashing)

Specially designed hash functions for passwords. Unlike SHA-256 (designed to be fast), bcrypt and Argon2 are deliberately slow and memory-intensive — making brute-force and GPU attacks impractical. They include a random salt (preventing rainbow table attacks) and a configurable work factor (increase it as hardware gets faster). Always use these for password storage, never raw SHA-256.

Certificate Authority (CA)

A trusted entity that issues digital certificates binding a public key to an identity (domain name, organisation). Your browser has a built-in list of trusted CAs (Let's Encrypt, DigiCert, etc.). When a website presents a TLS certificate, your browser verifies it was signed by a trusted CA. This is the chain of trust that makes HTTPS trustworthy.

Key Facts

  • AES-256 is so secure that brute-forcing it would take longer than the age of the universe — even with all computing power on Earth. Breaking AES requires a mathematical breakthrough, not faster computers.
  • The Diffie-Hellman key exchange was published in 1976 — the first public description of asymmetric cryptography. Before DH, two parties had to physically exchange encryption keys.
  • SHA-256 produces a 256-bit (32-byte) hash. The probability of two different inputs producing the same hash (collision) is approximately 1 in 2^128 — physically impossible to find by brute force.
  • WhatsApp uses the Signal Protocol, which provides E2EE for 2 billion users' messages. Not even WhatsApp's servers can read the messages — only the sender and recipient's devices hold the keys.
  • Let's Encrypt has issued over 4 billion TLS certificates since 2015, making HTTPS essentially free and ubiquitous. Before Let's Encrypt, a certificate cost $100–$300/year.
  • Quantum computers threaten RSA and ECC because Shor's algorithm can factor large numbers and solve discrete logarithm problems efficiently. NIST finalized post-quantum cryptography standards in 2024 (CRYSTALS-Kyber for key exchange, CRYSTALS-Dilithium for signatures).

Real-World Applications

Password storage

Never store plaintext or MD5/SHA-1 passwords. Use bcrypt (cost factor 12+) or Argon2id. On login, hash the input and compare to the stored hash. bcrypt is intentionally slow (~100ms) — 1 million login attempts per day takes ~27 hours; a dictionary attack of 10 billion attempts takes centuries. Pepper (server-side secret mixed in before hashing) adds another layer against database leaks.

HTTPS / TLS

Every HTTPS connection uses: ECDH to derive a session key, AES-GCM to encrypt all data with that key, ECDSA/RSA signatures to verify the server's certificate is legitimate. TLS 1.3 (2018) removed all legacy weak algorithms (RC4, SHA-1, 1024-bit RSA, static DH) and mandates forward secrecy. Always use TLS 1.2+ in production; TLS 1.3 where possible.

Disk encryption

Full-disk encryption (FileVault, BitLocker, LUKS) encrypts the entire drive with AES-256. The AES key is protected by your login password (via key derivation function) or a hardware TPM chip. A stolen laptop without the password cannot be read — the data is random bytes without the key.

SSH key authentication

SSH uses asymmetric cryptography: you generate an ECDSA or Ed25519 key pair. Your public key is added to ~/.ssh/authorized_keys on the server. To connect, your SSH client signs a challenge with your private key; the server verifies with your public key. No password is sent over the network — the private key never leaves your machine.

Frequently Asked Questions

What is the difference between hashing and encryption?

Encryption is reversible — with the key, you can recover the original data. Hashing is a one-way function — given the hash, you cannot recover the input (without brute force). Use encryption when you need the original data back (storing credit card numbers, encrypting files). Use hashing when you only need to verify data (passwords — you hash the input and compare, you never need the original password).

Is AES-128 safe or should I always use AES-256?

Both are computationally infeasible to brute-force with classical computers. AES-128 has a security margin of 128 bits; AES-256 has 256 bits. In practice, both are secure. AES-256 is preferred for systems that need to remain secure against future quantum computers (Grover's algorithm halves the effective key size, making AES-128 equivalent to 64-bit security). For most applications, AES-128-GCM is perfectly safe.

What is end-to-end encryption and why does it matter?

E2EE means only the sender and recipient can read messages — not the service provider, not the government, not a hacker who breaches the server. The keys exist only on end-user devices. WhatsApp, Signal, and iMessage use E2EE. Gmail and Slack do not (they can read your messages). E2EE matters because server-side breaches expose user data even with HTTPS — E2EE means the leaked data is encrypted ciphertext without the private keys.

What is a rainbow table attack and how does salting prevent it?

A rainbow table is a precomputed lookup table of hashes for common passwords: SHA256("password123") → abc123.... An attacker with a stolen hash database checks against the table to find the original password instantly. Salting prevents this: before hashing, a random string (salt) is appended to the password. The salt is stored with the hash. Rainbow tables don't work because a unique table would be needed per salt. bcrypt and Argon2 handle salting automatically.

Related Topics