Cipher
Rail Fence Cipher Encoder and Decoder
The rail fence cipher writes your message in a zigzag across a chosen number of rails, then reads each rail off in order to produce the ciphertext. Choose a rail count, type your message, and watch the scrambled text appear.
Result appears here
Unlike Caesar or Vigenere it never changes a single letter, it only reorders them, which makes it a transposition cipher rather than a substitution one. Everything happens in your browser.
How the Rail Fence Cipher works
Write the message diagonally down and up across the chosen number of rails, like a bouncing ball: down to the bottom rail, back up to the top, and repeat. Each letter lands on exactly one rail.
To read the ciphertext, take the rails one at a time, top to bottom, and read each rail left to right. Decoding reverses this: the same zigzag pattern shows which position in the ciphertext belongs to which rail.
Examples
History and origins
Zigzag transposition is one of the oldest and simplest ways to disguise a message, used since antiquity for dispatches where speed mattered more than strength. It takes its name from the wooden zigzag rail fences once common on farms, which the diagonal writing pattern resembles.
It offers very little real protection. With a short message an attacker can simply try every reasonable rail count and read off the one that produces sensible words, which is why it is taught today mainly to introduce the idea of transposition.
Frequently asked questions
How do I decode a rail fence cipher?
You need the same number of rails used to encrypt. Rebuild the zigzag pattern for that length and rail count, then read the letters back off in zigzag order, which is exactly what this tool does when you swap direction.
Does the rail fence cipher keep spaces and punctuation?
This tool transposes every character you type, including spaces and punctuation, so decoding restores the exact original text, not just the letters.
How many rails should I use?
Two rails barely scrambles anything, since it is close to alternating letters. More rails scramble the order further, though with a short message it is still easy to guess the rail count by trial and error.
Learn more
Go deeper on the ideas behind this tool.