Rail Fence Cipher
Classical transposition cipher — text is written in a zigzag across rails, then read off row by row.
Mode
Number of rails
Zigzag pattern (plain text → rail pattern)
W E C R L H
E R D S O E E F E A E D
A I V D E A
How the rail fence cipher works
The rail fence cipher writes the plaintext in a zigzag pattern across nimaginary rails. For example with 3 rails and text "WEAREDISCOVERED", the pattern is:
W . . . E . . . I . . . V . . . D . E . R . D . S . O . E . E . F . . . A . . . C . . . R . . . A . .
Reading each rail left-to-right and concatenating gives the ciphertext: WEIDVERDSOEEFARECA. Decryption reverses the process — place the known pattern of positions, fill in rows, then read diagonally. For other classical ciphers try Affine Cipher and Caesar Cipher.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Solving a puzzle whose ciphertext is a transposition rather than a substitution.
- Demonstrating that rearranging letters hides a message without changing the letter frequencies.
- Working through a classical cryptography exercise with a visual diagram.
- Producing a lightly obscured message for a game or a treasure hunt.
- Showing why frequency analysis alone does not break a transposition.
Frequently Asked Questions
- How does the rail fence cipher work?
- Letters are written in a zigzag across a fixed number of rails, then read off row by row. With three rails, `WEAREDISCOVERED` writes as a W shape and reads out as the top row, then the middle, then the bottom — a pure transposition with no substitution at all.
- Why is decoding harder to implement than encoding?
- Because you have to reconstruct the zigzag before you can read it. The lengths of each rail are determined by the message length and rail count, so the decoder computes those first, slices the ciphertext into rails, then walks the zigzag pulling one character from each.
- How secure is it?
- Not at all by modern standards. The only key is the rail count, so a handful of attempts breaks any message, and letter frequencies are unchanged because nothing is substituted — the plaintext's statistics survive intact. It is a teaching cipher and a puzzle, not a protection.
- Does it preserve spaces and punctuation?
- Here, yes — every character including spaces is placed on the fence. Traditional practice strips spaces and groups the output in fives, precisely because word lengths leak information. Keeping the spaces makes the tool reversible on arbitrary text at the cost of that leak.
- What is the difference between transposition and substitution?
- Transposition rearranges the same characters; substitution replaces them. That is why a rail fence output has exactly the same letter frequencies as its input, while a Caesar shift has the same SHAPE of frequency distribution moved along. Real ciphers combine both.
Common errors and gotchas
- Expecting frequency analysis to work. Transposition preserves letter counts, so the histogram looks like plain text.
- Forgetting the rail count is the key, so decoding without it means trying each possibility.
- Assuming spaces and punctuation are carried, when they are usually stripped before encoding.
- Losing the offset, where some variants start partway down the zigzag.
- Treating it as secure. The key space is tiny and exhaustible by hand.
Related Converters tools
JSON to YAML
Convert JSON into clean, readable YAML instantly.
YAML to JSON
Convert YAML configuration into valid JSON.
JSON to XML
Convert JSON structures into nested XML markup.
HWB to HEX Converter
Convert an HWB color to HEX.
JSON to SQL
Turn a JSON array of objects into SQL INSERT statements.
CSV to XML
Convert CSV rows into structured XML records.
XML to CSV
Flatten repeated XML records into CSV rows.
INI to JSON
Parse INI config sections and keys into JSON.