Rail Fence Cipher Encoder / Decoder

Encode or decode text with the classical rail fence cipher. Set the number of rails, decide whether to keep spaces and punctuation, and view the zigzag pattern to understand how the cipher works.

Chars processed

Rails used

Mode

Zigzag for current text and rail count (first ~40 characters shown).

How the rail fence cipher works

The message is written diagonally across several rows (rails) and then read off row-by-row.

Example: plaintext = WEAREDISCOVEREDFLEEATONCE, rails = 3

Write in zigzag:

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .

Read row by row ⇒ WECRL TEERD SOEEF EAOCA IVDEN (spaces added for clarity)

Decoder notes

To decode, we rebuild the zigzag: we first mark the pattern, fill rows with ciphertext, then read in zigzag order.

FAQ

  • Does it preserve spaces? Yes, if you tick “Keep spaces/punctuation”.
  • Is it secure? No: it’s a classical teaching cipher, good for puzzles.