🚧 Rail Fence Cipher

Encrypt and decrypt text using the Rail Fence cipher. Zigzag transposition cipher with configurable number of rails.

About the Rail Fence Cipher

The Rail Fence cipher is a classical transposition cipher that writes the plaintext in a zigzag pattern across several imaginary "rails", then reads off each rail to form the ciphertext. Unlike substitution ciphers, it does not change which letters appear — only their order. This tool runs entirely in your browser — nothing is uploaded.

How it works

  • Encrypt — The plaintext is written diagonally up and down across N rails. The ciphertext is formed by reading each rail left to right, top to bottom.
  • Decrypt — The tool reconstructs the zigzag pattern, determines how many characters belong to each rail, slices the ciphertext accordingly, and reads the rails in zigzag order.
  • With 3 rails and plaintext HELLOWORLD, the zigzag looks like:
    H . . . O . . . L . .
    . E . L . W . R . D .
    . . L . . . O . . . .
    Reading each rail gives HOLELWRDLO.
  • More rails create a more complex pattern but do not change the underlying security.

Use cases

  • Learning the difference between transposition and substitution ciphers.
  • Classical cryptography exercises and CTF puzzles.
  • Combining with substitution ciphers for product ciphers.
  • Demonstrating why reordering alone is not secure.

Frequently asked questions

Is the Rail Fence cipher secure? No. The key space is small (just the number of rails), and the cipher preserves letter frequencies, making it easy to break.

What is the best number of rails? There is no "best" — more rails produce a more complex pattern but still leave the cipher weak. Typical classroom examples use 2 or 3 rails.

Does the cipher affect non-letters? No. Spaces, digits, and punctuation are treated like any other character and shuffled along with the letters.

Does this tool upload my text? No. All encryption and decryption happens locally in your browser.