How to use the rail fence cipher tool
- Choose Encrypt or Decrypt.
- Type or paste the message. By default only letters are used and they are turned into capitals; untick this to keep spaces, punctuation and case.
- Set the number of rails from 2 to 50 and, if you need it, an offset for where the zigzag begins.
- Read the result and the table showing which characters sit on each rail.
Formula
Write the message in a zigzag across r rails: down to the last rail, back up to the first, and repeat
The cycle length is 2 (r - 1) characters
Encrypt: read the rails one after another, top to bottom
Decrypt: count the characters on each rail, cut the ciphertext into those pieces, then read the zigzag in order
The offset shifts where the first character falls in the cycle, so offset 0 starts on the top rail.
A well-known message on 3 rails
The message WE ARE DISCOVERED FLEE AT ONCE has 25 letters. On 3 rails the top rail holds W, E, C, R, L, T, E, the middle rail holds E, R, D, S, O, E, E, F, E, A, O, C and the bottom rail holds A, I, V, D, E, N. Reading the rails in order gives WECRLTEERDSOEEFEAOCAIVDEN, and decrypting that text gives WEAREDISCOVEREDFLEEATONCE.
With letters and spaces kept, Meet me at the old bridge on 4 rails becomes Meh eem tedbge a lrdttoi. The message HELLO WORLD on 3 rails with an offset of 2 becomes LOELWRDHOL.
How the zigzag works
A rail fence cipher is a transposition cipher: it does not change the letters, it only moves them. Imagine a fence with a few horizontal rails. You lay the message along the fence in a wave, then read each rail from left to right to get the new order. With only two rails the result simply separates the odd and even letters.
Because every letter survives, the letter counts of the ciphertext match the original. That is a clue for breaking it, and you can compare counts with the letter frequency counter. For ciphers that replace letters instead, try the Caesar cipher or the Playfair cipher tool.
Breaking and using it
The secret is just the number of rails and the offset. A short message has few possibilities, so a person with a pencil can try them all. That is why the cipher is used for puzzles, geocaching, escape rooms and teaching, and never for private information. A longer message does not help much: the number of keys stays small.
If you hold the ciphertext and no key, try each rail count from 2 up and read the output until it makes sense. You can also scramble letters for a game with the word scramble maker, or turn text into dots and dashes with the Morse code translator.
Assumptions and limits
- Messages can be up to 5,000 characters.
- The number of rails must be between 2 and 50, and the offset must be below 2 (rails - 1).
- When only letters are kept, digits, spaces and punctuation are removed and the output is in capitals.
- There is no padding: the ciphertext is exactly as long as the message.
Frequently asked questions
What is a rail fence cipher?
A transposition cipher that writes the message in a zigzag over several rails and reads off each rail in turn.
How do I decrypt without the key?
Try each number of rails from 2 upward. The right one gives readable text.
What does the offset do?
It moves the starting point of the zigzag. Most puzzles use offset 0, which starts on the top rail.
Is it secure?
No. There are very few possible keys, so it is for puzzles and lessons only.