How to use the rail fence cipher
- Choose the Direction: Encrypt or Decrypt.
- Set Rails with the slider, from 2 to 10. The default is 3.
- Leave Remove spaces and punctuation first ticked for the classic style, or untick it to keep spaces as part of the pattern.
- Type into Your text and copy the Result. To decrypt, use the same number of rails and the same tick-box setting that made the message.
What it does and when to use it
The rail fence is a transposition cipher: it keeps every letter but shuffles their order. It is a favourite first cipher in classrooms because you can do it with pencil and squared paper, and it shows clearly how transposition differs from substitution ciphers such as the Caesar cipher, which replace letters instead.
Like all classical ciphers, it is for puzzles and learning only and does not protect real information. The letter counts never change, so a frequency check instantly shows it is a transposition, and the small number of rail settings can be tried by hand. For a transposition with a keyword, try the columnar transposition cipher; the scytale cipher is an even older grid method.
How it works
Picture the rails as rows. The first character goes on rail 1, the next on rail 2, and so on down to the last rail, then the path turns and climbs back up, repeating in a zig-zag. With 3 rails the pattern of rails is 1, 2, 3, 2, 1, 2, 3, 2…
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 . .
Encryption reads rail 1 left to right, then rail 2, then rail 3: WECRLTE + ERDSOEEFEAOC + AIVDEN. Decryption works out how many characters belong on each rail from the message length, cuts the ciphertext into those pieces, and follows the zig-zag again to put each character back. The tool does this in your browser; it works on any characters, not only A to Z.
Worked examples
Results from the tool:
WE ARE DISCOVERED. FLEE AT ONCE 3 rails → WECRLTEERDSOEEFEAOCAIVDEN
Meet me at 10 2 rails → Mema1etet0
Me ea 0etm t1 2 rails, decrypt, → Meet me at 10
box unticked
The first is the textbook example, with the spaces and full stop removed. In the second, two rails simply take every other character: M, e, m, a, 1 then e, t, e, t, 0. The third was encrypted with spaces kept, so it decrypts correctly only with the box unticked.
Limits and tips
- Short messages on many rails barely change. Aim for a message at least three or four times as long as the number of rails.
- Removing spaces makes the result harder to read at a glance, but the person decrypting has to add the word breaks back.
- Agree on the rail count and the space setting in advance; both must match to decrypt.
Frequently asked questions
How do I decrypt a rail fence message if I do not know the rails?
Why does my decrypted text look scrambled even with the right rails?
Does it keep capital letters and digits?
Why is my text unchanged?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.