What is the rail fence cipher?

The rail fence cipher, also called the zigzag cipher, is a transposition cipher: it keeps every letter of a message and only changes their order. The letters are written in a zigzag across a few lines, called rails, and then read off one rail at a time. The name comes from that pattern, which looks like a fence with horizontal rails. Its only key is the number of rails, so it's quick to learn and just as quick to break. That makes it a classroom favorite and a common step in puzzle hunts, escape rooms and capture-the-flag games.

How the zigzag works

With 3 rails, the letters of PICNIC IN THE PARK run down and up across the rails like this:

P . . . I . . . T . . . A . .
. I . N . C . N . H . P . R .
. . C . . . I . . . E . . . K

Reading the rails from top to bottom gives PITA, INCNHPR and CIEK. The tool keeps the message's spaces where they were, so these letters fill words of the same lengths: PITAIN CN HPR CIEK. Whoever knows the number of rails can draw the same zigzag and put the letters back.

Tool description

Write or paste your message in the Plain text box, and the Cipher text box shows it zigzagged as you type, with a copy button below. The Rails field, under the Cipher menu, starts at 3. Insert sample puts in See you at the park at noon with 3 rails, giving Sot atn ey uth pran oe aeko. Pressing the swap button reverses the direction: the cipher text moves over into the input and gets decoded. This page also covers the other classical ciphers, such as Playfair and Baconian, through the Cipher menu. For reading rail fence messages, there's the Rail Fence Cipher Decoder, which starts on cipher text.

Examples

Plain text Rails Cipher text
Water the tulips 2 Wtrht lpa eteuis
Garden party at 6 p.m. 3 Gertad nata6 mr p y.p.
Piñata at the party 3 Pttaia at hpr yñaet
Bring the lemonade! 4 Bhnrt eoa iglmdnee!
Kites fly high today 5 Khyiy iat lgde fhost

What moves and what stays

  • Only letters and digits ride the zigzag. Each keeps its case and accent, so the ñ of Piñata travels as it is, and so do letters of other scripts, such as Greek or Cyrillic.
  • A digit is moved like any letter, which is how the 6 of Garden party at 6 p.m. lands inside a word.
  • Spaces, punctuation and line breaks never move, so every word keeps its length, which gives a clue away. To hide the lengths, type the message without spaces.
  • The first letter never moves, since it starts the top rail.

Choosing the number of rails

  • The Rails field takes whole numbers from 2. When it's empty, below 2 or a fraction, the tool says "Use at least 2 rails."
  • More rails don't always hide more. With as many rails as the message has letters and digits, nothing moves, and with one rail fewer, only the last two swap: Hello with 4 rails becomes Helol.
  • There are only a few numbers to try, so anyone can break the message by trying them one by one, which is just what the Cipher Identifier does. The tool runs entirely in your browser, and nothing you type is uploaded.