Skip to main content
Home/Tools/Security/Rail Fence Cipher Tool

Rail Fence Cipher Tool

Transposition cipher that writes text in zigzag pattern.

100% Private - Runs Entirely in Your Browser
No data is sent to any server. All processing happens locally on your device.
Loading Rail Fence Cipher Tool...
Loading interactive tool...

Exploring Transposition Ciphers?

Our security team understands both classical and modern cryptographic techniques.

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.

What Is the Rail Fence Cipher?

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.

How the Rail Fence Cipher Works

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.

Worked Example

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.

History

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.

When to Use Rail Fence

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.

Security and Cryptanalysis

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.

  • Brute force — try every N from 2 to about 12; the correct one produces readable English.
  • Frequency analysis confirmation — Rail Fence preserves the single-letter frequency distribution exactly. If ciphertext shows English-like letter frequencies but is unreadable, transposition (likely Rail Fence or columnar) is the first thing to try.
  • Anagram-and-score — modern solvers score each candidate N by language-model likelihood.

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.

Related Ciphers

  • Caesar cipher — a substitution baseline to contrast with Rail Fence's transposition.
  • Vigenère cipher — a polyalphabetic substitution often stacked with Rail Fence in puzzles for added layers.
  • Playfair cipher — another classical hand cipher, but a digraph substitution rather than transposition.

If you have ciphertext with English-like letter frequencies but no readable words, transposition is likely — start with the Cipher Identifier to confirm.

⚠️ Security Notice

This tool is provided for educational and authorized security testing purposes only. Always ensure you have proper authorization before testing any systems or networks you do not own. Unauthorized access or security testing may be illegal in your jurisdiction. All processing happens client-side in your browser - no data is sent to our servers.