Skip to main content
Be Bitwise

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:

text
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

XOR 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.

text
plaintext: 01001000 ('H')
key: 11010010
ciphertext: 10011010 (XOR)
ciphertext: 10011010
key: 11010010
plaintext: 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.

Key Concept

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.

text
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?

text
cipher1 = message1 XOR key
cipher2 = 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.

Key
Plaintext, key and ciphertext bytes in hex, one column per byte
Row01234567891011121314151617181920212223242526272829
Textattack at dawn, attack at dusk
Plaintext61747461636b206174206461776e2c2061747461636b206174206475736b
⊕ key6b65796b65796b65796b65796b65796b65796b65796b65796b65796b6579
Ciphertext0a110d0a06124b040d4b01181c0b554b040d1f041a0045181f451d1e1612
⊕ key again61747461636b206174206461776e2c2061747461636b206174206475736b
Decryptedattack at dawn, attack at dusk

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:

text
Plaintext: HELLO
Shift by 3: KHOOR

A 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.

Try itFind the right Caesar shift by eye, then swap in your own XOR ciphertext and see if the scorer still wins.
C (GCC 9.2.0)

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.