SimplyCalculated.org

Rail Fence Cipher Encoder & Decoder

Write your message diagonally across a set of horizontal rails — down, up, down, like a zigzag fence — then read it off row by row. The simplest transposition cipher there is, and a descendant of the ancient Greek scytale. Type on either side to encrypt or decrypt instantly. Everything runs locally in your browser.

100% private: everything is processed in your browser's memory — nothing you enter is uploaded to a server, logged, or stored.

Rail Fence Cipher

The message zigzags down and up this many horizontal lines before being read off row by row. Typing into the Plain Text box encrypts; typing into the Ciphertext box decrypts.

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.

Frequently Asked Questions

Is the rail fence cipher secure?
No — it is a transposition cipher, and like all pure transpositions it only scrambles the order of letters; the letters themselves are unchanged. That means the letter frequencies of your message survive intact, which is a huge giveaway: an attacker who suspects a transposition can try different rail counts (each is just a different rearrangement) and spot the result by eye or by simple anagramming. It is a fun puzzle cipher, not a protection mechanism. Real security needs a cipher that also changes the letters themselves, and ideally a large key space.
How do I choose the right number of rails?
Any whole number from 1 up works, but the tool is most useful between 2 and around 10 rails. One rail is the identity — the message passes through unchanged. If the rail count meets or exceeds the message length, the result is also the identity, because each character ends up on its own rail. The rail count is the cipher's only secret, and it must be shared (or guessed) to decrypt: decrypting with the wrong rail count produces scrambled garbage.
Why does my decryption produce garbage?
Almost always because the rail count is wrong. Encryption and decryption must use the same number of rails, and it is easy to forget which one was used — especially because the tool applies the current rail count in whichever direction you type. It can also happen if the ciphertext was edited or copied incompletely: because the message is read off the rails in a strict order, a single missing or extra character shifts the whole reconstruction.
Do spaces and punctuation get scrambled too?
Yes. The rail fence cipher treats every character — letters, spaces, punctuation, digits — as equal, and writes them all onto the zigzag path. So "ATTACK AT DAWN" with 3 rails encrypts to "ACTWTAKA ANT D", spaces included. Decryption puts them back exactly where they were. This is different from substitution ciphers like Caesar, which leave non-letters untouched.
What is the difference between the rail fence and other transposition ciphers?
The rail fence is one member of the transposition family, which all change the order of characters without changing the characters themselves. The columnar transposition writes the message into a grid row by row and reads it out column by column in a keyword-defined order; the double transposition applies two such passes to defeat many attacks. The rail fence is distinctive for its diagonal zigzag pattern, which makes it easy to visualize — and easy to break with a little anagramming.
Where did the rail fence cipher come from?
Transposition ciphers are ancient — the Greeks used the scytale, a rod around which a message strip was wound, as early as the 5th century BC. The rail fence is the simplest descendant of that idea, documented in classical and renaissance cipher literature. It has been used in real conflicts as a low-level field cipher, but mostly it survives today as a teaching tool and a staple of puzzles, games, and escape rooms.
Does this tool work with any text?
Yes — the rail fence is purely about rearranging characters, so any text works: uppercase, lowercase, digits, punctuation, emoji, even non-Latin scripts. There is no alphabet math involved, so nothing is ever skipped or changed, only reordered. That makes it the one cipher in this family with zero character-set limitations.

Formula last verified August 22, 2026 against our published methodology .