The Rail Fence cipher is a transposition cipher — instead of replacing letters with different letters (like a substitution cipher does), it rearranges the positions of the letters. The letters themselves stay exactly the same; only their order changes.
Imagine writing your message on a series of rails (horizontal lines), with a ball bouncing diagonally down and then back up, placing one letter on each rail as it goes. When it reaches the bottom rail, it bounces back up; when it reaches the top rail, it bounces back down. After placing all the letters, you read off each rail from left to right, top to bottom, to produce the ciphertext.
This shows how the letters are placed on the rails. Read each row from left to right, top to bottom, to get the ciphertext.
Compare the letter frequencies of the ciphertext to standard English. Notice anything? Because this is a transposition cipher, the frequencies are identical to the plaintext frequencies — we only moved letters around, we never changed them. This is a major weakness: frequency analysis immediately tells an attacker that no substitution was used.
To encrypt with a Rail Fence cipher:
For example, "HELLO WORLD EXAMPLE" with 3 rails:
Rail 0: H . . . O . . . L . . . A . . . E Rail 1: . E . L . W . R . D . X . M . L . Rail 2: . . L . . . O . . . E . . . P . .
Reading off each rail: HOLAE + ELWRDXML + LOEP = HOLAEELWRDXMLLOEP
To decrypt, you reverse the process: figure out how many letters belong on each rail (based on the zigzag pattern), fill the ciphertext back into the rails, then read in zigzag order to recover the plaintext.
Most ciphers you will encounter in this series (Caesar, Substitution, Vigenere) are substitution ciphers — they replace each letter with a different letter. The Rail Fence cipher is a transposition cipher: the letters are the same, just rearranged into a different order.
This distinction matters because the two types of ciphers have different strengths and weaknesses. Transposition ciphers preserve letter frequencies, which means frequency analysis will not help you figure out which letters were substituted — because none were. However, an attacker who notices the frequencies match normal English immediately knows a transposition cipher was used, not a substitution cipher.
Disclaimer: these pages are educational demos provided as-is, with no warranty of any kind. The author is not responsible for any consequences arising from their use.
Send comments and bug reports to chris@chrisspackman.com.
Version 0.1 — Last updated: 2026-02-21
This page is Copyright © 2025 – 2026
Chris Spackman <chris@chrisspackman.com>.
This web site developed entirely on GNU/Linux with Free / Open Source Software.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.