Rail Fence Cipher
Encode or decode a rail fence (zigzag) cipher with 2 to 20 rails and an optional offset, see the zigzag grid, or try every rail count to crack it.
The rail fence cipher writes a message in a zigzag across several rows, called rails, then reads each rail from left to right. With 3 rails, WEAREDISCOVEREDFLEEATONCE zigzags so that the top rail reads WECRLTE, the middle rail ERDSOEEFEAOC and the bottom rail AIVDEN, giving the ciphertext WECRLTEERDSOEEFEAOCAIVDEN. It's a transposition cipher: the letters stay the same and only their order changes, so the letter frequencies still look like English. The grid view shows where every character lands. The offset option starts the zigzag partway through its first cycle, a variation some puzzles use. If you don't know the number of rails, the crack option tries every count from 2 to 10 with every offset and ranks the results by how English they read.
0 starts on the top rail.
Zigzag grid
The plain text written in a zigzag across 3 rails. Reading each rail left to right gives the ciphertext.
| 1 | W | · | · | · | E | · | · | · | C | · | · | · | R | · | · | · | L | · | · | · | T | · | · | · | E |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | · | E | · | R | · | D | · | S | · | O | · | E | · | E | · | F | · | E | · | A | · | O | · | C | · |
| 3 | · | · | A | · | · | · | I | · | · | · | V | · | · | · | D | · | · | · | E | · | · | · | N | · | · |
Don't know the rails? Try every rail count
Decodings of the ciphertext above with 2 to 10 rails and every offset, ranked by how English the letter pairs look. Works best on 20 letters or more.
3 railsBest guess
WEAREDISCOVEREDFLEEATONCE
3 rails, offset 2
CEWEAREDISCOVEREDFLEEATON
3 rails, offset 3
EWEAREDISCOVEREDFLEEATONC
3 rails, offset 1
EAEWRIDESVOCEDERFEELANOTC
10 rails, offset 13
EEDECWRESEACIDNEVAOFORLET
5 rails, offset 7
RWLSADOOTEEECEAEECRFINVED
Common use cases
- Decode a zigzag cipher from an escape room, geocache or puzzle book
- Crack a rail fence message when you don't know the number of rails
- Show students how a transposition cipher rearranges letters
- Make a printable zigzag puzzle and check its answer
How to use
- Choose Encode or Decode.
- Type or paste your text.
- Set the number of rails and, if the puzzle uses one, the offset.
- Tick Letters only to strip spaces and punctuation first, as most puzzles do.
- Check the zigzag grid, or use Try every rail count to crack an unknown message.
FAQ
How many rails should I use?
Two or three rails is most common in puzzles. Any number works up to the length of the message; once the rails are as many as the characters, the text comes out unchanged.
Are spaces part of the cipher?
They can be. By default the tool keeps every character, including spaces, so decoding returns your exact text. Tick Letters only to remove spaces and punctuation before encoding, which is how the cipher is usually presented.
What does the offset do?
It starts the zigzag partway through its cycle, as if some characters had been written before the message began. With 3 rails and offset 1, the first letter goes on the middle rail and the zigzag heads down. A full cycle has 2 × (rails − 1) positions.
Is the rail fence cipher secure?
No. There are only a few sensible rail counts, so it can be broken by trying them all, which is what the crack option does.
Related tools
- Caesar Cipher Encoder & DecoderEncode or decode a Caesar cipher with any shift from 1 to 25, or crack one: all 25 shifts listed and ranked…
- Cipher IdentifierPaste a mystery code to see its letter frequencies and index of coincidence, a ranked list of likely ciphers…
- Vigenère Cipher Encoder & DecoderEncode or decode a Vigenère cipher with a keyword, see the key lined up under each letter, and estimate an…
- Atbash Cipher Encoder & DecoderEncode or decode Atbash, the cipher that reverses the alphabet (A↔Z, B↔Y). One box works both ways, keeps…
- Cryptogram GeneratorTurn a phrase into a printable cryptogram: a random letter substitution where no letter stands for itself…
- Pigpen Cipher TranslatorTurn text into pigpen cipher symbols, download them as SVG or PNG for printing, and decode pigpen by clicking…