Rail fence cipher — zig-zag encryption with 2 to 10 rails

The rail fence cipher writes your message diagonally down and up across a number of rails, then reads each rail in turn; choose the rails, encrypt, or decrypt with the same number.

Live demo · real result from this tool
Direction

Result

WECRLTEERDSOEEFEAOCAIVDEN

Processed on your device — nothing is uploaded.

How to use the rail fence cipher

  1. Choose the Direction: Encrypt or Decrypt.
  2. Set Rails with the slider, from 2 to 10. The default is 3.
  3. Leave Remove spaces and punctuation first ticked for the classic style, or untick it to keep spaces as part of the pattern.
  4. 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?
Try every value. The slider only goes from 2 to 10, so there are just nine possibilities, and the right one usually reads as plain words straight away. This is also why the cipher gives no real protection.
Why does my decrypted text look scrambled even with the right rails?
Check the Remove spaces and punctuation first box. It applies when decrypting too. If the ciphertext contains spaces because it was made with the box unticked, untick it before decrypting, or the spaces are removed and every letter after them lands in the wrong place.
Does it keep capital letters and digits?
Yes. The cipher only moves characters around; it never changes them. The tick box removes spaces and punctuation, but letters of any alphabet and digits stay.
Why is my text unchanged?
If there are as many rails as characters or more, each character sits alone on its rail and the order does not change. HELLO on 10 rails stays HELLO. Use fewer rails or a longer message.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close