applets.

XOR Secrets

← All apps

About XOR Secrets

XOR ("exclusive or") compares two bits: the answer is 1 if they differ and 0 if they are the same. Do it to every bit of a message with the bits of a key and the message turns to nonsense; do it again with the same key and the message comes back, because each bit has been flipped twice or not at all.

MIX: type a message and a key. Each byte is a column of eight squares (filled = 1, top = the 128s), with the message in black, the key in red, the cipher text in violet and, underneath, the cipher text XORed with the key again. Point at a column (or use ←/→ on the picture) to read it; click to keep it. A short key repeats, so the same letter at the same point in the key always gives the same cipher byte, and the page marks those. ONE-TIME PAD instead makes a random key as long as the message.

ONE-TIME PAD shows why a pad used once can't be broken. Type what you would like the intercepted cipher text to say, and the page shows the key that makes it say exactly that.

TWO-TIME PAD is the same pad used for two messages. XOR the two cipher texts and the pad cancels out, leaving the two messages XORed together. Guess a word one of them might contain (a "crib", such as " the "), slide it along, and where the guess is right the other message shows through. KEEP writes it into the worksheet.

CRACK A SHORT KEY breaks a repeating key without trying every possible key: it measures which key length fits best, then picks each key byte that makes the text read most like English.

THE STORY

In 1917 Gilbert Vernam, an engineer at the American telephone company, found a way to make teleprinter messages secret. They travelled as paper tape punched in a five-hole code. Vernam added a second, key tape and combined the two hole by hole: where the holes matched, no hole; where they differed, a hole. That is XOR. At the far end the same key tape, combined again, gave back the message. His patent was granted in 1919. This page uses eight-bit bytes, as computers do, rather than his five holes.

Vernam's key tape was a loop, so it repeated. Joseph Mauborgne, an officer in the US Army Signal Corps, saw that a repeating key could be broken, and that the key had to be random, as long as the message and never used again: the one-time pad. (In 2011 the historian Steven Bellovin found that a banker, Frank Miller, had described the same idea for telegraph codes in 1882.)

Claude Shannon proved it unbreakable, in a secret report at Bell Labs in 1945, published in 1949 as "Communication Theory of Secrecy Systems": with a truly random pad used once, the cipher text says nothing about the message except its length, and no system can do that with less key than message.

Pads used twice are another story. In August 1941 a German operator sent a long teleprinter message, was asked to repeat it, and sent it again with the same key, changing a few words. At Bletchley Park John Tiltman worked out both messages and the key; from that key Bill Tutte worked out how the machine, which the codebreakers called Tunny, was built. And in 1942, under wartime pressure, the Soviet office that made one-time pads printed some pages twice. From 1943 the US Army's codebreakers studied Soviet cables, and from 1946 they read parts of a few thousand messages, which exposed spies in the atomic bomb project and elsewhere. The work, later called Venona, went on until 1980 and was made public in 1995. Those pads added digits rather than XORing bits, but the mistake, and the way in, were the same.

WHERE IT CAME FROM

This app grew from two Python scripts. XOR_cipher.py XORs every character with a single number from 1 to 255: a key one byte long, which CRACK A SHORT KEY breaks at once (its first example uses the script's own test key, 67). EncryptionTool.py uses no key at all: it adds 42 to each character's code, multiplies by π and takes away 449, so anyone with the script can undo it. Things they get wrong:

HOW IT WORKS

Text is turned into bytes the standard way (UTF-8: one byte for plain letters, two to four for others, which is why "é" takes two columns). The pads you make come from the browser's cryptographic random generator. The examples' pads are made by a fixed pseudo-random formula so that they are the same on every visit; real pads must never be made that way.

Two-time pad: if C1 = M1 ⊕ P and C2 = M2 ⊕ P, then C1 ⊕ C2 = M1 ⊕ M2, with no pad left in it. A right guess G for part of M1 gives that part of M2 as G ⊕ C1 ⊕ C2. Fragments made only of letters and spaces, reading like English, are shown dark.

Cracking a short key: write the cipher text in rows as long as the key, and every column was mixed with one key byte. That only swaps byte values round, so a column keeps the lumpy letter counts of English: two bytes picked from it match far more often (about 7 times in 100) than in random bytes (under 1 in 100). This "index of coincidence" is the chart; the right length, and its multiples, stand out. For each column the page then tries all 256 key bytes and keeps the one that makes it read most like English (lots of spaces and e, t, a; no control codes), and picks the shortest key length that reads as well as any longer one.

Nothing you type leaves this tab or is stored. The address only remembers which tab and example are showing.

FURTHER WATCHING AND READING

Cipher hex

Base64

XOR again

Key

The XOR rule
message bit0011
key bit0101
cipher bit0110

A key bit of 1 flips the message bit; 0 leaves it alone. Flip twice and you are back where you started, so the same key locks and unlocks.

Try this