Cabinet Noir>Learn / Rail fence

Not sure which cipher? Identify it
learning.railFence.description
Typetransposition cipher
Breaking ittrivial to break
EraAmerican Civil War
NameFence-line ciphers; the scytale as the ancient forerunner
Modesencrypt · decrypt · cryptoanalysis

Learn

Rail fence

How the Rail fence cipher works

The idea in one sentence

Rail fence is the simplest Transposition to picture: the letters ride a fence on the diagonal, bouncing between the top and bottom rail, and the ciphertext is the rails read off one after another, top to bottom.

The recipe

  1. Choose a number of rails, say 3, and write the plaintext diagonally: A on rail 1, T on rail 2, T on rail 3, A back on rail 2, and so on — the Zigzag.

  2. With ATTACKATDAWN the fence fills up as rail 1: A · A · A, rail 2: T · C · T · T · A · W, rail 3: T · K · D · N.

  3. Read the rails top to bottom: AAA, then TCTTAW, then KDN.

  4. The ciphertext is AAATCTTAWKDN. Decryption is the same walk in reverse: count how many letters each rail owns, slice the ciphertext accordingly, and ride the fence again.

A worked example

{{example:railFence-basic}}

Each row follows one plaintext letter: which rail it sits on, and which position of the ciphertext it ends up at. Notice how the first three letters of the ciphertext are just the top rail — the fence leaks its whole shape in plain sight.

The offset variant

The classic fence always starts on the top rail — the first ciphertext letter is always the first plaintext letter, a gift to the attacker. The offset moves the starting rail: with 3 rails and offset 1, the zigzag begins on the middle rail heading down. Same fence, one more thing to try.

Why it is the on-ramp, not the fortress

The key is two small numbers. Even the biggest setting here — ten rails — is ten times a handful of tries, and each try is checked by whether the text reads. That makes rail fence the perfect first transposition to break by hand, and the guided cracker turns it into a single slider.

Rail fence: guides

Guide 1 — your first zigzag

  1. Open the tool, leave the rails at 3 and type MEET ME AT NOON. Watch the Zigzag draw itself: the first letters fall down the diagonal, then bounce.

  2. Compare the ciphertext with the fence: it is the rails concatenated, top to bottom — nothing else.

  3. Switch to decrypt and paste the cryptogram back. The same fence rebuilds the message.

Guide 2 — the offset

  1. Set the offset to 1 (start on the middle rail) and encrypt the same message again.

  2. Compare the two cryptograms: the offset version no longer starts with the message's first letter — the classic fence's most obvious tell is gone.

  3. Try the maximum offset (start on the bottom rail): the zigzag runs upward first. Every setting is just a different way of walking the same fence.

Guide 3 — from rail fence to columnar

  1. Encrypt a message on 3 rails and look closely at the rails: each one is a "column" of a zigzag-shaped grid.

  2. Now open the Columnar tool, encrypt the same message with a 3-letter keyword, and compare: the columnar grid is the same idea with straight columns instead of a diagonal.

  3. That is why both ciphers fall to the same weapon — rebuilding the order — and why the columnar cracker's step two is the rail fence slider's big brother.

Breaking the Rail fence

Rail fence is a Transposition with a two-number key: the rail count and the offset. There is nothing to analyse slowly — the whole attack is trying settings, and the wizard on the Cryptoanalysis tab compresses it into one slider.

{{challenge:railFence-demo}}

The flaw: a key space you can hold in your head

At most ten rails and a handful of offsets: a few dozen settings in total, each testable at a glance. No statistics course required — the only question is which setting makes the rows read like English. That is why rail fence is the on-ramp to transposition breaking: the experience of sliding until it reads is the same skill the Columnar cracker needs, minus the hard part.

The wizard, step by step

  1. Slide. Move the rail-count slider and the offset slider. The fence re-draws with every notch, and the plaintext candidate updates live — wrong settings show scrambled rows, the right one shows words.

  2. Watch the meter. Each setting is scored by how English-like its plaintext looks (Frequency analysis's digraph cousin: TH, HE and IN score high, nonsense pairs score nothing). The meter is a measuring stick, not an oracle — on very short texts a wrong setting can outscore the right one, and your eye is the final judge.

  3. Done. "Use this result" carries the rails and offset to the Tool tab, which decrypts the message. The setting is the whole key.

The tool never slides for you. The easy preset marks where the meter peaks — a pointer, not a click — because the finding is the lesson.

Where it stops working

More rails make the picture messier, not the cipher stronger: the key space grows linearly while the reading test stays instant. Double transposition and the columnar grid are the natural next steps — both break the "try every setting" shortcut, and both wait in the later chapters of this ladder.

Glossary

Plaintext — The original readable message before encryption.
Ciphertext — The scrambled message produced by encryption.
Key — The secret that controls how the text is scrambled.
Transposition — Changing the order of symbols without changing the symbols themselves.
Rail fence — A transposition where the letters ride a zigzag across a number of rails, and the rails are read off one after another.
Zigzag — The diagonal bounce between the top and bottom rail that gives the rail fence its shape — and its name.