Cipher Utility

Rail Fence Cipher

C-07

Write the message in a zigzag and read it off row by row. Runs in your browser; nothing is sent anywhere.

The zigzag

The message written in a zigzag across the rails. Reading each rail from left to right gives the ciphertext.

How to encrypt by hand

  1. Draw as many rows as there are rails.
  2. Write the message diagonally down and up.
  3. Read the first row, then the second, and so on.
  4. To decrypt, mark the zigzag and fill it row by row.

Worked example

WEAREDISCOVERED, 3 rails

Rows read left to right:

WECR · ERDSOEE · AIVD

WECRERDSOEEAIVD

Cipher Utility reference card Catalog no. C-07

What the rail fence cipher is

The rail fence cipher, also called the zigzag cipher, is a transposition cipher: it keeps every letter and only changes the order. The message is written diagonally across a number of rows, the rails, going down to the bottom rail and back up again. Reading each rail from left to right gives the ciphertext.

W . . . E . . . C . . . R . . . U . . . O . . . . E . R . D . S . O . E . E . R . N . T . N . E . . A . . . I . . . V . . . D . . . A . . . C . WECRUO + ERDSOEERNTNE + AIVDAC = WECRUOERDSOEERNTNEAIVDAC

The zigzag table under the tool draws this pattern for your own text.

Decrypting

To decrypt by hand, draw the empty zigzag for the message length and mark where each letter goes. Count how many marks fall on each rail, cut the ciphertext into pieces of those sizes, and write each piece along its rail. Then read the zigzag diagonally. The tool does this when you choose Decode.

How to break it

The only key is the number of rails, and it has to be smaller than the message length, so there are few options. Trying 2, 3, 4 and so on until the text reads normally breaks the cipher. The letter counts give it away too: they match normal English, because no letter was changed, only moved.

Questions about the rail fence cipher

How do I decode a rail fence cipher?

Choose Decode, paste the ciphertext and set the number of rails. If you do not know it, try 2, 3, 4 and so on; one of them will give readable text. There are only a few sensible values, so this takes seconds.

Is the rail fence the same as the zigzag cipher?

Yes. Both names describe writing the message in a zigzag across several rows and reading the rows one after another.

Does the tool keep spaces?

Yes. Spaces and punctuation are treated like letters and take their own place in the zigzag. Many puzzles remove spaces first; if yours did, remove them before encrypting so the result matches.

Is the rail fence cipher secure?

No. The letters are not changed, only moved, and the only key is the number of rails, so trying every rail count breaks it.

Is my text sent to a server?

No. Everything runs in your browser with JavaScript, and nothing you type leaves your device.