The Lab
Cryptography

Cipher Playground

Classical ciphers, one-time pads, a 3-rotor Enigma, educational RSA, and a couple of hashes, all running live in the browser. Nothing leaves your machine.

Caesar shift

Vigenère

One-Time Pad (letters)

Add each message letter to a pad letter, mod 26, so A+A=A and B+C=D. Decrypting subtracts the same pad. The pad has to be truly random, at least as long as the message, and used once: reuse it and the secrecy is gone. Generate builds one from the browser's cryptographic RNG; on paper people rolled dice or drew lettered tiles from a bag. Output is grouped in fives, the traditional pad format. HELLO under pad XMCKL gives EQNVZ.

One-Time Pad (bytes / XOR)

The same idea in bytes: combine each message byte with a pad byte using XOR, the exclusive-or operation, which works on any text or data, not just A to Z. XOR is its own inverse, so the same pad decodes it. The pad is random bytes, at least as long as the message, used once. Generate fills it from the browser's cryptographic RNG, sized to the message.
These two generators are here to show how a pad works, not to guard real secrets. A pad you build and read on a web page, then send over the internet, has already broken the only-two-copies rule, and anything typed into a browser can be logged or cached. For a message that genuinely needs a one-time pad, make it on paper away from the computer, the way described below, and never let the pad touch a network.

Make a pad by hand

1. Get real randomness

Do not make the letters up in your head. People are terrible at being random, and a guessable pad is a broken pad. Use a physical source:

  • Lettered slips or tiles. Write A through Z on 26 slips, drop them in a bag, and draw one. Write it down, put it back, shake the bag, and draw again. Drawing with replacement gives every letter an equal chance.
  • Dice. Roll two ten-sided dice to read a two-digit number from 00 to 99. Let 00 be A, 01 be B, on up to 25 for Z. Reroll anything 26 or higher. Rerolling the high numbers is what keeps every letter equally likely, so do not take a shortcut like dividing by 26.

Keep going until the pad is at least as long as the message you want to send.

2. Make exactly two copies

One for you, one for the person you are writing to, and no others. Number the pages so you both use them in the same order.

3. Encrypt by hand (letters)

Number the alphabet A=0 through Z=25. Write the message, and under each letter write the next letter from the pad. Add the two numbers, and if the total is 26 or more, subtract 26. Turn the result back into a letter.

So H(7) + X(23) = 30, minus 26 is 4, which is E. Do the whole message that way and you get the ciphertext. Write it out in groups of five letters, the traditional format, and send it.

4. Decrypt by hand

Line the same pad letters up under the ciphertext and subtract instead of add. If a result goes negative, add 26. E(4) - X(23) = -19, plus 26 is 7, which is H. The plaintext comes back.

Doing it in bytes

For the XOR version, each pad byte is eight random bits. Flip a coin eight times, heads for 1 and tails for 0, and that is one byte. To combine a message bit with a pad bit, write 1 when the two bits differ and 0 when they match. This is slow on paper, so the byte pad is really a job for a computer, but the rule is identical.

The rules that make it unbreakable

  • The pad is truly random.
  • It is at least as long as the message.
  • Only two copies exist, one with each person.
  • Each page is used once, then destroyed.

Break any one of those and it stops being a one-time pad and becomes just another cipher you can attack. Get all four right and there is no amount of computing power that can read the message, because every possible plaintext of that length is an equally good fit for the ciphertext.

XOR

The key repeats across the text and each byte is XOR'd. The same key turns the hex back into text.

RC4

A stream cipher: the key seeds a keystream that is XOR'd with the data. Educational only, RC4 is broken and unsafe for real use.

Atbash

Mirrors the alphabet, A to Z, B to Y, and so on. It is its own inverse, so the same operation decodes it.

ROT13

A Caesar shift of 13. Because 13 is half of 26, applying it twice returns the original.

Affine

E(x) = (a·x + b) mod 26. The multiplier a has to be coprime with 26 or the mapping is not reversible, so only 12 values of a are allowed.

Rail Fence

A transposition: write the text in a zigzag down and up the rails, then read it off row by row. It reorders the letters instead of substituting them.

Enigma

A 3-rotor Enigma I (historical rotor wirings I to V, reflectors B and C). Encryption and decryption are the same operation, so match the settings and type. Rotors I II III, rings AAA, position AAA, reflector B, no plugboard: typing AAAAA gives BDZGO.

RSA (educational)

Pick two primes and the tool derives n = p·q, φ(n), and the private exponent d. Each character is encrypted as c = me mod n and decrypted with d. Small primes only, so this shows the math, it is not secure. n has to exceed 255 to fit a byte.

CRC-32

A checksum, not a cipher: the standard CRC-32 (polynomial $EDB88320) used in zip, PNG, and Ethernet. The string 123456789 checks to $CBF43926.

SHA-256

A cryptographic hash, computed by the browser's WebCrypto. One-way, so there is nothing to decode. SHA-256 of abc starts ba7816bf and ends f20015ad.