Rail Fence Cipher
The rail fence cipher writes a message in a zigzag across a set number of rails and reads it off row by row, so WEAREDISCOVEREDFLEEATONCE on 3 rails becomes WECRLTEERDSOEEFEAOCAIVDEN.
Input
More options
Every character goes on the fence, spaces included. Remove spaces first for the classic letters-only form.
Encoded with 3 rails
Zigzag
Type some text to see it on the rails.
Why use our Rail Fence Cipher?
2 to 10 Rails
Encode or decode with any count from 2 to 10 rails. Both sides need the same count, and an optional offset sits under More options.
Your Text on the Rails
The Zigzag card draws your own text on the rails, up to the first 100 characters, and lists every rail in full so you can check each step by eye.
Every Rail Count at Once
In Decode, Unknown rail count decodes your text with each count side by side, with a button to use it, so a lost key takes one look instead of guess after guess.
What is the Rail Fence Cipher?
The rail fence cipher is a transposition cipher: it writes the message in a zigzag down and up across a set number of rails, then reads each rail from left to right. The letters themselves never change, only their order, so the key is simply the number of rails.
This tool puts every character on the fence, spaces and punctuation included, so the coded text is as long as the message. Textbook examples remove spaces first, which is why the example above is written as WEAREDISCOVEREDFLEEATONCE. Paste coded text exactly as you received it, or its spaces land in the wrong places.
How the Zigzag Works
- Down and up:Write the letters diagonally down and up across the rails, then read each rail left to right, which is why it is also called the zigzag cipher (Wikipedia).
- The name:The name compares the rows to the horizontal rails of a fence.
- Period:On 3 rails the pattern repeats every 4 letters. The example gives the rails WECRLTE, ERDSOEEFEAOC, AIVDEN.
- Any count:In general the cycle is twice the rails minus 2: 2 letters on 2 rails, 4 on 3 and 18 on 10.
- Wikipedia check:Wikipedia's own example, WEAREDISCOVEREDRUNATONCE, on 3 rails gives WECRUOERDSOEERNTNEAIVDAC, the same as its table.
- Offset:Offset, under More options, starts the zigzag partway through its cycle.
Common Use Cases
CTF Challenges
Capture the flag puzzles sometimes hide a flag behind a rail fence; paste it into Decode and read down the list of rail counts.
Cipher Homework
Check a zigzag you drew on squared paper against the rail lines the tool lists under the drawing.
Teaching Transposition
Show a class that a cipher can hide a message by moving letters without changing a single one.
Scrambled Clues
Write a clue on 3 or 4 rails so it looks scrambled while still using only the letters of the answer.
Layered Puzzles
Rail fence a message first, then pass it through the Vigenere cipher for a two-step puzzle.
Coded Notes
Swap short coded notes with a friend who knows the rail count, with no app or account on either side.
How to Use the Rail Fence Cipher Decoder
Enter the message
Type or paste your text into Text. Spaces and punctuation stay on the fence: WE ARE on 2 rails gives W REAE.
Set the rails
Set the number of rails, from 2 to 10. Use the minus and plus buttons or type the number.
Encode or decode
Press Encode or Decode. The output heading names the rail count, and the Zigzag card redraws the text.
Copy or test counts
Copy the output. If the count is unknown, open Unknown rail count in Decode and press Use on the line that reads as words.
Why It Is a Transposition Cipher
Letters Move, Not Change
A transposition cipher moves letters without replacing them, so the letter counts match normal text and a frequency count gives it away (Wikipedia: Transposition cipher).
The Opposite of Caesar
A Caesar cipher does the opposite: it replaces each letter and keeps its place. Run one message through both pages to see the difference.
Few Usable Keys
A rail count equal to or above the text length leaves the text unchanged, so few keys are usable and trying them all is quick; Wikipedia calls the rail fence weak (Wikipedia: Rail fence cipher).
Decoding When You Do Not Know the Rails
- •Try every count: The key is the number of rails, and there are only a few to try, so trying them all is quick.
- •Read the list: 2 rails: WEEFCERALOTCEAEIRVDDSEONE; 3 rails: WEAREDISCOVEREDFLEEATONCE; 4 rails: WTEVFEEEEDARCDOECSROANIEL. Only 3 rails reads as words.
- •By hand: By hand, count how many letters each rail holds: WECRLTEERDSOEEFEAOCAIVDEN on 3 rails splits into runs of 7, 12 and 6 letters, then read the zigzag back.
- •No score: Every line holds the same letters, so the list is not scored; you pick the line that reads as words.
- •Check the offset: If no line reads, the sender may have used an offset. Set it under More options and the list decodes with it.
- •Short texts: The list only shows counts below the text length, because a count equal to or above it changes nothing.
Encode a Message on the Rails
Pick a rail count, type a message and watch the zigzag fill in. Switch to Decode with the same count and the original comes back, spaces and all.
The rail fence cipher decoder and encoder run in your browser; nothing you type is sent. It is a cipher for puzzles and learning, not for protecting real secrets.
Worked Example on 3 Rails
The Zigzag
WEAREDISCOVEREDFLEEATONCE written down and up across 3 rails, 4 letters per cycle.
Reading the Rails
- Rail 1: WECRLTE (7 letters)
- Rail 2: ERDSOEEFEAOC (12 letters)
- Rail 3: AIVDEN (6 letters)
Joined in order, the rails give WECRLTEERDSOEEFEAOCAIVDEN. Decoding cuts the coded text back into runs of those lengths and reads the zigzag again.
Related tools
Frequently Asked Questions
How does the rail fence cipher work?
You write the message in a zigzag down and up across the rails, then read each rail from left to right. WEAREDISCOVEREDFLEEATONCE on 3 rails becomes WECRLTEERDSOEEFEAOCAIVDEN.
How do you decrypt a rail fence cipher?
Paste the coded text, set the same number of rails used to encode it and press Decode. Spaces and punctuation sit on the fence too, so paste the coded text exactly as received: MEET AT NOON encoded on 3 rails only decodes back with its spaces in place.
How do you solve it without the number of rails?
In Decode, open Unknown rail count to see the text decoded with every rail count from 2 to 10, then pick the line that reads as words.
What is the key in a rail fence cipher?
The number of rails. Encoding and decoding must use the same count. An Offset under More options, from 0 to 3 on 3 rails, starts the zigzag partway through its cycle, and the other side needs it too.
Why did my text not change?
With as many rails as characters, each character sits on its own rail, so ABC on 3 rails stays ABC. Use fewer rails.
Is the rail fence cipher secure?
No. There are only a few useful rail counts to try, and the letters themselves are not changed, so it is for puzzles and learning only.