🏗️ Rail Fence Cipher – Zigzag Transposition Cipher Online
The Rail Fence cipher (also called the zigzag cipher) is a classical transposition cipher that rearranges the characters of a plaintext by writing them diagonally across a set of imaginary "rails" and then reading the result row by row. It is widely taught in introductory cryptography courses and is a staple of CTF (Capture the Flag) competitions and puzzle games.
How the Rail Fence Cipher Works
Given a plaintext and a rail count N, the cipher writes each character of the message onto one of N rails in a repeating zigzag pattern:
- Start at Rail 0, move diagonally downward one rail at a time until reaching Rail N−1.
- Reverse direction and move diagonally upward back to Rail 0.
- Repeat until all characters are placed.
- Read each rail left-to-right and concatenate all rails to produce the ciphertext.
For example, encoding WEAREDISCOVEREDRUNATONCE with 3 rails:
| Rail | Characters | Collected |
|---|---|---|
| Rail 0 | W . . . E . . . C . . . R . . . L . . . A . . . E | WECRLAE |
| Rail 1 | . E . R . D . S . O . E . E . R . N . T . N . C . | ERDSOEERNTC |
| Rail 2 | . . A . . . I . . . V . . . D . . . U . . . O . . | AIVDUO |
Ciphertext: WECRLAEERDSOEERNTNCAIVDUO
Encoding Formula
The rail assignment for character at position i is determined by:
- Period:
p = 2 × (rails − 1) - Cycle position:
pos = (i + offset) mod p - Rail:
pos < rails ? pos : p − pos
This pure-arithmetic formula makes the Rail Fence cipher extremely fast to compute even for large texts, with O(n) time complexity.
Decoding
Decoding reverses the process in two steps:
- Compute rail lengths — run the same zigzag formula over the ciphertext length to determine how many characters belong to each rail.
- Distribute and reconstruct — split the ciphertext into rail buckets according to those lengths, then read them back in zigzag order to recover the original message.
The Offset (Shifted) Variant
The offset parameter shifts the starting position in the zigzag cycle. Without an offset the first character always lands on Rail 0. With an offset of k, the cycle starts k positions into the period — producing a different ciphertext from the same plaintext and rail count. Valid offsets range from 0 to 2×(rails−1)−1. Always use the same offset for encoding and decoding.
Security Considerations
The Rail Fence cipher has an extremely small key space — only 2–20 meaningful rail counts for typical texts — making it trivially brute-forceable. It provides no resistance to frequency analysis since it only rearranges characters without substituting them. It should be used exclusively for:
- 📚 Education and cryptography coursework
- 🧩 Puzzle games, escape rooms, and CTF challenges
- 🎮 Obfuscation in games and fun encodings
Never use the Rail Fence cipher to protect sensitive data. For real security, use a modern authenticated encryption algorithm such as AES-GCM.
Tips for Using This Tool
- Enable Ignore Spaces to strip whitespace before encoding — useful when the receiving party will re-add spaces after decoding.
- Use the ⇄ Swap button to instantly flip the result into the input field and switch between Encode and Decode mode in one click.
- The Zigzag Diagram visually shows which character lands on which rail, making it ideal for step-by-step learning.
- If you are decoding a CTF ciphertext and do not know the rail count, try rail values 2 through 10 — the correct one will produce readable plaintext.