The Magic Codex

Rail Fence Cipher

Write diagonally, read straight across. The zigzag transposition cipher with a live pattern view — watch every letter find its rail.

3

Zigzag pattern

How to use the Rail Fence Cipher

The rail fence is the friendliest transposition cipher there is: no alphabets to memorize, just a zigzag you can draw on paper.

  1. Type a message in the box at the top.
  2. Set the rails with the slider — 2 is a gentle shuffle, 10 is a proper tangle. The pattern view redraws instantly.
  3. Choose Encode or Decrypt and watch the result update live.
  4. Study the zigzag: each row of the pattern is one rail; follow a letter's column down the rows to see the diagonal path it traveled.
  5. Flip it: "Use output as input" moves the result into the message box — handy for checking a decode round-trip.

Decoding by hand

Count the ciphertext's letters and draw that many columns. Snake the zigzag down and up across your rails, then write the ciphertext into the rails row by row — first rail first. Now read the zigzag path from left to right: there's your message. The pattern view above is exactly this drawing, done for you.

A tiny history

Zigzag writing is ancient — the Spartans' scytale wrapped a strip around a staff for a similar effect — but the rail fence as a named cipher belongs to the age of pen-and-paper puzzling, where drawing the fence was the algorithm. It survives today in puzzle hunts and as the classic first transposition cipher in cryptography courses, usually paired with columnar transposition as its bigger sibling.

What is the rail fence cipher?
A transposition cipher: you write the message diagonally down and up across a number of 'rails' (rows), then read off each row in turn. The letters never change — only their order does. With 3 rails, WEAREDISCOVEREDFLEEATONCE becomes WECRLTEERDSOEEFEAOCAIVDEN.
How do I decode a rail fence cipher by hand?
Draw the zigzag for the rail count: mark the down-and-up path across as many columns as the message is long, then write the ciphertext into the rails row by row. Reading the zigzag path left to right spells the plaintext. This page's pattern view does the drawing for you.
How many rails should I use?
Anything from 2 to 10 works here. Two rails is the gentlest scramble (odd positions, then even); more rails tangle the text further but also make the zigzag longer. For puzzles, 3–5 rails is the sweet spot — enough to hide the message, easy to draw by hand.
Does the rail fence cipher change spaces and punctuation?
No — like the message letters, spaces and punctuation are simply carried along the zigzag and land somewhere in the ciphertext. Nothing is added or removed, so decoding restores the exact original, spaces included.
Is the rail fence cipher secure?
No. It's a simple transposition with a tiny key space — the rail count — and it preserves letter frequencies, so it falls to analysis quickly. It's a lovely teaching cipher and a fun puzzle mechanic, but never use it for real secrets.
Is my text sent anywhere?
No. All encoding happens in your browser with JavaScript. Nothing is uploaded, stored, or tracked.

Need help with this tool?

Found a bug, or have a suggestion for the codex? Write to us — a real human reads every message.

[email protected]

More from the codex