Home › Ciphers › Rail Fence Cipher
Rail Fence Cipher
The rail fence is transposition at its most visual: write the message in a zigzag across a set of horizontal rails — down, down, up, up — then read each rail straight across to form the ciphertext. Named for a fence's zigzag rails and used in the American Civil War, it is the transposition every textbook teaches first, because the whole mechanism fits in one picture. This tool adds the control most simple versions omit: the offset, the rail the zigzag starts on, without which whole families of published examples refuse to decode. It opens on the literature's standard example, so you can watch the fence being built.
The rail fence cipher is a transposition: letters are dealt one by one onto rails in a bouncing zigzag (down to the bottom rail, back up to the top), and the ciphertext is the rails read off in order, top to bottom. No letter changes — only positions move. The key is the rail count, plus the starting offset.
Also called the zigzag cipher. Period of the zigzag: 2 × (rails − 1) letters.
Encode or decode — the textbook example is loaded
Rail 1: WECRLTE · Rail 2: ERDSOEEFEAOC · Rail 3: AIVDEN
Everything runs in your browser — nothing is uploaded.
Down, down, up, up — then read the rails
Letters are dealt one by one onto rails following a bouncing path: rail 0, 1, 2, …, up to the last rail, then back down, the period being 2 × (rails − 1). The ciphertext is the rails concatenated: the top rail's letters in order, then the next rail's, down to the bottom. Decoding deals the ciphertext back into rails of the correct lengths — the lengths are fixed by the message length, the rail count and the offset — and re-reads the zigzag. Nothing is substituted and nothing is padded: the ciphertext has exactly the plaintext's letters, in a new order, which is why the letter frequencies of a rail-fence ciphertext are perfect English frequencies wearing a disguise of position.
The offset shifts the starting rail: with offset 1 the first letter lands on the second rail and the whole pattern phase-shifts, which matters because puzzle sources differ in their default phase. If a found example won't decode at offset 0, the offset control is the fix — the same sentence on 3 rails at offset 1 gives RSEFACWAEICVRDLETNEEDOEEO, a completely different ciphertext from the same fence. Rail counts here run 2 to 12: beyond a dozen rails the zigzag is a sparse columnar cipher in disguise and should be solved as one.
A fence from the telegraph age
The rail fence belongs to the nineteenth century's working cryptography. It is documented in American Civil War practice, when short field messages moved by telegraph and a system that needed no equipment, no tables and no shared book — only an agreed rail count — had obvious appeal; both Union and Confederate operators worked in a world of simple transpositions and word codes. The name is the picture: the zigzag of the writing resembles the split-rail fences of the American countryside, rails laid in a zigzag because they needed no posts. The system has no single inventor to name — it is a folk construction, the simplest possible regular route for letters to travel — and textbooks have taught it ever since as the first transposition, the one whose entire mechanism a student can hold in a single glance. Its ancient cousin is the Spartan scytale, a different geometry with the same soul: positions move, letters do not.
The fence, and the period table
| Rail 1 (top) | W · · · E · · · C · · · R · · · L · · · T · · · E |
|---|---|
| Rail 2 | · E · R · D · S · O · E · E · F · E · A · O · C · |
| Rail 3 (bottom) | · · A · · · I · · · V · · · D · · · E · · · N · · |
The worked example drawn as a fence: read the plaintext down the zigzag — W, E, A, R, E… — and read the ciphertext along the rails. The zigzag's period grows with the rail count:
| Rails | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|
| Period (letters per full zigzag) | 2 | 4 | 6 | 8 | 10 |
The standard published vector, step by step
Example 1 — WE ARE DISCOVERED FLEE AT ONCE, 3 rails, offset 0
The standard published vector for this cipher — the example Wikipedia's transposition article prints — reproduced by the tool exactly. Picture the fence: W starts rail 0; E drops to rail 1; A to rail 2; R bounces back to rail 1; E returns to rail 0 — W E A R E laying the first V of the zigzag. When the writing is done, rail 0 holds WECRLTE, rail 1 holds ERDSOEEFEAOC, rail 2 holds AIVDEN — read them off in order and the ciphertext assembles: WECRLTE + ERDSOEEFEAOC + AIVDEN = WECRLTEERDSOEEFEAOCAIVDEN. This example is the arbiter between implementations: offset and bounce conventions differ across websites, and only one convention reproduces the textbook string. If your source's example differs, it is using a phase-shifted fence — set the offset to match.
Example 2 — the same sentence at offset 1
Change nothing but the starting rail and every letter's rail assignment changes: the fence is the same, the phase is different, the ciphertext shares not one position with Example 1's. That is why the offset is key material and not a cosmetic setting — and why a rail-fence ciphertext that resists decoding at offset 0 deserves a walk through the offsets before the rail count is blamed.
A key space you can search by hand
The fence's weakness is the whole transposition family's weakness in miniature: it preserves the letter multiset perfectly and moves letters by small, regular strides. Two structural leaks follow. First, the letters of the top rail are sampled from the plaintext at perfectly regular intervals (every 2 × (rails − 1) letters), so the ciphertext's opening chunk is a strided slice of English — readable patterns survive in it. Second, the key space is tiny: a dozen rail counts times a dozen offsets is 144 fences, a hand-searchable space even in 1863 and a trivial one now. The transposition solver on this site brute-forces every rail count and offset and ranks the outcomes by how English they read, before it even starts on columnar orders.
The strided leak is worth seeing once on the worked example. On 3 rails the period is 4, so the top rail collects every fourth letter of the plaintext — positions 1, 5, 9, 13… — and the ciphertext simply hands those letters to the analyst as its opening chunk, WECRLTE, in order. The middle rail likewise holds two interleaved strides. An analyst who suspects a fence does not even need the full brute force: fixing a candidate rail count fixes exactly which ciphertext positions belong to which stride of the plaintext, and English digraphs that were separated by the fence can be tested stride by stride. This is why layered use — fence over substitution — was the historical instinct: the substitution breaks the letter identities the strides would otherwise betray, and the fence breaks the positions the frequency count would otherwise use.
- Security status: none against search — 144 candidate fences at most, each testable in milliseconds.
- What gives it away: perfect English letter frequencies in a text that reads as nonsense.
- The working order by hand: rails first (the count is usually known or guessable), then walk the offset from 0 upward.
The fence and its family
| Relative | How it differs | Solve it with |
|---|---|---|
| Redefence | The ACA's keyed fence: a keyword sets the rail count and the order the rails are read off, plus the offset | The Redefence page |
| Scytale | The ancient device form: write along a rod, read around it — a grid read-off, not a zigzag | The Scytale page |
| Columnar transposition | Letters fill a keyword-width grid and columns are read in keyword order — a far larger key space | The Columnar page |
| Two-rail fence | The degenerate case: a simple even/odd split of the letter stream — the weakest fence there is | The tool above, rails = 2 (HELLO becomes HLOEL) |
The first transposition everyone meets
The fence endures because it is a perfect teaching object — its picture explains transposition in ten seconds — and because, layered over a substitution, small regular moves complicate a solver's life more than they have any right to. It is a staple of puzzle hunts, geocache stages and CTF warm-ups, where the setter's question is never "can you break a fence?" but "can you recognise one?" — the tell being a ciphertext with flawless English letter frequencies and no readable words. Alone, though, it is a fence, not a wall: its historical users relied on the enemy not intercepting the message at all, and on the system being unknown, far more than on the zigzag itself.
Build a fence by hand
Practice 1 — encode MEET AT THE PARK on 4 rails, offset 0
Thirteen letters on a zigzag of period 6: the rails fill unevenly — the middle rails of a fence always carry more than the edges.
Reveal the answer
MTKETHREAEATP — rail 1: MTK · rail 2: ETHR · rail 3: EAEA · rail 4: TP.
Practice 2 — decode MTKETHREAEATP on 4 rails
Deal the ciphertext back into rails of 3, 4, 4 and 2 letters, then read the zigzag.
Reveal the answer
MEETATTHEPARK — MEET AT THE PARK, spaces stripped as always.
Frequently asked questions
How many rails should I use?
For the classic picture, 3. More rails spread the message thinner and look more scrambled, but the family resemblance is unchanged and the solver tries them all anyway. The meaningful choice is usually between 2 (a simple even/odd split — the weakest fence) and 3–5 for puzzles; beyond about a dozen rails you have reinvented a sparse columnar grid.
What does the offset actually change?
The phase of the zigzag: offset k starts the first letter on rail k, counting down first. The rail lengths change, the ciphertext changes entirely, and decoding must use the same offset to invert. It defaults to 0, the textbook convention, which reproduces the published WE ARE DISCOVERED vector; non-zero offsets exist to match phase-shifted sources and to multiply the variant space slightly.
Why did my spaces disappear?
Transposition rearranges a letter stream; spaces are positions, and positions are exactly what is being shuffled. The convention across this site's transposition family is to strip spaces and punctuation and work on letters alone, stating so on each page. If you need word boundaries preserved, you are looking for a substitution-family tool instead.
Is rail fence the same as the zigzag cipher in puzzle apps?
Usually yes in mechanism, sometimes no in phase: apps variously start the zigzag at the top rail or draw it rising. If an app's output won't decode here at offset 0, try the offsets in turn — the solver ranks all of them in about a second, which is faster than arguing with conventions.
Can a rail fence be made stronger?
Only by leaving the family: keying the read-off order turns it into Redefence, and layering it over a substitution complicates both attacks. The plain fence's 144-member key space cannot be enlarged from inside — more rails stop helping once the zigzag spans the whole message.