Cryptography: how secrets stay secret
Lesson 2 of 4
2Cryptography: how secrets stay secret
The big pictureEvery time you open your bank app, your phone agrees a secret key with the server across a network anyone can listen to, encrypts everything with it, and checks the server really is the bank. All in under a second, without ever having met. That is what modern cryptography does.
What the picture shows: Plaintext goes in with a key and comes out as meaningless ciphertext, and only the right key turns it back.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Three tools in your box: a box with one key that locks and unlocks (symmetric): fast, but how do you give your friend the key without it being stolen? A mailbox with a slot anyone can drop a letter into, that only its owner can open (asymmetric). And a fingerprint for a file (hashing): the same file always gives the same fingerprint, any small change gives a completely different one, and you cannot turn a fingerprint back into the file.
Symmetric: AES-256-GCM and ChaCha20-Poly1305 (authenticated AEAD encryption). Asymmetric: RSA and ECC (X25519 for key exchange, Ed25519 for signing). A digital signature hashes the message then signs with the private key, so anyone verifies with the public one (integrity and non-repudiation). TLS 1.3: ECDHE key exchange with forward secrecy, then AEAD for the session, and X.509 certificates from a PKI chain to prove identity. Post-quantum: on 13 August 2024 NIST published FIPS 203 (ML-KEM), FIPS 204 (ML-DSA) and FIPS 205 (SLH-DSA), and picked HQC in March 2025 as a backup, against "harvest now, decrypt later".
Before You Start
🔑Identity and passwords
You know why passwords are stored as hashes.
🛰️Opening a website
From the networking track: the TLS stage of the journey.
The Core Idea
1Symmetric and asymmetric
Symmetric uses one key to lock and unlock, and it is very fast, so it encrypts large data: your disk, your messages, the browsing session. Its one problem: how does the key reach the other side safely? Asymmetric solves it with two keys: a public one you publish so anyone can lock with it, and a private one that never leaves your device and alone can unlock.
2Hashing: a fingerprint that cannot be reversed
A hash function takes any input, a word or a whole film, and gives a fixed-length fingerprint: SHA-256 always gives 64 hex digits. Three properties make it useful: the same input gives the same fingerprint, a tiny change changes the whole fingerprint (the avalanche effect), and the fingerprint does not reveal the input.
Drill: the avalanche effect in SHA-256
Change one character in either box and watch the fingerprint. Coloured digits are the ones that differ. It is computed inside your browser and the text is sent nowhere.
6e8a4186420e83b03f2fd0f7836ef2e2e7869d0b88230e2c65a67968b1aed035
f0671d391fe3e4b072f6b0d3c00ae1fcb10f34a646af13ada46dcb5965671aef
61 of 64 digits differ. A tiny change in the text changed nearly the whole fingerprint, and the fingerprint cannot tell you the original text.
The first and third lines match so their hashes match; the second differs by one digit and its whole hash changed.
import hashlib
for text in ["Send 500 SAR to Sara", "Send 900 SAR to Sara", "Send 500 SAR to Sara"]:
digest = hashlib.sha256(text.encode()).hexdigest()
print(digest[:32] + "...", text)That is why software sites publish a file's hash next to the download link: you hash what you downloaded and compare, and if they match the file was not tampered with on the way.
3Digital signatures and TLS
A digital signature runs asymmetric crypto the other way: you sign with your private key and everyone verifies with your public key. That is how your phone knows an update really came from the vendor and nobody altered it.
TLS 1.3 (the browser padlock) combines all three: asymmetric key exchange to agree on a session key, then fast symmetric encryption for all the data, and a certificate signed by a trusted authority proving the server really is bank.example.
| Tool | Protects | Modern examples | Avoid |
|---|---|---|---|
| Symmetric | Confidentiality | AES-GCM · ChaCha20 | DES, RC4, ECB mode |
| Asymmetric | Confidentiality, key exchange | X25519 · RSA-3072 · ML-KEM | RSA under 2048 bits |
| Hashing | Integrity | SHA-256 · SHA-3 | MD5 · SHA-1 |
| Signatures | Integrity, identity | Ed25519 · ECDSA · ML-DSA | A private key shared between people |
4After quantum computing
Large quantum computers may one day break RSA and ECC, which protect the internet today. Attackers may collect encrypted data now to decrypt later. So in August 2024 NIST published the first three quantum-resistant standards: ML-KEM for key exchange, and ML-DSA and SLH-DSA for signatures. Major browsers already use a hybrid key exchange combining X25519 with ML-KEM.
Worked example
The task: you downloaded an installer, and the site publishes its SHA-256 hash next to it. How do you check, and what does a mismatch mean?
The answer: hash the file on your machine: sha256sum file or shasum -a 256 file on Linux and macOS, Get-FileHash file on Windows. Compare the output character by character with the published one. A match means the file did not change. A mismatch means a broken download or tampering, so do not install it. And remember: if the site itself is hacked the hash may be swapped too, which is why a digital signature is stronger than a hash alone.
Myth vs Reality
| Myth | Reality |
|---|---|
| Encryption and hashing are the same. | Encryption is undone with a key and protects confidentiality; hashing is one-way and protects integrity. |
| Quantum computers have already broken encryption. | No capable quantum computer exists yet, but migration takes years so it started early. AES-256 stays strong. |
Where you meet it at work
🏦At home
The browser padlock, your bank app and WhatsApp messages are cryptography you use daily.
🧑💻Developer
Picks AES-GCM or libsodium, signs their releases, and never keeps keys in the code.
Test your understanding
A digital signature guarantees:
It proves the message was not altered and came from the private key's owner; it does not hide the content.
Why did NIST adopt post-quantum algorithms with no capable quantum computer yet?
"Harvest now, decrypt later" threatens long-lived data, and migration takes years.
You change one character of a text. What happens to its SHA-256 hash?
That is the avalanche effect; the length always stays 64 digits.
Which symmetric cipher is common today?
RSA is asymmetric; SHA-256 and MD5 are hashes, not encryption.
What does a sender sign a message with digitally?
Anyone verifies it with their public key.
Quick review cards
Check Your Understanding
Why does TLS use both kinds instead of asymmetric alone?
Because asymmetric is slow, so it only agrees a key; fast symmetric then encrypts the whole session.
Can you rely on MD5 for passwords or file checks?
No, MD5 is broken and two files with the same hash can be made. Use SHA-256 for files and Argon2id for passwords.
Why do we hash passwords instead of encrypting them?
Encryption can be reversed with the key, so a stolen key exposes every password. A hash cannot be reversed; we only compare hashes.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Done with this lesson?
Sign in to save your progress.