Rail Fence Cipher

Zig-zag transposition over N rails — visual rail display shows the diagonal pattern.

3
Plaintext
Ciphertext
Enter plaintext and adjust the number of rails.

How to Use the Rail Fence Cipher Tool

  1. Choose Encrypt or Decrypt — select the direction with the option chips.
  2. Set the number of rails — drag the slider to choose 2–10 rails.
  3. Enter your text — type or paste plaintext or ciphertext.
  4. View the result — the encrypted or decrypted text appears on the right.
  5. Visualize — click the Visualize chip to see the zig-zag rail pattern.

About the Rail Fence Cipher

The Rail Fence cipher (also called the Zig-Zag cipher) is one of the simplest transposition ciphers. Instead of substituting letters, it rearranges them by writing the message in a zig-zag pattern across a number of "rails" (rows), then reading off the rails one at a time to produce the ciphertext. The name comes from the visual resemblance to a split-rail fence: the plaintext characters form a zig-zag pattern that looks like the rails of a fence when displayed.

How Encryption Works

With N rails and the message "WE ARE DISCOVERED FLEE AT ONCE" (spaces removed: WEAREDISCOVEREDFLEEATONCE), write the letters in a diagonal zig-zag: starting at rail 1, moving down to rail N, then back up to rail 1, repeating throughout. With 3 rails: Rail 1 receives positions 0, 4, 8, 12... (every "top" of the zig-zag); Rail 2 receives the intermediate positions; Rail 3 receives the "bottom" of each V. Read all of rail 1 first, then rail 2, then rail 3, and concatenate to get the ciphertext.

Decryption

Decryption requires knowing the number of rails. Given the ciphertext length and rail count, you can calculate exactly how many characters fall on each rail. Assign positions back to rails, then fill them in order, then reconstruct the plaintext by reading the characters at each position in their original zig-zag order.

Security Analysis

The Rail Fence cipher provides minimal security. Since it is a pure transposition cipher, individual letter frequencies are preserved exactly — the ciphertext has the same letter frequencies as the plaintext. An attacker can try all possible rail counts (at most N/2 for a message of length N) to find the one that produces recognizable English. The cipher is interesting as an educational tool and as a component in more complex cipher systems, but should never be used for actual security needs.

Historical Context

Transposition ciphers like Rail Fence have been used since ancient times. The ancient Spartan scytale was a cylinder-based transposition cipher. During the American Civil War, the Union Army used a form of route cipher (a more complex transposition). The Rail Fence cipher itself is primarily a textbook example that illustrates the concept of transposition — rearranging rather than replacing characters. When combined with a substitution cipher, transposition significantly increases cryptographic strength, as Claude Shannon formalized in his 1949 theory of secrecy systems.

Frequently Asked Questions

The Rail Fence cipher is a transposition cipher where plaintext is written diagonally in a zig-zag pattern across N rails (rows), then read off row by row. It gets its name from the visual resemblance to a fence made of rails.
The number of rails is the key. With 2 rails, odd and even positions alternate. More rails create a longer zig-zag period. The maximum useful rails is half the plaintext length.
Write the plaintext in a zig-zag across N rails, then read each rail left to right and concatenate them. For 3 rails and 'HELLOWORLD': rail 1 = H, O, L; rail 2 = E, L, W, R, D; rail 3 = L, O → ciphertext: HOLEL WRDLO.
No. It's a pure transposition cipher — letter frequencies are preserved. An attacker can try all possible rail counts quickly. It's primarily used as an educational example, not for real security.
Rail Fence uses a fixed zig-zag pattern with only the number of rails as the key. Columnar transposition uses a keyword to determine column reading order, offering a much larger key space and greater security.