Rail Fence cipher explained in simple terms
The Rail Fence cipher scrambles a message by writing it in a zig-zag wave and reading it horizontally.
Imagine writing a message like a rollercoaster.
You go up and down across the page. Every letter sits at a different height in the track. Once you're done writing the entire message, you grab your pen and read the letters row by row, ignoring the rollercoaster path you just drew.
This completely destroys the original order of the letters without actually changing any of the letters themselves. To read the message, you just have to know how tall the rollercoaster was (how many rails).
What is the Rail Fence cipher?
The Rail Fence cipher is a classical transposition cipher. It does not replace any letters; instead, it rearranges them. The text is written following a zig-zag sequence down and up across n rails (rows), and the ciphertext is formed by reading horizontally.
The Rail Fence cipher scrambles text using a visual zig-zag pattern over a set number of rails. It is widely studied in computer science to understand rail fence cipher encryption and decryption algorithms.
The mathematical period of the pattern is 2·(n−1). This means the cycle repeats exactly after that many characters. When n = 2, the cipher essentially splits the text into alternating characters and stitches them together.
How it works (rules summary)
Rail Fence cipher rules step-by-step
- 1. Set the rails: Pick a number n (usually 3 to 5). Draw n empty rows.
- 2. Write in zig-zag: Place the first letter in row 1, column 1. The next goes in row 2, column 2. Keep going diagonally down until you hit the bottom rail, then bounce back up. Repeat until the full message is mapped.
- 3. Read horizontally: Read row 1 left to right, then row 2, and so on. Concatenate all rows to get the ciphertext.
Decrypt by reversing: rebuild the skeleton, fill rows with ciphertext, then read along the zig-zag path.
The Rail Fence cipher pattern — period formula
The Rail Fence cipher pattern follows a repeating zig-zag structure that depends on the number of rails. The pattern repeats every 2·(n−1) characters.
3 Rails
Period = 2·(3−1) = 4
4 Rails
Period = 2·(4−1) = 6
5 Rails
Period = 2·(5−1) = 8
Rail Fence zig-zag visualization (3 rails)
Plaintext: R E A L F E N C E C I P H E R
Rail Fence Cipher — Solved Examples
The easiest way to understand the pattern is visually. Here are two rail fence cipher examples step by step.
Example 1 — Rail fence cipher with 3 rails
Plaintext: ATTACKATDAWN | Rails: 3 | Period: 2·(3−1) = 4
Rail 1: A . . . C . . . D . . .
Rail 2: . T . A . K . T . A . N
Rail 3: . . T . . . A . . . W .
Rail 1: ACD Rail 2: TAKTAN Rail 3: TAW
Ciphertext: ACDTAKTANTAW
Example 2 — Rail fence cipher with 4 rails
Plaintext: SECRETMESSAGE | Rails: 4 | Period: 2·(4−1) = 6
Rail 1: S . . . . . S . . . . .
Rail 2: . E . . . M . E . . G .
Rail 3: . . C . E . . . S . . E
Rail 4: . . . R . . . . . A . .
Rail 1: SS Rail 2: EMEG Rail 3: CESSE Rail 4: RA
Ciphertext: SSEMEGCESSERA
With 4 rails, letters are split across more rows — the ciphertext looks less recognizable than with 3 rails.
Example 3 — Rail fence cipher with 5 rails (mini)
Plaintext: HELLOWORLD | Rails: 5 | Period: 2·(5−1) = 8
Rail 1: H . . . . . . . H .
Rail 2: . E . . . . . O . .
Rail 3: . . L . . . R . L .
Rail 4: . . . L . W . . . .
Rail 5: . . . . O . . . . D
Rail 1: HH Rail 2: EO Rail 3: LRL Rail 4: LW Rail 5: OD
Ciphertext: HHEORLLLWOD
With 5 rails and a 10-letter message, each rail holds only 2 letters — the text is maximally scrambled but trivially brute-forced.
Example 4 — Decryption step by step
Ciphertext: HLOEL | Rails: 2
To decrypt, rebuild the zig-zag skeleton, fill rows with the ciphertext, then read along the zig-zag path.
Step 1 — Skeleton (5 chars, 2 rails): ? . ? . ? / . ? . ? .
Step 2 — Fill Rail 1 with first 3 chars (HLO): H . L . O / . ? . ? .
Step 3 — Fill Rail 2 with next 2 chars (EL): H . L . O / . E . L .
Step 4 — Read zig-zag: H-E-L-L-O
Recovered plaintext: HELLO
Use this free Rail Fence cipher calculator to visualize the matrix and scramble text instantly.
Free Rail Fence Cipher Calculator (Encrypt & Decrypt)
Watch the zig-zag build dynamically. Just type your text and change the rails.
Choose the height of the fence.
Pattern visualization
Used by students and developers to test rail fence cipher encryption and decryption algorithms.
Why Rail Fence is perfect for understanding transposition
Rail Fence is often taught as the foundational jumping-off point for transposition ciphers. It requires zero machinery—just a paper and a pencil. You trace the shape, write the letters, and extract horizontally. In seconds, an intelligible message becomes a scrambled block.
However, the key weakness is the precise mathematical period. With n=3 rails, the cycle repeats every 4 positions. Letters at index 0, 4, 8 always land on rail 1. This exact geometric predictability means an attacker doesn't even need to simulate the zig-zag to write an algorithm to break it.
Fixed Period: 2·(n−1)
The zig-zag has an exact mathematical period. With n=3, period=4. With n=5, period=8. This allows direct index calculation for any letter position.
Preserves Language Frequencies
Rail fence changes no letters. 'E' remains 'E'. The language frequency is identical to the original text, instantly signaling to an attacker that it is a transposition cipher.
The Simplest Key Possible
A single integer n ≥ 2. The keyspace is incredibly small. Modern computers can evaluate every possible rail size instantly.
Classic attacks
Knowing how to break the Rail Fence cipher is trivial: the keyspace is tiny because there is only one parameter (the rails). With enough text, brute force takes seconds.
Rail Enumeration
With a ciphertext of length L, the maximum useful number of rails is L/2. An attacker algorithmically tries n=2, 3, 4 until readable text appears.
Frequency Analysis
Checking the letter frequencies immediately confirms normal linguistic distribution, ruling out substitution ciphers like Caesar or Vigenere.
Dictionary Validation
Algorithms score the output of different rail sizes against dictionary words and common bigrams (like "TH") to auto-detect the correct key.
Pros and cons
Pros
- Highly visual and intuitive for teaching transposition.
- Very simple parameter to experiment with.
- Easy to implement in code using array indexing logic.
Cons
- Trivially easy to brute-force the single integer key.
- Preserves the exact letter frequency of the original text.
- Offers no data integrity or true cryptographic security.
How to solve the Rail Fence cipher step by step
Follow these exact steps to perform rail fence cipher encryption and decryption.
-
1.
Choose the number of rails
This integer key defines the vertical height of your zig-zag grid.
-
2.
Draw the zig-zag skeleton
Mark the path going down sequentially to the bottom rail, and bouncing back up to the top.
-
3.
Fill the matrix
Write the plaintext along the marked zig-zag path, character by character.
-
4.
Read by rows
Read the characters horizontally row by row to produce the final scrambled encrypted text.
People also search for
Keep learning: related resources
Rail Fence is the most visual base of transposition. Continue your journey to more secure algorithms:
Columnar (next level)
Rail Fence uses a number; Columnar uses a keyword. The logical next step to add variability to transposition.
Scytale (historical origin)
The Spartan scytale is the mother of all geometric transpositions. The historical context of Rail Fence.
Caesar (substitution vs trans.)
Caesar changes letters; Rail Fence merely moves them. Two completely different ways to hide information.
Vigenère (polyalphabetic)
Where Rail Fence fails (exposed frequencies), Vigenère succeeds by hiding them.
What is cryptography?
A complete historical timeline of cryptography from the Greeks to modern standards like AES.
See all ciphers
Explore Playfair, Bacon, Beaufort, Autokey, Affine, Columnar, Scytale and more.