Definition

The Rail fence cipher is a transposition cipher. As such it is a method that rearranges the characters in a message in a particular way without changing them. Here, the message is written down in a zigzag pattern across some rows or “rails” and then read off row by row to form the ciphertext.

Photograph of a wood rail fence in Hiawatha National Forest (source)

How does the Rail fence cipher work?

To encrypt the message “We are discovered. Run at once.” we lay out the letters without spaces and punctuation in a zigzag pattern across a certain number of “rails”, starting in the top left corner.

W       E       C       R       U       O
  E   R   D   S   O   E   E   R   N   T   N   E
    A       I       V       D       A       C
Plaintext message laid out in a zigzag pattern across 3 rails

After that, we simply read off the letters row by row and put them together to get the following ciphertext:

WECRUOERDSOEERNTNEAIVDAC
Ciphertext (encrypted version of the message above)

To decode, we fill in the ciphertext in the same zigzag pattern row by row, then read off the plaintext column by column.

The number of rails you use and the initial offset (number of columns you skip before laying out the message) form the key of the cipher. In this example, we use 3 rails and no initial offset.

Resources