Chapter II
Ground rules
What modern cryptography promises, who it defends against, and the raw material it works with: bits, XOR and randomness.
The four goals
Confidentiality, integrity, authenticity, non-repudiation: four different promises, and encryption alone keeps only the first.
Kerckhoffs’s principle
Assume the enemy knows the whole system. Only the key is secret.
Kerckhoffs’s second requirement: the system “must not require secrecy, and must be able to fall into the enemy’s hands without inconvenience.”
Attackers and threat models
Secure means nothing until you say against whom: what the attacker can see, and what they can make you do.
Eve listens; Mallory also alters and injects messages. What they can obtain sets the attack model:
Bits of security
An n-bit key means 2ⁿ guesses. At 128 bits, brute force is physically out of reach.
A key of bits can take values, and each extra bit doubles the attacker’s work. Strength is counted in bits: the base-2 logarithm of the effort.
Bits, bytes and XOR
XOR flips the bits where the key has a 1; the same key flips them back.
XOR, written ⊕, is the workhorse on bits. With a uniformly random key, the result is uniformly random whatever the message says: the one-time pad of chapter I, on bits, and the core of every stream cipher (chapter IV).
Encoding is not encryption
Hex and Base64 change how bytes look, not who can read them.
Bytes often cross channels made for text, such as URLs and headers. Hex writes each byte as two characters. Base64 reads six bits at a time, each picking one of 64 letters, digits and signs, and pads the end with =.
Randomness
Keys, IVs and salts come from the operating system’s secure generator, a CSPRNG, never from Math.random. Nonces only have to be unique.
The generator is the operating system’s: crypto.getRandomValues() in browsers, Node.js and Bun, getrandom() on Linux. Math.random() is not one: browsers use xorshift128+, whose state a few outputs rebuild. The simplest generators give themselves away after one.
In one breath
- The four goals. Confidentiality, integrity, authenticity, non-repudiation: four different promises, and encryption alone keeps only the first.
- Kerckhoffs’s principle. Assume the enemy knows the whole system. Only the key is secret.
- Attackers and threat models. Secure means nothing until you say against whom: what the attacker can see, and what they can make you do.
- Bits of security. An n-bit key means 2ⁿ guesses. At 128 bits, brute force is physically out of reach.
- Bits, bytes and XOR. XOR flips the bits where the key has a 1; the same key flips them back.
- Encoding is not encryption. Hex and Base64 change how bytes look, not who can read them.
- Randomness. Keys, IVs and salts come from the operating system’s secure generator, a CSPRNG, never from Math.random. Nonces only have to be unique.