Rail fence cipher online – encoder/decoder
Rail fence cipher encoder and decoder online: encrypt text in a zigzag across 2–6 rails. Includes a rail table and worked example.
A message that is not read from left to right
What is the rail fence cipher?
The rail fence cipher does not change the letters, only their order. The message is written in a zigzag across several rails — like the boards of a fence — and the ciphertext is formed by reading the rails one after another, from top to bottom.
How does the zigzag work?
Letters run down to the last rail, bounce back up and repeat the cycle. The key is the number of rails: with three rails, successive letters land on rails 1, 2, 3, 2, 1, 2, 3, 2 and so on.
Example with three rails
The message “REBUSOWNIA” on three rails produces “RSIEUONABW”. The first rail collects R, S and I, the second E, U, O, N and A, and the third B and W. Reading the rails in order gives the ciphertext.
Rail table for three rails
The top row is the position of a letter in the message and the bottom row the rail it lands on. The 1-2-3-2 pattern repeats until the text ends.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Rail | 1 | 2 | 3 | 2 | 1 | 2 | 3 | 2 | 1 | 2 | 3 | 2 | 1 | 2 | 3 | 2 |
How do you decrypt the text?
First rebuild the zigzag itself: count how many letters fall on each rail for the given text length. Then split the ciphertext into that many parts, place them on the rails and read the letters along the zigzag. A wrong number of rails produces nonsense, so the parameter has to be known exactly.
Transposition versus substitution
Substitution ciphers such as Caesar or Atbash exchange letters for other letters. The rail fence belongs to the transposition family: the set of letters stays the same and only their arrangement changes. That is why the letter statistics of the ciphertext match the message exactly.
See also: Caesar cipher · Atbash cipher
How can the rail fence cipher be broken?
The number of sensible settings is tiny — testing one rail count after another until readable text appears is enough. The ciphertext itself betrays the method: the same letters as the original, in the same quantities. For short messages a few attempts recover the content.
Why is the rail fence cipher insecure?
The key is one small number, so the space of possibilities is negligible. The letter distribution stays untouched and fragments of text are often readable at a glance. Transposition becomes valuable only as a second layer on top of a substitution — on its own it protects nothing.
History of the rail fence cipher
Simple zigzag transpositions were used as convenient field ciphers in the nineteenth century: they needed no tables or devices, only paper and an agreed number of rails. The idea of rearranging letters is far older — the Spartan scytale, a strip of leather wound around a rod, applied the same principle in antiquity.
Rail fence cipher template to print
An A4 worksheet with a zigzag for the selected number of rails, instructions, an example, a puzzle and blank practice grids.