What Is the Rail Fence Cipher?
The rail fence cipher (also called the zigzag cipher) is a transposition cipher: it reorders the characters of a message without changing any of them. To encrypt, you write the message in a diagonal zigzag across an imaginary set of horizontal lines — the "rails" — moving down one rail per character until you reach the bottom, then up until you reach the top, and so on. Once the whole message is written out, you read the rails off one at a time, top to bottom, and the result is the ciphertext.
Transposition ciphers are among the oldest cryptographic techniques known. The Greeks used the scytale — a rod with a strip of parchment wound around it, on which the message was written along the rod and read off across the spiral — from around the 5th century BC. The rail fence is the simplest modern form of the same idea, and it appears throughout the history of low-grade field ciphers. Today its home is puzzles, games, escape rooms, and classrooms, where its visual, hands-on nature makes it one of the most accessible ciphers to learn.
How the Rail Fence Cipher Works
Every character of the message is assigned a rail number that follows a repeating zigzag pattern. For N rails, the sequence of rails is 0, 1, 2, …, N−1, N−2, …, 1, 0, 1, 2, … — descending to the bottom, bouncing back up to the top, and repeating. The pattern repeats every 2(N−1) characters:
| Rails | Rail pattern (first 8 positions) | Cycle length |
|---|---|---|
| 2 | 0 1 0 1 0 1 0 1 | 2 |
| 3 | 0 1 2 1 0 1 2 1 | 4 |
| 4 | 0 1 2 3 2 1 0 1 | 6 |
| 5 | 0 1 2 3 4 3 2 1 | 8 |
Encryption assigns each character to the rail its position dictates, then concatenates the rails from top to bottom. Decryption runs the same pattern, counts how many characters belong to each rail, slices the ciphertext into those rail-sized pieces, and walks the zigzag again — pulling the next character from each rail as it goes — to recover the original order.
Step-by-Step Example
The canonical example encrypts "WEAREDISCOVEREDFLEEATONCE" with 3 rails. Write the message along the zigzag — W on rail 0, E on rail 1, A on rail 2, R back on rail 1, and so on:
| Rail | Characters |
|---|---|
| 0 | W . . . E . . . C . . . R . . . L . . . T . . . E |
| 1 | . E . R . D . S . O . E . E . F . E . A . O . C . |
| 2 | . . A . . . I . . . V . . . D . . . E . . . N . . |
Reading the rails top to bottom — rail 0, then rail 1, then rail 2 — gives WECRLTEERDSOEEFEAOCAIVDEN. Try it in the tool above with 3 rails, then paste the ciphertext into the Ciphertext box to watch the original phrase come back letter by letter.
Example Reference Table
| Plain Text | Rails | Ciphertext |
|---|---|---|
| hello | 2 | hloel |
| hello | 3 | hoell |
| ATTACK AT DAWN | 3 | ACTWTAKA ANT D |
| SimplyCalculated | 4 | SCaiyaltmlluepcd |
Paste any plain-text value with its rail count into the tool to confirm the ciphertext matches, or paste ciphertext with the same rail count to confirm it decodes back. Notice that spaces and punctuation ride the rails just like letters — nothing is left untouched.
Security and Cryptanalysis
The rail fence belongs to the transposition family, and its security is minimal. Because the letters themselves never change, every letter frequency in the plaintext survives in the ciphertext — the most direct fingerprint of a transposition. An attacker who suspects one can try each rail count in turn; the ciphertext rearranges differently each time, and a human (or a dictionary) instantly recognizes which rearrangement reads as language. The rail count is the only key, and it is a very small one. Historically, transpositions like this were used as a first line of defense against casual interception, or layered inside more complex ciphers — the Allied forces of World War II, for instance, routinely mixed transposition steps into field ciphers that also substituted letters. On its own, it is a puzzle cipher.
Variants
The rail fence's closest relatives are other transpositions. The columnar transposition writes the message into a grid row by row, then reads columns out in an order determined by a keyword — more flexible and with a real key. The double transposition applies a transposition twice, which was famously judged to be a secure field cipher by French and German forces in the early twentieth century before modern cryptanalysis showed even it was breakable. The rail fence itself is sometimes varied by changing the angle of the zigzag or the starting rail, but the core idea — write diagonally, read horizontally — stays the same.
Troubleshooting & Common Mistakes
Decryption comes out scrambled
The rail count must match the one used to encrypt. Because the tool decrypts with whatever rail count is currently set, double-check that number first. If it is right, verify the ciphertext was copied completely — transposition is unforgiving: one missing character throws off every rail after it.
My output looks identical to my input
With 1 rail, or with a rail count equal to or greater than the message length, the cipher is the identity — every character sits on its own rail and the message reads off in order. Short messages with many rails are the classic cause. Lower the rail count to see actual scrambling.
Can I use rail counts above 20?
The tool's input is capped at 20 for clarity, but the math handles any whole number. Values above the message length simply become the identity transformation, so more rails than characters never helps.
How This Cipher Compares to Other Classical Ciphers
The rail fence cipher is one of several classical ciphers covered on this site, and the only transposition cipher among them — it rearranges letter order instead of substituting letters, a fundamentally different technique from every other row in the table below.
| Cipher | Mechanism | What Makes It Different |
|---|---|---|
| Caesar Cipher | Substitution | Shifts every letter by one fixed number (the key) through the alphabet — the simplest classical substitution cipher. |
| Atbash Cipher | Substitution (fixed) | A special case of substitution with no key at all — it always mirrors the alphabet (A↔Z, B↔Y, ...). |
| Vigenère Cipher | Polyalphabetic substitution | Repeats a keyword to shift each letter by a different amount, defeating simple frequency analysis that breaks Caesar. |
| Beaufort Cipher | Polyalphabetic substitution (reciprocal) | A variant of Vigenère's idea that is self-reciprocal — the same operation both encrypts and decrypts. |
| Playfair Cipher | Digraph substitution | Encrypts letters two at a time using their positions in a 5×5 key square — the first practical digraph cipher (1854). |
| Affine Cipher | Mathematical substitution | Encrypts using a linear function (ax + b mod 26) with two keys instead of one shift value. |
| ROT47 | Substitution (extended range) | Applies a Caesar-style shift across the full printable ASCII range, not just letters — used to obfuscate text online (e.g. spoilers), not for security. |
| Rail Fence Cipher (this page) | Transposition | Rearranges letter order in a zigzag pattern instead of substituting letters — a fundamentally different technique from every cipher above. |
| A1Z26 | Encoding (not a cipher) | Simply maps each letter to its position number (A=1...Z=26) — a puzzle/encoding convention, not intended for real secrecy. |
| NATO Phonetic Alphabet | Encoding (not a cipher) | Replaces each letter with a spoken code word (Alfa, Bravo, Charlie…) for reliable voice transmission on noisy radios — built for clarity, not secrecy. |