The classic example with 3 rails
WE ARE DISCOVERED. RUN AT ONCE.
WECRUOERDSOEERNTNEAIVDAC
With Letters only, spaces and punctuation are removed first, as in most textbooks.
Write a message in a zigzag over any number of rails and read it off row by row. See the zigzag drawn as you type, or let the solver find the number of rails for you.
Zigzag
Rail guesses
WE ARE DISCOVERED. RUN AT ONCE.
WECRUOERDSOEERNTNEAIVDAC
With Letters only, spaces and punctuation are removed first, as in most textbooks.
WECRUOERDSOEERNTNEAIVDAC
WEAREDISCOVEREDRUNATONCE
Decryption counts how many letters belong to each rail, fills the rails and reads the zigzag again.
Tichihsngnnsorols atrrghalneperttesai izade d rw os er ytsmu i e neer e irwe eg aza hrai yw tetesah ebteodcas fc sme gtetb,htte hrh.
The rail fence cipher writes the message in a zigzag and then reads it row by row, so the letters stay the same but their order changes.
Change the rails to any other number, then look at Rail guesses: 4 rails comes out on top.
The rail fence cipher writes the message diagonally down and up across a number of "rails", like a zigzag fence, and then reads the rails one by one from top to bottom. The letters themselves never change, only their order, which makes it a transposition cipher. The key is the number of rails.
With 3 rails, WEAREDISCOVEREDRUNATONCE is written like this:
W . . . E . . . C . . . R . . . U . . . O . . .. E . R . D . S . O . E . E . R . N . T . N . E. . A . . . I . . . V . . . D . . . A . . . C .Reading the rails in order gives WECRUO + ERDSOEERNTNE + AIVDAC = WECRUOERDSOEERNTNEAIVDAC.
The pattern repeats every 2 × (rails − 1) characters: 4 for 3 rails, 6 for 4 rails. An offset starts the zigzag partway through that cycle, which some puzzles use as a second part of the key.
To decrypt, first work out how many characters land on each rail. For 24 letters on 3 rails, the top and bottom rails get 6 each and the middle rail gets 12. Split the ciphertext into those pieces, put each piece on its rail, and read the zigzag again from left to right.
There are very few possible keys: with up to 12 rails and every offset, fewer than 150. The solver here decrypts the message with all of them. Because a transposition keeps the original letters, letter frequencies cannot tell the right answer apart. Instead, each result is scored by how often common English letter pairs such as TH, HE, IN and ER appear next to each other, and the highest scores are listed first.
| Cipher | Type | What the key controls |
|---|---|---|
| Rail fence | Transposition | Number of rails (and offset) |
| Columnar transposition | Transposition | Column order, from a keyword |
| Caesar | Substitution | How far each letter shifts |
The rail fence cipher is not secure. With so few keys, it can be broken by hand in minutes and by a computer instantly. It is popular in puzzles, escape rooms and school exercises.
function railFence(text, rails) { const rows = Array.from({ length: rails }, () => []); let r = 0, step = 1; for (const ch of text) { rows[r].push(ch); if (r === 0) step = 1; else if (r === rails - 1) step = -1; r += step; } return rows.map((row) => row.join('')).join('');} railFence('WEAREDISCOVEREDRUNATONCE', 3); // "WECRUOERDSOEERNTNEAIVDAC"The result is different from another tool
Tools differ in whether spaces and punctuation are part of the fence. Toggle Letters only. Some puzzles also start the zigzag lower down: try the Offset.
Decrypting gives scrambled text
The number of rails or the offset is wrong. Look at Rail guesses in Decrypt mode, which tries them all.
The solver's first guess is wrong
Very short messages have too few letter pairs to score reliably. Read the other candidates in the list, or add more ciphertext.
Choose Decrypt and paste the message. Rail guesses tries every number of rails from 2 to 12 and every offset, and ranks the results by how English they look. Click the readable one.
A transposition cipher: the letters stay the same and only their positions change.
It depends on the convention. Many textbooks remove spaces and punctuation first. This tool keeps them by default and removes them when you tick Letters only.
The offset starts the zigzag partway through its cycle, for example on the second rail going down. It adds a little variety to the key.
No. Everything runs in your browser.
Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.
Tip: press Ctrl D to bookmark dev247.