← ALL INSIGHTS
CONCEPTS · ENGINEERING · 10 MIN READ

XOR in Cryptography: The Reagent That Cancels Itself Out.

A red reagent bottle K poured into three beakers: clear message, dark ciphertext, clear message again

Picture a chemistry lab. On the top shelf sits a small red bottle labelled K.

Pour it into any clear solution and the solution turns murky. Nobody can tell what was in there. Pour the same bottle in a second time and something odd happens: the murk vanishes, and the original solution is back, exactly as it was. Not roughly. Exactly.

No real chemical behaves like this, which is a shame, because it would make a fantastic party trick. But there is an operation in your computer that does, and it holds up most of modern encryption. It is called XOR.

TL;DR: XOR is a reagent that cancels itself out. Pour it once and your data becomes unreadable; pour it again and it is gone. That one property makes encryption possible, and the same property is behind the three classic accidents below.

The reagent that cancels itself

XOR works on bits, the 0s and 1s, two at a time. It asks one question: are you two different? If yes, the answer is 1. If no, 0.

That sounds dull until you notice what it means. Pour a bit of reagent into a bit of solution and it flips, or doesn’t. Pour the same bit of reagent in again, and it flips back. Every time. Two identical pours always cancel.

Pour twice and the reagent vanishesA beaker holds six bits of a message. A red drop of reagent K falls in: the liquid turns dark and the bits where K is 1 flip. A second drop falls: the same bits flip back and the original message returns.KMESSAGECIPHERTEXTMESSAGE · K GONE
FIG·01 — FIRST POUR FLIPS THE BITS, SECOND POUR FLIPS THEM BACK

In code, the whole lab fits in one small function. I have called it pour, because that is exactly what it does:

import secrets

def pour(solution: bytes, reagent: bytes) -> bytes:
    """XOR, drop by drop. Pour the same reagent twice and it vanishes."""
    if len(solution) != len(reagent):
        raise ValueError("the reagent must match the solution, drop for drop")
    return bytes(s ^ r for s, r in zip(solution, reagent))

solution = b"PAY 100 TO ALICE"
reagent = secrets.token_bytes(len(solution))  # random, as long as the message, used once

cloudy = pour(solution, reagent)  # first pour: unreadable
clear = pour(cloudy, reagent)     # second pour: the reagent is gone
assert clear == solution
print(cloudy.hex())
print(clear)

Run it and the first print is gibberish, a fresh batch of it every time. The second is your message, home safe. Encryption and decryption are the same move: pour, then pour again.

The bottle every cipher keeps on the bench

Here is the part that made cryptographers fall in love. If the reagent is truly random, as long as the message, and used only once, the murky solution tells you nothing. Not “very little”. Nothing. Gilbert Vernam patented this in 1919, and in 1949 Claude Shannon proved it in his paper on secrecy systems. It is called the one-time pad, and it is the only cipher with a mathematical guarantee of that kind.

The catch is the size of the bottle. To encrypt a 4 GB backup you first need a fresh 4 GB of reagent, delivered secretly to the other side, then poured down the drain forever. Most labs cannot run on that budget.

So modern ciphers like AES and ChaCha20 cheat, politely. They start from a small key and mix up a long stream of random-looking reagent from it, called a keystream. Then they pour. Underneath the impressive names, “encrypted with AES-GCM” means: XORed with something extremely hard to guess.

A small key grows a long keystreamA small key, marked in red, produces a long row of keystream bits one tile at a time. Each keystream bit is XORed with the message bit below it, giving the ciphertext row at the bottom.KEYKEYSTREAM ⊕ MESSAGE = CIPHERTEXT
FIG·02 — ONE SMALL KEY, MIXED INTO A LONG KEYSTREAM

Which also means every lab accident XOR allows comes along for the ride. There are three worth knowing by heart.

Accident 1: one bottle, two beakers

You pour bottle K into beaker A. Later, in a hurry, you pour the same bottle K into beaker B. Both look nicely murky. Then someone mixes the two beakers together.

The reagent meets itself, and cancels. What is left is no longer protected at all: it is just solution A mixed with solution B. Guess one, and the other separates out on its own.

One bottle, two beakersThe same reagent K is poured into beaker A and beaker B. When the two beakers are mixed, K meets itself and cancels, leaving A XOR B: the two messages, no longer protected.KA ⊕ BA + KB + K
FIG·03 — SAME REAGENT, TWO BEAKERS: MIX THEM AND K CANCELS OUT
beaker_a = pour(b"PAY 100 TO ALICE", reagent)
beaker_b = pour(b"PAY 900 TO MALLO", reagent)  # same bottle, second beaker

mixed = pour(beaker_a, beaker_b)  # the reagent meets itself and vanishes
print(pour(mixed, b"PAY 100 TO ALICE"))  # guess one, read the other: b'PAY 900 TO MALLO'

This is not a classroom hypothetical. In the 1940s, Soviet intelligence reused some of its one-time pad pages, and American codebreakers in the Venona project spent decades reading messages that were meant to be unbreakable.

And it did not stay in the 1940s. Modern ciphers take a nonce along with the key, a number that must be used once, so that every message gets fresh reagent. Reuse the nonce and you have reused the bottle. In 2016, researchers scanning the internet found 184 HTTPS servers repeating nonces, including large corporations, financial institutions and a credit card company. Same accident, much nicer lab coats.

Accident 2: a stranger’s drop

Your beaker is murky, so nobody can read it. Good. But nobody sealed it either. A stranger walks past and adds a single drop of their own.

Later you pour bottle K to clear it. The reagent leaves, as always. Their drop does not. It lands in your message, exactly where they put it, and nothing looks wrong. In plain AES-CTR or ChaCha20, flipping a bit in the ciphertext flips the same bit in the message, and the attacker never needs the key:

import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

key, nonce = os.urandom(32), os.urandom(16)
aes_ctr = Cipher(algorithms.AES(key), modes.CTR(nonce))

def encrypt(data: bytes) -> bytes:
    enc = aes_ctr.encryptor()
    return enc.update(data) + enc.finalize()

def decrypt(data: bytes) -> bytes:
    dec = aes_ctr.decryptor()
    return dec.update(data) + dec.finalize()

ct = bytearray(encrypt(b"PAY 100 TO ALICE"))
ct[4] ^= ord("1") ^ ord("9")  # attacker flips bits; never sees the key
print(decrypt(bytes(ct)))      # b'PAY 900 TO ALICE', and no error
A stranger’s drop survivesAn unsealed beaker holds dark ciphertext. A stranger’s red drop falls in and settles. Bottle K pours to clear the liquid: the reagent leaves but the red drop stays, and the message now reads 900 instead of 100.KNO SEAL100 → 900
FIG·04 — THE REAGENT LEAVES, THE STRANGER’S DROP STAYS

The receiver sees a clean, valid instruction and pays 900. Encryption hides what a message says. It never promised nobody touched it. Those are two different jobs, like a lock and a tamper seal, and plain CTR only fitted the lock.

The fix is to seal the beaker. Authenticated encryption, such as AES-GCM or ChaCha20-Poly1305, adds a tag that is checked before anything is poured back. Try the same drop and the seal breaks loudly:

import os
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

aead = AESGCM(AESGCM.generate_key(bit_length=256))
nonce = os.urandom(12)  # 96 bits; never reuse with this key
ct = bytearray(aead.encrypt(nonce, b"PAY 100 TO ALICE", b"invoice-42"))

ct[4] ^= ord("1") ^ ord("9")  # the same bit flip as before
try:
    aead.decrypt(nonce, bytes(ct), b"invoice-42")
except InvalidTag:
    print("rejected: ciphertext was modified")

The b"invoice-42" part is a label glued to the outside of the beaker: checked, but not encrypted. It ties this ciphertext to the record it belongs to, so nobody can swap beakers between orders.

Accident 3: the impatient inspector

Last one, and it is sneaky because the code looks fine. Your lab checks every incoming sample against a reference, drop by drop. The inspector is impatient: the moment a drop differs, they stop and shout “reject”.

Now anyone with a stopwatch can learn something. A sample rejected instantly was wrong from the first drop. One that took slightly longer got the first drop right. Time enough samples and you can rebuild the reference one drop at a time. That is what == does when you compare a webhook signature or an API token.

The impatient inspectorA reference row and a sample row of bits. A scan line compares them left to right and stops at the first mismatch, marked in red. Below, four bars show the check taking longer as more leading bits are correct, which leaks the secret.FIRST MISMATCHTIME PER GUESS
FIG·05 — STOPPING EARLY LEAKS HOW MANY DROPS WERE RIGHT

The fix uses XOR again, on our side this time. XOR every pair of drops, keep a running note of any difference, and only look at the note at the very end. The inspector now takes the same time whether the first drop or the last one is wrong:

import hmac

def xor_equal(a: bytes, b: bytes) -> bool:
    # Shows the idea only: Python itself gives no timing guarantee.
    if len(a) != len(b):
        return False
    diff = 0
    for x, y in zip(a, b):
        diff |= x ^ y  # any differing bit sticks; no early exit
    return diff == 0

def signature_ok(expected: bytes, received: bytes) -> bool:
    return hmac.compare_digest(expected, received)  # use this in real code

print(xor_equal(b"abc", b"abc"), xor_equal(b"abc", b"abd"), signature_ok(b"abc", b"abc"))

The loop shows the idea; Python itself makes no timing promises. In real code, call hmac.compare_digest, which exists for exactly this. Every serious language ships an equivalent.

Lab rules

  • Never brew your own reagent. Use AES-GCM or ChaCha20-Poly1305 from a maintained library, and never ship homemade “XOR encryption”.
  • One bottle, one beaker. Never reuse a nonce with the same key. A random 12-byte nonce for AES-GCM is fine; a counter is fine if you can prove it never repeats, even across restarts.
  • Seal every beaker. If you are not using authenticated encryption, you are not done.
  • A broken seal is an incident, not a glitch. Log it, alert on it, and never fall back to an unsealed path.
  • Send the impatient inspector home. Compare secrets with a constant-time function.
  • Keep a test that adds one stranger’s drop to a ciphertext and expects it to be rejected.

In the interview

What does XOR do, and why can the same operation both encrypt and decrypt?

A strong answer: XOR outputs 1 where two bits differ and 0 where they match. Anything XORed with itself is 0, and anything XORed with 0 is unchanged, so (A ⊕ K) ⊕ K = A. The second pour cancels the first. Bonus points for saying it is cheap and has no carries between bits.

Why is the one-time pad unbreakable, and why don’t we use it everywhere?

A strong answer: with a truly random key as long as the message and used once, every possible message of that length is equally likely given the ciphertext, which is what Shannon proved in 1949. We don’t use it because you need as much secret key as data, delivered securely and never reused. Real ciphers grow a keystream from a short key instead.

AES-GCM is secure. So what goes wrong if you reuse a nonce?

A strong answer: the same key and nonce produce the same keystream, so XORing the two ciphertexts cancels it and leaves the two messages XORed together, the two-beaker accident. With GCM it also weakens the seal, so an attacker can start forging messages. The fix: a fresh random 96-bit nonce per message, or a counter that provably never repeats.

XOR is the most obedient chemical in the lab. It does exactly what you pour it for, every single time. Which is why, in cryptography, almost every disaster starts with someone pouring it twice.

LEARNING PATH · CRYPTOGRAPHY BASICS · 1 OF 1 You finished this path. Explore more concepts
FILED UNDER CONCEPTS ENGINEERING
MORE CONCEPTS
SEE ALL CONCEPTS →
WE'RE HIRING

Liked taking this apart?
This is the work we do.

Interns and graduates welcome. Tell us this post brought you here.

See open roles →