XOR & Simple Ciphers
Symmetric Encryption · Networking & Cryptography track
6 min read
In this module
The Simplest Secret
Encryption turns readable data (plaintext) into unreadable noise (ciphertext) so that only someone with the key can reverse the process. At the heart of nearly every modern cipher sits a humble bitwise operation you already know: XOR.
XOR: The Cryptographer's Favourite
Recall from the logic gates module that XOR outputs 1 when the inputs differ and 0 when they match:
0 XOR 0 = 00 XOR 1 = 11 XOR 0 = 11 XOR 1 = 0XOR has a near-magical property for cryptography: it's its own inverse. XOR data with a key, XOR the result with the same key, and the original data drops back into your lap.
plaintext: 01001000 ('H')key: 11010010ciphertext: 10011010 (XOR)
ciphertext: 10011010key: 11010010plaintext: 01001000 ('H') (XOR again - we're back!)One operation handles both encryption and decryption. Simple, elegant, and woven through the guts of every modern cipher.
XOR is the foundation of symmetric encryption because (A XOR K) XOR K = A. Encrypting is XORing with the key; decrypting is XORing with the same key. Every major symmetric cipher (AES, ChaCha20, etc.) uses XOR as a core building block.
The One-Time Pad
The simplest XOR cipher uses a key that is:
- As long as the message
- Truly random
- Used only once
This is the one-time pad (OTP), and it's the only cipher that's provably unbreakable. Not "hard for a really big computer" — mathematically impossible to crack without the key.
message: "HELLO" = 48 45 4C 4C 4F (hex)key: A3 7B 19 DE 55 (random bytes)cipher: EB 3E 55 92 1A (XOR)Without the key, the ciphertext could decrypt to any message of the same length with equal probability. The attacker gains exactly zero information.
That guarantee covers secrecy and nothing else. An attacker who knows or guesses part of the message can flip bits in the ciphertext and the same bits flip in the decrypted plaintext: XOR the first ciphertext byte with 02 (the difference between H, 48, and J, 4A) and "HELLO" decrypts as "JELLO", with no way for the receiver to notice. Detecting tampering needs a separate authentication step, which later modules cover.
The catch is logistical. The key must be as long as the message, genuinely random, and never reused. Shipping keys like that around securely is brutally hard, which is why one-time pads have only seen real use in special circumstances — diplomatic cables during the Cold War, the odd intelligence channel, and a few paranoid bankers.
Key Reuse: The Fatal Mistake
What happens if you reuse the key for two different messages?
cipher1 = message1 XOR keycipher2 = message2 XOR key
cipher1 XOR cipher2 = (message1 XOR key) XOR (message2 XOR key) = message1 XOR message2 (the key cancels out!)The attacker doesn't even need the key. XOR the two ciphertexts together, the key cancels, and you're left holding the XOR of the two plaintexts — which leaks structure and lets frequency analysis or known-plaintext tricks recover both.
Try both sides of this below. In "XOR cipher", encrypt a message with a short repeating key and watch the repeats show through, then switch to a one-time pad and decrypt by XORing with the key again. In "Two-time pad", the same pad encrypts two messages: check that c1 ⊕ c2 matches p1 ⊕ p2 byte for byte, then guess a word such as " the " and see which offsets reveal part of the other message.
| Row | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Text | a | t | t | a | c | k | a | t | d | a | w | n | , | a | t | t | a | c | k | a | t | d | u | s | k | |||||
| Plaintext | 61 | 74 | 74 | 61 | 63 | 6b | 20 | 61 | 74 | 20 | 64 | 61 | 77 | 6e | 2c | 20 | 61 | 74 | 74 | 61 | 63 | 6b | 20 | 61 | 74 | 20 | 64 | 75 | 73 | 6b |
| ⊕ key | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 | 6b | 65 | 79 |
| Ciphertext | 0a | 11 | 0d | 0a | 06 | 12 | 4b | 04 | 0d | 4b | 01 | 18 | 1c | 0b | 55 | 4b | 04 | 0d | 1f | 04 | 1a | 00 | 45 | 18 | 1f | 45 | 1d | 1e | 16 | 12 |
| ⊕ key again | 61 | 74 | 74 | 61 | 63 | 6b | 20 | 61 | 74 | 20 | 64 | 61 | 77 | 6e | 2c | 20 | 61 | 74 | 74 | 61 | 63 | 6b | 20 | 61 | 74 | 20 | 64 | 75 | 73 | 6b |
| Decrypted | a | t | t | a | c | k | a | t | d | a | w | n | , | a | t | t | a | c | k | a | t | d | u | s | k |
XORing the ciphertext with the same key gives back every plaintext byte. Bytes that aren't printable ASCII show as dots, including each byte of a multi-byte UTF-8 character.
The 3-byte key repeats (shaded bands). 4 plaintext bytes equal the byte 3 places later, so the ciphertext repeats there too (marked). That repetition is how a short key gets found.
This is not a chalkboard worry. Real systems have died on this hill, from early wireless encryption (WEP, whose 24-bit per-packet IVs ran out and repeated, so the same RC4 keystream got reused) to embarrassing intelligence community blunders that historians will be picking over for decades.
The Caesar Cipher and Substitution
Before XOR-based ciphers, classical ciphers used substitution. The Caesar cipher shifts each letter by a fixed amount:
Plaintext: HELLOShift by 3: KHOORA child can break this. Try all 26 shifts and read whichever output looks like English. Or skip the brute force and lean on letter frequencies — 'E' shows up about 12% of the time. Single-character substitution ciphers are toys.
Break two of them yourself. The program tries all 25 Caesar shifts, then recovers a single-byte XOR key by scoring all 256 candidates on how English-like the output looks. Both ciphers are toys: never use either to protect anything real.
A key space of 256 is nothing, and the scorer doesn't need to know the message. It only needs to know that English is mostly letters and spaces.
From Simple to Strong
The one-time pad is perfect but impractical. Simple XOR with a short repeating key is practical but insecure (the key can be recovered through pattern analysis). The challenge of modern cryptography is building ciphers that are both practical (small key, fast computation) and secure — meaning the ciphertext looks like noise to anyone who doesn't hold the key. You get there through complex transformations that stack XOR alongside substitution, permutation, and mixing across many rounds. That's exactly what AES does, and it's where we're headed next.
The Kerckhoffs Principle
A bedrock rule of cryptography: the security of a cipher must rest only on the secrecy of the key, not the secrecy of the algorithm. The algorithm should be public, hammered on by everyone, and still hold up so long as the key stays secret.
This is Kerckhoffs's Principle, and it's why AES is published and openly analysed. Security through obscurity is brittle — once the algorithm leaks (and it always does eventually), every byte you've ever encrypted with it is suspect. Security through key secrecy holds under scrutiny: even with full knowledge of how AES works, an attacker still can't decrypt your data without your key.
From XOR to Modern Ciphers
XOR is the atom of encryption. It's not encryption by itself. The leap from XOR to a real cipher means stacking it alongside substitution tables, bit permutations, and mathematical mixing functions to forge ciphertexts that resist every known analytical attack. The next lesson watches AES do exactly that — building on these ideas into a block cipher that has shrugged off a quarter of a century of cryptanalysis. The path from a single XOR gate to a modern encryption standard is the story of layering simple operations into something far stronger than the sum, with each layer designed to plug a specific class of attack the previous layer left open.