XOR and the one-time pad
Bit by bit with a key
XOR (⊕, exclusive or) gives 1 when two bits differ and 0 when they are the same. XOR with a 1 flips a bit,
XOR with a 0 keeps it, and the same key applied a second time flips the same bits back: (P ⊕ K) ⊕ K = P,
because K ⊕ K = 0. The one-time pad (OTP) is exactly that: the message XORed with a key that is truly random,
at least as long as the message, secret, and used only once. Claude Shannon proved in 1949 that the ciphertext
then says nothing about the content beyond its length — perfect secrecy. In the toy the key comes from
crypto.getRandomValues, a cryptographic generator: its output cannot be told apart from random in practice,
but formally the guarantee becomes computational rather than unconditional.
One-time pad
Limit 1 KB. We count UTF-8 bytes — “ą” is 2 bytes. The key will be exactly as long.
The message is empty. Type at least one character.
Type a message and draw a key: it will be random and exactly as long as the message.
XOR bit by bit: 0 ⊕ 0 = 0, 0 ⊕ 1 = 1, 1 ⊕ 0 = 1, 1 ⊕ 1 = 0.
The same key twice
Trying every key gains nothing: for every text of the same length there is a key that “decrypts” the ciphertext to it — see for yourself in the toy’s “Break it” mode above. The one-time pad falls to misuse. When one key encrypts two messages, C₁ ⊕ C₂ = (P₁ ⊕ K) ⊕ (P₂ ⊕ K) = P₁ ⊕ P₂ — the key vanishes, and the XOR of two texts gives a great deal away. The attacker guesses a common word, drags it along P₁ ⊕ P₂ (crib dragging), and where the word really stands in one message reads a piece of the other; every byte read also gives away a key byte, K = C ⊕ P. The one-time pad does not protect integrity either: whoever flips a bit of the ciphertext flips the same bit of the plaintext.
A key used twice
Where XOR with a key is used
- VenonaIn early 1942 the Soviet maker of one-time pads printed duplicate key pages; the sources disagree on why. From 1943 American codebreakers looked for cables encrypted with the same pages and read close to 3,000 of them, in whole or in part.Source (Venona)
- Moscow–Washington hotlineFrom 1963 the teleprinter link was protected by ETCRRM one-time tape machines, which work on the one-time pad principle. Each side made its own key tapes, and couriers delivered them to the embassy at the other end.Source (Moscow–Washington hotline)
- CTR mode (NIST SP 800-38A)AES encrypts successive counter blocks and the output is XORed with the plaintext — a one-time pad whose key is stretched by a block cipher. Counters must be distinct across all messages under one key; a repeated counter may expose the blocks that used it.Source (CTR mode (NIST SP 800-38A))
- ChaCha20 (RFC 8439)ChaCha20 expands the key and nonce into a keystream that is XORed with the plaintext. As the RFC states, a repeated nonce gives the same keystream, so the XOR of the ciphertexts equals the XOR of the plaintexts.Source (ChaCha20 (RFC 8439))
- KRACK (2017)Mathy Vanhoef and Frank Piessens showed that reinstalling a key in WPA2 forces a device to reuse nonces, so Wi-Fi encryption reuses keystream and captured packets can be decrypted.Source (KRACK (2017))
Do not build a cipher from bare XOR
Encrypt data with a ready-made AEAD from a library — AES-GCM in WebCrypto or ChaCha20-Poly1305 — with a new nonce for every message (see the AES and modes and AES-GCM and ChaCha20-Poly1305 stations). They are stream ciphers like the one-time pad, but with a short key that can safely be used many times and a tag that detects a changed ciphertext. A real one-time pad only makes sense if you can draw, deliver securely and destroy after use a key as long as all your messages.
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
false,
['encrypt', 'decrypt'],
);
// a new nonce for every message, stored next to the ciphertext
const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt(
{ name: 'AES-GCM', iv },
key,
data,
);Do not encrypt by XOR with a fixed key or a password repeated over and over — every key byte then encrypts
many bytes of text, the same mistake as in “Break it”. Do not repeat a nonce in CTR, GCM or ChaCha20: the
same key and nonce give the same keystream. Do not draw a key with Math.random() (see the
Randomness station).
// ✕ a short key repeated over and over: each of its bytes
// encrypts every fifth byte of the data
const key = new TextEncoder().encode('magic');
const out = data.map((b, i) => b ^ key[i % key.length]);