The Rail Fence cipher rearranges letters along a zigzag. Start on the top rail, move down to the bottom, then climb back up. Once the message is written, read each rail from left to right, starting at the top.

The path used here

The grid uses A–Z letters only. Spaces and punctuation are removed, and you can see the changes in the prepared input. Decoding restores those prepared letters, without the original spacing or punctuation.

  • Choose 2–12 rails.
  • Begin on the top rail and move downward. The offset is zero.
  • Reverse direction at the top and bottom rails.
  • Do not pad short rails with X or another character.

Three rails, zero offset

InputWEAREDISCOVEREDFLEEATONCE
Settings3 rails; start at top; first direction down
Expected resultWECRLTEERDSOEEFEAOCAIVDEN
Try this method
RailLetters read
1WECRLTE
2ERDSOEEFEAOC
3AIVDEN

How to decode the zigzag

First mark a zigzag with as many positions as there are ciphertext letters. Count the positions on each rail, fill the rails from top to bottom, then follow the zigzag to read the message. Do not split the ciphertext into equal pieces: the rails usually hold different numbers of letters.

The example has 25 letters, divided into rails of 7, 12, and 6. Blank grid cells are simply outside the path. They do not stand for spaces or missing letters.

Try one yourself

Encode MEETME using 3 rails. First write down the rail sequence 1, 2, 3, 2, 1, 2.

Keep exploring

Follow a three-rail examplePrint Rail Fence grid practiceSubstitution vs. transpositionMake a two-stage puzzle and answer key