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.

1W···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

  1. Choose Encode or Decode.
  2. Type or paste your text.
  3. Set the number of rails and, if the puzzle uses one, the offset.
  4. Tick Letters only to strip spaces and punctuation first, as most puzzles do.
  5. 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.