I write about breaking foundational technologies down to first principles and rebuilding them from scratch to understand how modern software actually works.
Follow the ongoing work on Substack:
đ https://dmytrohuz.substack.com
âWe use spells we don't understand, then act surprised when things break.â
đ The Magic We Never Question
The next stop in my journey to rebuild the internet from first principles is something powerful, essential, and strangely invisible: security protocols.
TLS (Transport Layer Security) encrypts communication between web browsers and servers, keeping online interactions secure. SSH (Secure Shell) provides secure remote access to servers. VPNs (Virtual Private Networks) create encrypted tunnels over the internet, protecting data from prying eyes.
We use them every day - to log into servers, secure our APIs, encrypt sensitive data. They are the invisible guards that make the modern web possible. Without them, the internet would be like shouting your passwords into a crowded train station.
And behind all of them is one thing: cryptography.
For years, I thought I understood it. I installed certificates. I enforced HTTPS. I even helped debug production TLS issues. I knew the right commands. I thought I knew the concepts.
But I was wrong.
đ§ The âWhy?â That Exposes Everything
It hit me while I was building my own web server from scratch. I was deep into HTTP, connection handling, sockets - the guts of the web. And then I reached the point where I wanted to add HTTPS support.
I paused. âOkay,â I thought, âso this is where SSL goes. But... how does it actually work?â
Not the commands. Not the installation steps.
The logic. The math. The trust.
And I realized - I had no idea.
I could say âTLS handshakeâ or âpublic keyâ or âcertificate authority,â but if someone asked me why any of this worked, Iâd stumble. Just like when a child asks, âWhy is the grass green?â and you answer âphotosynthesis,â only to collapse at the second âwhy.â
Thatâs when I understood: I wasnât using cryptography. I was just reciting spells.
And so, I made a decision: no more black boxes. I would rebuild my knowledge from the ground up - understand every mechanism - and share that journey openly.
đ This Series: From Zero to TLS, Step by Step
After searching for the right starting point, I found it: Stanfordâs Cryptography I course by Dan Boneh. Itâs a rigorous but accessible deep dive into the foundations of modern cryptography - no fluff, no handâwaving.
This series will follow the course - not just repeating it, but rewriting the insights in my own words, through the lens of a developer rediscovering the fundamentals.
We begin with Week 1: the basics of symmetric encryption, randomness, and probability - the raw materials from which realâworld protocols are built.
Week 1 - What Even Is Cryptography?
đď¸ The General and the Message
Imagine you're a Roman general. Tomorrowâs battle is critical. You canât risk riding out, but you must send secret orders to another commander.
You use a messenger. But what if heâs intercepted?
The only solution: make your message meaningless to outsiders - and readable only by the intended recipient.
That is the ancient problem cryptography solves:
Make a message readable only by its intended recipient - even if itâs seen by the entire world.
đ The Core Model of Symmetric Encryption
Cryptography often starts with symmetric encryption, where the same secret key is used for both encryption and decryption.
Notation:
-
m = message (plaintext) -
k = key (secret shared between sender and receiver) -
E(k, m) = c = encryption function (Encryption must be reversible because the intended recipient needs to convert the ciphertext back into the original message. Without reversibility, the encrypted data would be permanently unreadable.) -
c = ciphertext (unreadable version of the message) -
D(k, c) = m = decryption function
The encryption and decryption must satisfy:
D(k, E(k, m)) = m
Both sides must share the same key - hence, symmetric.
đş Historical Ciphers (and How They Failed)
Examples:
- Caesar Cipher: shift each letter by ânâ
- Substitution Cipher: swap every letter with another
- Vigenère Cipher: use a keyword to shift letters cyclically
How They Were Broken:
- Frequency analysis: Exploits letter frequency in language.
- Knownâplaintext attacks: Using known message portions to reverseâengineer keys.
These early ciphers lacked true randomness, making them vulnerable.
đ§Ž Probability: The Foundation of Secrecy
đ˛ Probability Distributions
If we have a set U (e.g., 2âbit strings {00, 01, 10, 11}), a probability distribution P assigns likelihood to each item.
Examples:
-
Uniform distribution: Each outcome equally likely (
P(x)Â =Â 1/4). -
Point distribution: One outcome certain (
P(10)Â =Â 1, othersÂ0).
đŚ Events
In cryptography, weâre often interested in how likely it is for certain outcomes - like a key being guessed - to happen. These are called events.
An event is a collection of possible outcomes. Rolling a die, outcomes are {1,2,3,4,5,6}.
- Event A (even numbers):
{2,4,6},Pr[A]Â =Â 0.5.
â ď¸ Union Bound
Estimates the probability that at least one of several rare events happens (A,âŻB,âŻorâŻboth):
Pr[A âŞÂ B] â¤Â Pr[A] + Pr[B]
The union (âŞ) means âA or B (or both).â The sum on the right may doubleâcount the overlap, so it is always an upper bound - handy when you just need a worstâcase figure.
Example
- Event A: an attacker correctly guesses a 128âbit AES key on the first try â
Pr[A] = 2^{-128}. - Event B: the same attacker forges a 128âbit MAC tag by blind luck â
Pr[B]Â =Â 2^{-128}.
The chance the attacker succeeds in either attack is at most:
Pr[A âŞÂ B] â¤Â 2^{-128} + 2^{-128} = 2^{-127}
Still astronomically small - but twice the singleâattack probability. The union bound lets us combine multiple risks into one clear, conservative number.
đŁ Variables vs. Random Variables
A variable holds a known, fixed value (e.g., x = 5).
A random variable captures outcomes from random processes:
- Coin flips (
X: number of heads). - Die rolls (
Y: even = 1, odd = 0).
They measure randomness mathematically.
đ Uniform Random Variables and Randomized Algorithms
A uniform random variable: every outcome equally probable (e.g., random 8âbit key).
-
Deterministic algorithm (
y â A(m)): same input yields same output. -
Randomized algorithm (
y â A(m; r)): randomnessrgives unpredictable outcomes.
In cryptography, randomized algorithms prevent predictability and pattern recognition.
đ Random Variables in Cryptography
Randomness in cryptography prevents attackers from easily predicting encrypted outcomes. Without randomness, encryption outputs would be predictable, allowing attackers to exploit patterns and ultimately compromise the security of the encryption.
Examples:
-
Key Generation (
K): 128âbit keys have equal probabilities (1 in 2^128). -
Ciphertext (
C) Distribution: If encryption varies outputs for same inputs, it prevents leaks. -
Attacker Guess (
X): Probability attacker correctly guesses plaintext (e.g., how often a hackerâs random guess matches your password).
Random variables quantify cryptographic security.
đ§Š Independence
Events A and B are independent if knowing that one occurs tells you nothing about the chance of the other. Formally, the probability of both happening together (the intersection AâŻâ§âŻB) equals the product of their individual probabilities:
Pr[AâŻâ§âŻB] = Pr[A] Ă Pr[B]
Example
Fair coins â Flip two independent fair coins.
- EventâŻA: first coin is heads â Pr[A]âŻ=âŻÂ˝.
- EventâŻB: second coin is heads â Pr[B]âŻ=âŻÂ˝.
Because the flips are independent, the chance of both being heads is:
Pr[AâŻâ§âŻB] = ½ Ă ½ = Ÿ - exactly the realâworld probability of âHH.â
Why it matters in crypto
If a ciphertext leaks information about its plaintext, the events âciphertextâŻ=âŻCâ and âplaintextâŻ=âŻPâ become dependent. A secure cipher keeps them independent so that:
Pr[plaintextâŻ=âŻP | ciphertextâŻ=âŻC] = Pr[plaintextâŻ=âŻP],
meaning the ciphertext reveals nothing beyond what an attacker already knew.
⨠The Magic of XOR in Cryptography
XOR (exclusive OR) is a basic bitwise operation thatâs at the heart of modern symmetric encryption. Itâs simple, powerful, and - most importantly - reversible.
How XOR Works
XOR takes two bits as input and outputs 1 if they are different, 0 if they are the same.
| Bit 1 | Bit 2 | Bit 1 â Bit 2 |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Why XOR Is Perfect for Encryption
-
Reversibility: If you XOR a bit with a key, you can recover the original by XORâing again with the same key.
- Example: If your plaintext bit is
Pand your key bit isK: - Encryption:
C = P â K1101 â 0110 = 1011 - Decryption:
P = C â K 1011 â 0110 = 1101
- Example: If your plaintext bit is
This works for entire strings, not just single bits - just apply XOR to each bit in the string.
XOR: The Ideal Encryption Building Block
- No Information Lost: XOR is a one-to-one transformation if the key is kept secret.
- No Bias Introduced: If the key is random, the result looks random, regardless of the original message.
- Simplicity: XOR is easy to compute, both in hardware and software, and forms the basis for more complex encryption schemes.
đŤ Why Not Use Other Bitwise Operations for Encryption?
The core reason we use XOR and not OR/AND is reversibility: you must be able to get the original message back using the key and the ciphertext.
Letâs prove it step by step:
1. XOR Is Reversible
If you encrypt with XOR, you can always decrypt:
- Encrypt:
C = P â K - Decrypt:
P = C â K
Proof:
Letâs expand the decryption:
C â K = (P â K) â K
But XOR is associative and commutative, and K â K = 0:
(P â K) â K = P â (K â K) = P â 0 = P- You always recover the original P.
2. OR and AND Are Not Reversible
Example with OR
Suppose you use OR for encryption:
- Encrypt:
C = P OR K
Letâs try to recover P from C and K.
Case:
Letâs say K = 1.
- For any P (0 or 1):
P OR 1 = 1
So C = 1, but you canât tell if P was 0 or 1.
Another way:
| P | K | P OR K | Can you recover P from C and K? |
|---|---|---|---|
| 0 | 0 | 0 | Yes (P=0) |
| 0 | 1 | 1 | No (could be P=0 or 1) |
| 1 | 0 | 1 | Yes (P=1) |
| 1 | 1 | 1 | No (could be P=0 or 1) |
Example with AND
Suppose you use AND for encryption:
- Encrypt:
C = P AND K
Case:
Letâs say K = 0.
- For any P:
P AND 0 = 0
So C = 0, but you canât tell if P was 0 or 1.
| P | K | P AND K | Can you recover P from C and K? |
|---|---|---|---|
| 0 | 0 | 0 | No (could be P=0 or 1) |
| 0 | 1 | 0 | Yes (P=0) |
| 1 | 0 | 0 | No (could be P=0 or 1) |
| 1 | 1 | 1 | Yes (P=1) |
Conclusion:
OR and AND âlose informationâ - many different P and K can give you the same C, so you cannot get the original message back.
XOR is the only common bitwise operation that is always reversible with knowledge of the key.
đŞ Summary: Building Blocks Learned
- Symmetric encryption starts with a shared secret key.
- Probability and events help us measure risk and security.
- Random variables and independence turn randomness into something we can analyze.
- XOR is the backbone of secure, reversible encryption at the bit level.
- Why not OR/AND? Theyâre not reversible - encryption must always be reversible!
Weâre building from the fundamentals upward, laying the groundwork for real cryptographic security.
Cryptography isnât magic-itâs math, logic, and trust.
Letâs keep uncovering its secrets, one layer at a time.
P.S.
This post is an experiment in format: I packed a lot of material and rewrote it from a developerâfirst perspective. ChatGPT helped with proofreading and structure, but the voice and mistakes are mine. Iâd love your feedback on clarity, depth, and length -everything will feed into the next articles in the series.
Thanks for reading, and stay tuned-more posts are coming soon!
Follow the ongoing work on Substack:
đ https://dmytrohuz.substack.com
Top comments (1)
Man, Iâve lost count of how many times I thought I got this but had no clue what was happening under the hood - reading this hits home in the best way.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.