Skip to content

↑↓ navigate ↵ open Esc close Search runs locally

Rail Fence Cipher: Encoder, Decoder & Solver

Write a message in a zigzag over any number of rails and read it off row by row. See the zigzag drawn as you type, or let the solver find the number of rails for you.

  • Runs in your browser
  • Nothing is uploaded
  • Free, no sign-up
0 chars
Rail fence
Runs in your browser. Nothing is uploaded. Tip: Decrypt tries every rail count and offset

How to use the Rail fence cipher tool#

  1. Choose Encrypt or Decrypt and set the number of Rails (the key). An Offset starts the zigzag partway down.
  2. Type or paste your text. Every character, including spaces, goes into the fence unless you tick Letters only.
  3. Watch the Zigzag diagram under the tool to see which rail each character lands on.
  4. Don't know the key? In Decrypt mode, Rail guesses tries every rail count and offset and lists the most English-looking results. Click one to apply it.

Examples#

The classic example with 3 rails

Input
WE ARE DISCOVERED. RUN AT ONCE.
Output
WECRUOERDSOEERNTNEAIVDAC

With Letters only, spaces and punctuation are removed first, as in most textbooks.

Decrypt with 3 rails

Input
WECRUOERDSOEERNTNEAIVDAC
Output
WEAREDISCOVEREDRUNATONCE

Decryption counts how many letters belong to each rail, fills the rails and reads the zigzag again.

Crack it without the key

Input
Tichihsngnnsorols atrrghalneperttesai izade d rw os  er ytsmu i e neer e irwe  eg aza  hrai  yw tetesah ebteodcas fc sme gtetb,htte hrh.
Output
The rail fence cipher writes the message in a zigzag and then reads it row by row, so the letters stay the same but their order changes.

Change the rails to any other number, then look at Rail guesses: 4 rails comes out on top.

What is the rail fence cipher?#

The rail fence cipher writes the message diagonally down and up across a number of "rails", like a zigzag fence, and then reads the rails one by one from top to bottom. The letters themselves never change, only their order, which makes it a transposition cipher. The key is the number of rails.

How it works#

With 3 rails, WEAREDISCOVEREDRUNATONCE is written like this:

plaintext
W . . . E . . . C . . . R . . . U . . . O . . .. E . R . D . S . O . E . E . R . N . T . N . E. . A . . . I . . . V . . . D . . . A . . . C .

Reading the rails in order gives WECRUO + ERDSOEERNTNE + AIVDAC = WECRUOERDSOEERNTNEAIVDAC.

The pattern repeats every 2 × (rails − 1) characters: 4 for 3 rails, 6 for 4 rails. An offset starts the zigzag partway through that cycle, which some puzzles use as a second part of the key.

How to decrypt#

To decrypt, first work out how many characters land on each rail. For 24 letters on 3 rails, the top and bottom rails get 6 each and the middle rail gets 12. Split the ciphertext into those pieces, put each piece on its rail, and read the zigzag again from left to right.

How to break the rail fence cipher#

There are very few possible keys: with up to 12 rails and every offset, fewer than 150. The solver here decrypts the message with all of them. Because a transposition keeps the original letters, letter frequencies cannot tell the right answer apart. Instead, each result is scored by how often common English letter pairs such as TH, HE, IN and ER appear next to each other, and the highest scores are listed first.

Rail fence vs other ciphers#

CipherTypeWhat the key controls
Rail fenceTranspositionNumber of rails (and offset)
Columnar transpositionTranspositionColumn order, from a keyword
CaesarSubstitutionHow far each letter shifts

The rail fence cipher is not secure. With so few keys, it can be broken by hand in minutes and by a computer instantly. It is popular in puzzles, escape rooms and school exercises.

Rail fence cipher in code#

javascript
function railFence(text, rails) {  const rows = Array.from({ length: rails }, () => []);  let r = 0, step = 1;  for (const ch of text) {    rows[r].push(ch);    if (r === 0) step = 1;    else if (r === rails - 1) step = -1;    r += step;  }  return rows.map((row) => row.join('')).join('');} railFence('WEAREDISCOVEREDRUNATONCE', 3); // "WECRUOERDSOEERNTNEAIVDAC"

Common errors and fixes#

  • The result is different from another tool

    Tools differ in whether spaces and punctuation are part of the fence. Toggle Letters only. Some puzzles also start the zigzag lower down: try the Offset.

  • Decrypting gives scrambled text

    The number of rails or the offset is wrong. Look at Rail guesses in Decrypt mode, which tries them all.

  • The solver's first guess is wrong

    Very short messages have too few letter pairs to score reliably. Read the other candidates in the list, or add more ciphertext.

Frequently asked questions#

How do I decrypt a rail fence cipher without the key?

Choose Decrypt and paste the message. Rail guesses tries every number of rails from 2 to 12 and every offset, and ranks the results by how English they look. Click the readable one.

Is the rail fence cipher a substitution or a transposition cipher?

A transposition cipher: the letters stay the same and only their positions change.

Does the rail fence cipher include spaces?

It depends on the convention. Many textbooks remove spaces and punctuation first. This tool keeps them by default and removes them when you tick Letters only.

What is the offset?

The offset starts the zigzag partway through its cycle, for example on the second rail going down. It adds a little variety to the key.

Is my text uploaded?

No. Everything runs in your browser.

Your next tool is one search away.

Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.

Browse all tools

Tip: press Ctrl D to bookmark dev247.