4 Cryptography

The security of a good cipher rests on the key, not on hiding how it works. That single idea, Kerckhoff’s principle, organises the whole field.
Cryptography is the closest thing security has to a superpower: done right, it lets you protect information even when your enemy can see everything you send. It is also the part of the field most wrapped in mystique and most often misunderstood. This chapter strips away the mystique. You do not need the mathematics to reason about cryptography well. You need to understand what each kind of cryptography guarantees, what it doesn’t, and where the real weaknesses lie (which, as ever, are rarely in the maths).
4.1 What this chapter covers
By the end you should be able to:
- Explain the difference between an algorithm and a key, and state Kerckhoff’s principle.
- Distinguish symmetric (one-key) from asymmetric (two-key) cryptography and say what each is good for.
- Explain hashing vs encryption, and where digital signatures fit.
- Reason about attacks: brute-forcing small key spaces, known-plaintext and chosen-plaintext attacks.
4.2 The algorithm and the key
Every cipher has two parts, and telling them apart is the foundation of everything else. The algorithm is the method: the fixed procedure for scrambling and unscrambling. The key is the secret input that steers it: the particular setting that turns readable plaintext into scrambled ciphertext and back. The same algorithm with a different key produces different output. The set of all possible keys is the key space, and its size is one measure of a cipher’s strength: the more keys an attacker would have to try, the better.
Here is where beginners’ intuition usually goes wrong. It feels safer to keep the algorithm secret too. Surely hiding the method helps? Kerckhoff’s principle says the opposite, and it is one of the most important ideas in the whole field:
A cryptosystem should be secure even if everything about it, except the key, is public knowledge.
In other words, the security must rest entirely on the secrecy of the key, never on the secrecy of the algorithm. This sounds counter-intuitive until you see why it’s a strength. A secret algorithm has never been examined by anyone but its creators, so no one knows whether it’s flawed. Once it leaks (it always leaks), your security evaporates. A public algorithm, published and attacked by the world’s cryptographers for years, has earned its trust the hard way. This is why every cipher you should trust (the ones protecting your web traffic, your messages, your disk) has an openly published algorithm and keeps only the key secret. “Secret sauce” cryptography is a warning sign.
4.4 Asymmetric cryptography: a key you can publish
The breakthrough that solved it is asymmetric (or two-key) cryptography. Instead of one shared secret, each person has a pair of mathematically linked keys: a public key they can hand out to anyone, and a private key they keep to themselves. The crucial property is that what one key locks, only the other can unlock. Knowing the public key does not let you work out the private key.
That single trick does two different jobs, and it’s worth keeping them apart. The first is confidentiality. Anyone can encrypt a message to you using your public key, but only your private key can decrypt it. Now a stranger can send you a secret without ever sharing a key in advance. The key-distribution problem dissolves, because the encrypting key was never secret. The second is authenticity, through digital signatures. Run it the other way: you encrypt (sign) something with your private key, and anyone can verify it with your public key. Since only you have your private key, a valid signature proves the message came from you and wasn’t altered. This is where non-repudiation and integrity come from.
Asymmetric cryptography is slower than symmetric, so real systems use both: asymmetric cryptography to safely exchange a fresh symmetric key, then fast symmetric encryption for the conversation. That hybrid is, in essence, what happens every time your browser opens a secure connection. The web calls this scheme Transport Layer Security, or TLS.
4.5 Hashing: one-way, and not encryption
A hash function is often grouped with cryptography but does something different, and the difference matters. Encryption is reversible. The whole point is that the right key gets the original back. A cryptographic hash is deliberately one-way: it turns any input into a fixed-length fingerprint that cannot be reversed to recover the input. You met this in the last chapter, where systems store the hash of a password rather than the password itself.
So the two answer different questions. Encryption protects confidentiality: keeping content secret from those without the key. Hashing protects integrity: proving that content hasn’t changed, since any alteration produces a different fingerprint. If you want to hide a message, encrypt it. If you want to prove a file hasn’t been tampered with, hash it. Confusing the two is a common and revealing mistake: “I hashed it so it’s secret,” or “I encrypted it so I’ll know if it changed.”
4.6 Attacks, and the limits of weak crypto
Understanding how ciphers fail sharpens the intuition. The simplest failure is a key space too small to matter. The classic Caesar cipher shifts each letter by a fixed amount. But there are only twenty-five possible shifts, so an attacker tries them all in seconds. A monoalphabetic substitution cipher, where each letter maps to another, has an enormous key space yet still falls easily, because it leaks the structure of the language: letter frequencies survive the substitution, and ‘e’ is still the commonest letter. A big key space is necessary but not sufficient. A cipher can be broken by the patterns it fails to hide.
Cryptographers also reason about what an attacker gets to see. In a known-plaintext attack, the attacker has some plaintext and its matching ciphertext and uses the pair to attack the key. In the stronger chosen-plaintext attack, they can feed in plaintext of their choosing and study the output. A cipher worth trusting must withstand even an adversary with those advantages. That is why real ciphers are published and battered by experts rather than kept secret and hoped over.
Modern cryptography is strong, and attackers know it, so they almost never attack the maths. They steal the key from a poorly protected file, trick a user into revealing it, exploit a bug in the implementation of a sound algorithm, or take the data before it’s encrypted or after it’s decrypted. Even ransomware, which weaponises encryption against you, succeeds through the plumbing (how it gets in and what it can reach) rather than by breaking the cipher. The cryptography is the strongest link. Assume the attacker will go for the others.
4.7 Where this connects
The hashing that stores your password is the integrity side of authentication. The encrypted tunnel a VPN builds rests on the symmetric, asymmetric, and key-exchange ideas here, as VPN and firewalls will show. And the secure connection behind every reputable website is the hybrid scheme described above, which returns in web security and data protection.
4.8 Questions to consider
- Why is it a good thing that strong ciphers publish their algorithms openly?
- Symmetric crypto is faster; asymmetric solves key distribution. Sketch how a real system might use both.
- What’s the practical difference between hashing a message and encrypting it? When would you want each?