Skip to content

Rail Fence Cipher

Write your message in a zigzag across 2 to 10 rails and read it off rail by rail — or paste cipher text and unwind it. The zigzag is drawn out under the tool, and when you don't know the rail count, decode mode can try them all at once.

Mode

Positions of the zigzag to skip before the first character. 0 starts on the top rail.

Keep

Whatever you keep travels along the fence like a letter; anything else is removed before the message is written out.

The zigzag

The plain text runs down and up across the rails. Reading each rail from left to right, top rail first, gives the cipher text.

Rail 1WECRLTE
Rail 2ERDSOEEFEAOC
Rail 3AIVDEN

Reading the rails

  1. Rail 1WECRLTE
  2. Rail 2ERDSOEEFEAOC
  3. Rail 3AIVDEN

How to Use the Rail Fence Cipher

  1. 1. Pick Encode or Decode. Encode turns your message into cipher text; Decode turns cipher text back into the message.
  2. 2. Set the rails and the offset. Choose 2–10 rails. Leave the offset at 0 unless the zigzag was started part-way down.
  3. 3. Decide what travels the fence. Spaces and punctuation are removed first by default, as in the textbook examples. Tick them to keep them — they then move along the rails like letters, and whoever decodes must keep them too.
  4. 4. Follow the zigzag. The grid shows where each character lands and lists the rails as they are read off. Copy the result, or press Swap to decode what you just encoded.

How the Rail Fence Cipher Works

The rail fence is a transposition cipher: every letter of the message survives, and only the order changes. Write the message diagonally down and up across a number of rows — the rails — so it zigzags like a split-rail fence, then read each rail from left to right, top rail first. Here is WE ARE DISCOVERED FLEE AT ONCE on three rails, with the spaces taken out:

Three rails

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 . .

Reading the rails in order gives WECRLTE + ERDSOEEFEAOC + AIVDEN = WECRLTEERDSOEEFEAOCAIVDEN, which is exactly what the tool returns for that message with 3 rails and offset 0. Decoding needs only the message length. Twenty-five letters on three rails put 7 on the top rail, 12 on the middle and 6 on the bottom, so the cipher text is cut into pieces of those lengths and the zigzag is walked again, taking the next letter from whichever rail it reaches.

The offset

An offset starts the zigzag part-way through its cycle. On three rails the cycle visits the top, middle, bottom and middle rails, four positions in all, so the offset runs from 0 to 3; offset 1 starts on the middle rail heading down. With offset 1 the same message becomes RSEFACWAEICVRDLETNEEDOEEO. In general a fence of r rails has a cycle of 2(r − 1) positions, and the offset is part of the key: decoding with the wrong one scrambles the result as thoroughly as the wrong rail count.

Breaking a Rail Fence Cipher

A rail fence is easy to recognise and easier to break. Because it only rearranges letters, the cipher text keeps the letter frequencies of ordinary English — plenty of E, T and A, hardly any Z — which tells a cryptanalyst straight away that the letters have been moved rather than replaced. Then there are very few keys: only rail counts from 2 up to one less than the message length make any difference, and a short message is unlikely to have used more than a handful.

That is what the brute-force option is for. In Decode mode, tick Try every rail count and scan the list for the line that reads as language. If none does, change the offset and look again — every combination of 2–10 rails and every offset adds up to only 90 possibilities.

Transposition did not stay this weak. Stronger forms such as columnar transposition under a keyword, and double transposition, which applies it twice, carried real military traffic well into the twentieth century. The rail fence is the simplest member of the family, which is why it is the one taught first.

Frequently Asked Questions

What is a rail fence cipher?
A transposition cipher that writes the message in a zigzag across several rows, called rails, and then reads the rows one after another. The letters themselves never change, only their positions, so the key is just the number of rails — plus an offset in some versions.
How do you decode a rail fence cipher?
Count the letters, work out how many land on each rail for the given rail count, and cut the cipher text into pieces of those lengths, top rail first. Then trace the zigzag again, taking the next letter from whichever rail it reaches. Decode mode does this for you, as long as you use the same rail count, offset and space setting that were used to encode.
How many rails should I use?
Two or three rails are the classroom standard. Any count from 2 up to one less than the message length rearranges the letters, while a fence with at least as many rails as letters leaves the text unchanged. More rails scatter the letters further but add no real security, because there are still very few possibilities to try.
What does the offset do?
It starts the zigzag part-way through its down-and-up cycle instead of on the top rail. With r rails the cycle has 2(r − 1) positions, so the offset runs from 0 to 2r − 3. The sender and the receiver must use the same offset.
Should I keep spaces in a rail fence cipher?
Textbook examples remove spaces and punctuation before encoding, which also hides the word lengths, and the tool does the same by default. If you keep them, they ride the fence like letters: the cipher text then contains scattered spaces, and whoever decodes it has to keep spaces too or the rail lengths will not match.
Is the rail fence cipher secure?
No. Its letter frequencies match plain English, which gives it away, and the number of possible keys is tiny — the brute-force option tries every rail count in one go. It is good for puzzles, escape rooms and learning how transposition works, not for anything private.

Related Tools

More in Codes & Ciphers

See all Codes & Ciphers