Free online Rail Fence cipher tool with visual zigzag diagram. Encrypt and decrypt text using transposition with configurable rail count and offset.
Not sure which cipher you have? Use the Cipher Identifier to auto-detect cipher types from unknown ciphertext using frequency analysis and Index of Coincidence.
Rail Fence is a classical transposition cipher: it does not substitute any letters, it only rearranges them. The plaintext is written in a zigzag pattern across a fixed number of "rails" (rows) and then read off row by row to produce the ciphertext. Because no letter is changed, the ciphertext has exactly the same letter frequencies as the plaintext — a strong fingerprint for cryptanalysis.
Pick a number of rails N (typically 2–6). Write the plaintext one character at a time, moving diagonally down the rails until you hit the bottom, then diagonally up until you hit the top, and so on — tracing a zigzag. After all letters are placed, read across each rail left-to-right and concatenate the rows.
The period of the zigzag is 2(N − 1). For 3 rails, the period is 4; for 4 rails, 6; for 5 rails, 8.
Encrypt "WEAREDISCOVEREDFLEEATONCE" with N = 3 rails:
| Rail 1 | W . . . E . . . C . . . R . . . L . . . T . . . E | | Rail 2 | . E . R . D . S . O . E . E . F . E . A . O . C . | | Rail 3 | . . A . . . I . . . V . . . D . . . E . . . N . . |
Read off each rail: WECRLTE + ERDSOEEFEAOC + AIVDEN
Ciphertext: "WECRLTEERDSOEEFEAOCAIVDEN"
To decrypt, the receiver must know N. They compute the length of each rail by simulating the zigzag with placeholders, slice the ciphertext into rail-sized chunks, and walk the zigzag reading from those chunks to reconstruct the plaintext. Some variants add a starting offset, slightly enlarging the key space.
Rail Fence is an ancient transposition technique with no single inventor. Transposition ciphers date back to the Spartan scytale (~5th century BCE), where a strip of parchment wound around a wooden rod produced a similar rearrangement. Rail Fence in its modern named form is a 19th-century pedagogical staple that became a fixture of puzzle books and introductory cryptography texts.
Educational and recreational use only. It is a standard early example in cryptography courses because it cleanly illustrates the difference between substitution and transposition: the ciphertext looks scrambled but each letter is preserved. It also appears regularly in CTF challenges as one layer in a stacked cipher, in escape rooms and puzzle hunts, and in geocaching coordinates. Do not use it for any real security purpose.
Rail Fence is among the weakest classical ciphers. The key is just a small integer (the rail count), so the entire keyspace can be brute-forced by trying every plausible N (typically 2 through ~12) and checking which result looks like English.
The Index of Coincidence is unchanged from plaintext (~0.067 for English), which is itself the diagnostic: an IoC consistent with English combined with unreadable text strongly suggests transposition.
If you have ciphertext with English-like letter frequencies but no readable words, transposition is likely — start with the Cipher Identifier to confirm.