Clever Quest Lab ← Labs

Vernam Cipher

Encrypt with a random key as long as the message, then test how random each part is.

Encrypt

Key (random bytes)

        
Ciphertext = plaintext XOR key

        

Any characters work: the text becomes UTF-8 bytes, and every change gets a fresh random key of exactly the same length.

Decrypt

Recovered plaintext

        

XOR step by step

P = plaintext bit, K = key bit, C = ciphertext bit. XOR gives 1 where the two bits differ and 0 where they are equal; doing it again with the same key gives the plaintext back.

Randomness check

Original

Key

Ciphertext

Entropy is the average surprise per symbol: 8 bits per byte (4.70 per letter) means every value is equally likely; it cannot exceed log2 of the number of symbols counted. Index of coincidence is the chance that two positions picked at random hold the same value: about 0.0039 for random bytes, 0.038 for random letters and 0.066 for English letters; the number after × scales it so that 1.0 means random. Chi-square p asks whether the histogram could come from perfectly even data; below 0.01 it clearly does not.

Runs on your device. Nothing you type or record is uploaded. Keeps working without a connection while the page is open. Works with touch. Saves only the open mode, the hex or Base64 choice, the letter options and the key-reuse demo on this device.

Added

How to use it

Pick a mode at the top. Text works on the bytes of anything you type or paste, so capitals, digits, accents, symbols and emoji all work. Letters is the pencil-and-paper version with A to Z only. File encrypts any file, Image turns a picture into colour noise, and Key reuse shows what goes wrong when one key is used twice. The Randomness check below compares the original data, the key and the ciphertext.

Controls

How it works

The Vernam cipher combines each bit of the message P with a bit of the key K using exclusive or (XOR, ⊕), which gives 1 when the two bits differ and 0 when they are equal:

C = P ⊕ K     P = C ⊕ K     (letters: C = (P + K) mod 26,  P = (C − K) mod 26)

Decryption repeats the operation, because K ⊕ K = 0. In the Letters mode A = 0 … Z = 25, and key letters are drawn without bias: random bytes from 234 to 255 are thrown away, so each letter has a chance of exactly 1/26.

In 1917 Gilbert Vernam built a teleprinter machine that combined each 5-bit character with a character from a punched paper key tape. Joseph Mauborgne saw that the system becomes unbreakable when the tape is random and never used again, and in 1949 Claude Shannon proved it: seeing the ciphertext does not make any message of that length more or less likely. This perfect secrecy holds only if the key is truly random, at least as long as the message, kept secret and never reused. The page uses the browser’s crypto.getRandomValues, a cryptographically secure pseudorandom generator seeded by the operating system, not a physical noise source. Everything runs on your device: nothing is uploaded and nothing you type is saved.

An encrypted file starts with a small hidden header (a fixed marker and the file name) encrypted together with the data, so decryption restores the name and notices a wrong key. Pictures are encrypted in red, green and blue, with transparency flattened onto white, and must be saved as PNG: JPEG changes colour values slightly, and every changed bit of the ciphertext becomes a wrong bit of the picture.

The Randomness check counts how often each value occurs and computes the entropy H in bits per symbol and the index of coincidence IC:

H = −Σ pi log2 pi     IC = Σ ni(ni − 1) / (N(N − 1))

Here ni is the count and pi the share of value i among N symbols. IC is near 1/256 for random bytes and 1/26 for random letters, while English letters give about 0.066 because E, T and A are common. The chi-square test compares the counts with a flat histogram and needs at least five expected counts per value.

Things to try

Frequently asked questions

Is the one-time pad really unbreakable?

Yes, when all four conditions hold: the ciphertext then reveals only the message length. In practice key handling fails, because making, sharing and destroying long keys safely is hard.

Why is the entropy of my random key below 8 bits?

Because the sample is small. In 30 bytes at most 30 different values can appear, so the measured entropy cannot exceed log2 30 ≈ 4.9 bits. The index of coincidence has no such bias, so it is the better test for short data.

What happens if a key is used twice?

The XOR of the two ciphertexts equals the XOR of the two messages, and a guessed word in one message reveals the other at that spot. In the 1940s, duplicated pad pages let American analysts read part of Soviet intelligence traffic in the Venona project.

Why not use it for everything?

The key must be as long as everything you send and reach the other person secretly in advance. Modern ciphers use short keys; they are secure in practice, though not in Shannon’s perfect sense.

Sources