2. XOR & One-Time Pad

The only provably unbreakable cipher

The substitution cipher failed because the same plaintext letter always becomes the same ciphertext letter. What if we could combine each byte of the message with a corresponding byte from a random key? If the key is truly random, the ciphertext reveals nothing about the plaintext. This is the idea behind XOR encryption.

The XOR Operation

XOR (exclusive or) is a bitwise operation. For each pair of bits, the output is 1 if exactly one of the inputs is 1, and 0 otherwise.

XOR truth table

A  B  A⊕B
0  0   0
0  1   1
1  0   1
1  1   0

XOR has a property that makes it perfect for encryption: it's its own inverse.

The Self-Inverse Property

If C = P ⊕ K, then P = C ⊕ K. XOR with the same key encrypts and decrypts. There's no need for separate encrypt/decrypt operations.

A Worked Example

Encrypting "Hi" with key "Ky"

Plaintext:   H        i
ASCII:       72       105
Binary:      01001000 01101001

Key: K y ASCII: 75 121 Binary: 01001011 01111001

XOR: 01001000 01101001 01001011 01111001 ──────────────── Ciphertext: 00000011 00010000 Decimal: 3 16

Decrypting — XOR with the same key

Ciphertext:  00000011 00010000
Key:         01001011 01111001
             ────────────────
Plaintext:   01001000 01101001
             H        i        ← original restored!

Pseudocode

function xor_encrypt(plaintext, key):
    output = []
    for i in range(len(plaintext)):
        output.append(plaintext[i] XOR key[i mod len(key)])
    return output

// Decryption is identical — just call xor_encrypt again with the same key

The Danger of Key Reuse

When the key is shorter than the message, it repeats (note the mod len(key) above). This creates a repeating pattern that an attacker can exploit — it's essentially a Vigenere cipher, which was broken centuries ago.

Why repeating keys are fatal

If we XOR two ciphertexts encrypted with the same key:

C₁ ⊕ C₂ = (P₁ ⊕ K) ⊕ (P₂ ⊕ K) = P₁ ⊕ P₂

The key cancels out! The attacker gets the XOR of two plaintexts, which leaks massive amounts of information — especially if the plaintexts are English text or structured data.

Critical Rule

Never reuse a key with XOR encryption. If two messages are encrypted with the same key, an attacker can XOR the ciphertexts together to eliminate the key entirely, reducing the problem to a simple crib-dragging attack.

The One-Time Pad

What if the key is exactly as long as the message, truly random, and never reused? Then you have a one-time pad (OTP) — the only encryption scheme that is provably unbreakable, proven by Claude Shannon in 1949.

  1. Generate a truly random key

    The key must be the same length as the message, generated from a cryptographically secure random source.

  2. XOR plaintext with key

    Each byte of the message is XORed with the corresponding byte of the key.

  3. Transmit the ciphertext

    The ciphertext can be sent over any public channel — it's information-theoretically secure.

  4. Destroy the key

    The key must never be reused. Both sender and receiver destroy their copy after use.

Why It's Unbreakable

Given any ciphertext, every possible plaintext of the same length is equally likely. There is no computation, no matter how powerful, that can determine which plaintext is correct without the key. This is called perfect secrecy.

The ciphertext reveals nothing

Ciphertext:  7A 3F 91

With key₁ = 1B 5E F4 → Plaintext = "aha" With key₂ = 35 50 FC → Plaintext = "OMG" With key₃ = 2A 5A E0 → Plaintext = "Per"

Every three-letter string is equally valid. Without the key, you learn nothing about the plaintext.

Why We Can't Use the One-Time Pad

If the one-time pad is unbreakable, why don't we use it for everything? Because of a devastating practical problem:

The Key Distribution Problem

The key must be as long as the message. To encrypt a 1 GB file, you need a 1 GB key. And you need to securely deliver that key to the recipient before communication begins. If you had a secure channel to send the key, you could just send the message over that channel instead.

Requirement Why it's hard
Key as long as message Encrypting 1 TB of data requires 1 TB of key material
Truly random Pseudorandom generators are deterministic — not truly random
Never reused You need fresh key material for every single message
Securely distributed Both parties must already share the key — chicken-and-egg problem

The one-time pad has been used in practice for extremely high-value communications — the Moscow--Washington hotline during the Cold War used OTP. But for general use, we need ciphers that work with short, reusable keys.

Stream Ciphers — Practical XOR

A stream cipher solves the key length problem by using a short key to generate a long pseudorandom keystream. The keystream looks random but is entirely determined by the key — so the recipient can regenerate the same stream.

Stream cipher concept

Short key: 0x2B7E1516

 ┌─────────────────┐

Key → Keystream ├→ A7 C3 09 FF 1B 4E 82 D0 ... │ Generator (PRNG) │ └─────────────────┘

Plaintext: 48 65 6C 6C 6F 20 57 6F ... Keystream: A7 C3 09 FF 1B 4E 82 D0 ... XOR ───────────────────── Ciphertext: EF A6 65 93 74 6E D5 BF ...

Modern stream ciphers like ChaCha20 and Salsa20 generate keystreams that are computationally indistinguishable from random — no known attack can tell them apart from true randomness. They're fast, simple, and widely used in protocols like TLS 1.3 and WireGuard.

Real-World Usage

System How it uses XOR / stream ciphers
WireGuard VPN ChaCha20 stream cipher for all data encryption
TLS 1.3 ChaCha20-Poly1305 as one of its cipher suites
Wi-Fi (WPA3) Uses AES-based stream mode (CTR) for data frames
RC4 (deprecated) Once used in SSL/TLS and WEP — broken due to bias in keystream
Cold War hotline True one-time pad — keys physically transported by courier

Key Takeaways

  • XOR is the fundamental building block of modern encryption — it's fast and self-inverse
  • The one-time pad (XOR with a truly random, message-length, never-reused key) is provably unbreakable
  • In practice, OTP is unusable because the key is as large as the message
  • Never reuse a key — XORing two ciphertexts eliminates the key entirely
  • Stream ciphers solve the key length problem by generating a long pseudorandom keystream from a short key

Try It Yourself — XOR Encryption

Enter a plaintext and a key below. The key repeats cyclically to match the plaintext length (like a Vigenere cipher). Watch XOR encrypt and then decrypt the message using the exact same operation.

Encryption — Byte-by-Byte XOR

Ciphertext

Decryption — XOR with the Same Key

Same operation encrypts and decrypts

XOR is its own inverse. Applying the same key a second time perfectly restores the original plaintext — there is no separate decryption algorithm.

Statistics


Stream ciphers encrypt data byte-by-byte, but many applications need to encrypt fixed-size blocks of data with additional structure. That's where block ciphers come in — starting with DES, the cipher that launched modern cryptography.