Rail Fence Cipher
Encode and decode the rail fence (zigzag) cipher with any rail count, plus a live fence diagram.
Puzzle toy, not real security.
Zigzag diagram
The plaintext written down and up across the rails. Reading each row left to right, top row first, gives the ciphertext.
โ
Don't know the key? Try every rail count
Decodings for 2โ12 rails with offset 0. The right one is the row that reads like real words.
How it works
It is a puzzle toy, not real security. The rail fence cipher only shuffles letters around, so it is trivially broken โ use it for escape rooms, geocaching, CTFs and classroom demos, never to protect anything private. Everything here runs in your browser; nothing you type is uploaded.
The rail fence cipher (also called the zigzag cipher) is a transposition
cipher: the letters of your message are written diagonally down and up across an imaginary
fence of rails, then read back off one row at a time. With the classic example
WEAREDISCOVEREDFLEEATONCE on 3 rails the fence looks like
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.., and reading the rows gives
WECRLTEERDSOEEFEAOCAIVDEN. Decoding reverses it: this tool rebuilds the same
zigzag pattern, works out how many characters land on each rail, and drops the ciphertext back
into place.
The key is simply the number of rails, optionally with an offset that starts the zigzag part-way down the fence. Because the key space is tiny, the cipher is broken by trying every rail count โ which is exactly what the auto-crack list above does. Classic rail fence work strips spaces and punctuation and uppercases everything, so switch to Keep every character only if you want spaces and symbols transposed too. The tool is a teaching and puzzle aid; it offers no real protection, and everything is computed locally in JavaScript with no network requests.
Frequently asked questions
How does the rail fence cipher work?
It is a transposition cipher. You write the message diagonally down and up across a number of imaginary rails, then read the letters back off one row at a time. With 3 rails, WEAREDISCOVEREDFLEEATONCE becomes WECRLTEERDSOEEFEAOCAIVDEN. The key is simply the number of rails, optionally with an offset that starts the zigzag part-way down the fence.
Can I decode a rail fence message without knowing the key?
Yes. Switch the tool to Decode and it lists the decoding for every rail count from 2 to 12 at once โ the correct key is the row that reads like real words. Because there are only a handful of sensible keys, the rail fence cipher is broken in seconds, which is why it is a puzzle rather than real encryption.
Is the rail fence cipher secure?
No. It only rearranges the letters you already have, so letter frequencies are untouched and the tiny key space can be searched by hand. Treat it as a teaching example, escape-room prop or CTF puzzle, never as protection for private data. Everything on this page runs locally in your browser and nothing you type is uploaded.