Guide · Tier 2

The Rail Fence

The letters never change; they just move. Write in a zigzag, read off in rows.

What it is

The Rail Fence is a transposition cipher: it keeps every letter but scrambles their order. You write the message in a zigzag down and up across a number of "rails" (rows), then read each rail from left to right. The key is the number of rails. Shuffling letters this way is very old: Spartan generals did it by winding a strip around a staff. Read about that on the Archive card The Spartan Staff.

Draw the zigzag

Lay example 2 out on 2 to 5 rails. The right number turns it back into words along the zigzag.

Pick a number of rails.

How to encode

  1. Drop the spaces and write the letters in a zigzag: down one rail per letter to the bottom, then back up.
  2. Read the top rail left to right, then the next rail, and so on.

MEET BY THE CANAL on 2 rails:

M·E·B·T·E·A·A·
·E·T·Y·H·C·N·L

Reading the rails gives MEBTEAA + ETYHCNL = MEBTEAAETYHCNL.

How to break it, step by step

  1. Suspect it when the letters look like normal English (plenty of E, T, A) but in the wrong order. That's a transposition, not a substitution.
  2. Count the letters. The zigzag repeats every 2 × (rails − 1) letters, so the top rail gets about one letter in every cycle.
  3. Try 2 rails, then 3, then 4. For each, split the cipher text into rails of the right lengths, lay them on the zigzag and read along it.
  4. Stop when it reads as words. Then put the spaces back by sense.

Worked example 1: rails given

2 rails. Decode MEBTEAAETYHCNL.

With 2 rails, the first 7 letters are the top rail and the rest are the bottom. Lay them alternately: top, bottom, top, bottom… That gives MEETBYTHECANAL, or with spaces, MEET BY THE CANAL.

Worked example 2: rails unknown

PTLRSAKHBUCAECEET

RailsRead along the zigzag
2PUTCLARESCAEKEHTB
3PACKTHEBLUECRATES
4PRBEUSTACEAKLHETC

Three rails gives English: PACK THE BLUE CRATES.

Common mistakes

  • Splitting the rails into equal lengths. The top and bottom rails are shorter than the middle ones.
  • Reading down the columns instead of along the zigzag.
  • Expecting word breaks to survive: they don't, so add them back by sense.