Encryption Basics
Security Fundamentals · Security Fundamentals track
5 min read
In this module
Keeping Secrets with Maths
Encryption transforms readable data (plaintext) into scrambled data (ciphertext) that is meaningless without the right key. It is the primary tool for achieving confidentiality, and it comes in two fundamental flavours.
Symmetric Encryption
In symmetric encryption, the same key is used to encrypt and decrypt. Think of it like a lockbox with a single key - whoever has the key can both lock and unlock it.
A simple teaching example is the XOR cipher. XOR each bit of your message with the corresponding bit of a key and you get ciphertext; XOR the ciphertext with the same key and the original message re-emerges. XOR alone is far too weak for real use, but the principle (combine data with a key using a reversible operation) carries over: AES mixes its round keys into the data with XOR, and stream ciphers such as ChaCha20 XOR a keystream into the message. Real ciphers also need a mode of operation, a fresh IV or nonce per message, and ideally authentication, which is why you reach for an authenticated mode such as AES-GCM from a vetted library rather than building your own.
Symmetric encryption uses one shared key for both encryption and decryption - fast but requires both parties to securely share the key. Asymmetric encryption uses a pair of keys (public and private) - slower but solves the key distribution problem, since the public key can be shared openly.
Asymmetric Encryption
Asymmetric (public-key) encryption uses two mathematically related keys. Data encrypted with the public key can only be decrypted with the corresponding private key. You can freely publish your public key; anyone can use it to encrypt a message that only you can read. The private key also does a second job: signing. A signature made with the private key can be checked by anyone holding the public key, which proves who sent a message rather than hiding it.
This elegantly solves the biggest problem with symmetric encryption: how do two parties agree on a shared key without an eavesdropper intercepting it? In practice, systems like HTTPS use public-key techniques to agree a symmetric key, then switch to the faster symmetric algorithm for the actual data transfer.
To see the maths work, here's RSA with numbers small enough to check by hand. It's a toy: real keys use primes hundreds of digits long, and these would fall to a child with a calculator.
Key generation pick primes p = 3, q = 11 modulus n = p × q = 33 totient φ(n) = (p − 1)(q − 1) = 20 public exponent e = 3 (shares no factor with 20) private exponent d = 7 (3 × 7 = 21, and 21 mod 20 = 1) public key (e, n) = (3, 33) private key (d, n) = (7, 33)
Encrypt the message m = 4 with the public key c = m^e mod n = 4^3 mod 33 = 64 mod 33 = 31
Decrypt with the private key m = c^d mod n = 31^7 mod 33 = 4Anyone can compute 31 from 4 using (3, 33). Getting back from 31 to 4 needs d = 7, and finding d means knowing φ(n), which means factoring n. Factoring 33 is trivial. Factoring a 2048-bit modulus isn't, as far as anyone knows.
How HTTPS Uses Both
When your browser connects to a website over HTTPS:
- Browser and server each send an ephemeral Diffie-Hellman key share and both derive the same shared session key from them. Fresh shares every time give forward secrecy: stealing the server's key later doesn't decrypt old traffic.
- The server presents its certificate, which contains its public key, and signs the handshake with the matching private key, so your browser knows who it agreed the key with.
- All subsequent data is encrypted with that session key using fast symmetric encryption.
Older TLS versions could instead have the browser encrypt a secret under the server's RSA public key. TLS 1.3 removed that option because it had no forward secrecy.
Step through a real TLS 1.3 handshake below. In the modern version the certificate's key doesn't encrypt the session key at all: both sides send Diffie-Hellman key shares in their first messages, derive the same secret from them, and the server uses its certificate key to sign the handshake so you know who you agreed it with. Watch the badge on each message move from plaintext to handshake keys to application keys.
TLS 1.3 full handshake
A first connection: ECDHE key exchange and a certificate, in one round trip. (RFC 8446 section 2)
- Handshake traffic keys from here
- Application traffic keys from here
Nothing sent yet. Step through to watch what each side learns and when encryption starts.
- Early Secret
- Early Secret
This hybrid approach gives you the best of both worlds: the key-exchange safety of asymmetric cryptography and the speed of symmetric cryptography.
Why This Matters
Without encryption, every password, message, and card number you send would be readable by anyone on the path. Understanding how it works — even at a conceptual level — sharpens every technical decision you make about the tools and protocols you reach for.