Logo

MonoCalc

/

Rail Fence Cipher

Encode/Decode

Ciphertext

24 chars3 rails
WECRUOERDSOEERNTNEAIVDAC

Zigzag Diagram

24 characters

Rail 0:

W
E
C
R
U
O

Rail 1:

E
R
D
S
O
E
E
R
N
T
N
E

Rail 2:

A
I
V
D
A
C

Rail 0

Rail 1

Rail 2

Rail Contents

Rail 0WECRUO6 chars
Rail 1ERDSOEERNTNE12 chars
Rail 2AIVDAC6 chars

24

Input chars

3

Rails used

12

Longest rail

0

Offset

About This Tool

🏗️ 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:

  1. Start at Rail 0, move diagonally downward one rail at a time until reaching Rail N−1.
  2. Reverse direction and move diagonally upward back to Rail 0.
  3. Repeat until all characters are placed.
  4. Read each rail left-to-right and concatenate all rails to produce the ciphertext.

For example, encoding WEAREDISCOVEREDRUNATONCE with 3 rails:

RailCharactersCollected
Rail 0W . . . E . . . C . . . R . . . L . . . A . . . EWECRLAE
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:

  1. Compute rail lengths — run the same zigzag formula over the ciphertext length to determine how many characters belong to each rail.
  2. 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.

Frequently Asked Questions

Is the Rail Fence Cipher free?

Yes, Rail Fence Cipher is totally free :)

Can I use the Rail Fence Cipher offline?

Yes, you can install the webapp as PWA.

Is it safe to use Rail Fence Cipher?

Yes, any data related to Rail Fence Cipher only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

What is the Rail Fence cipher and how does it work?

The Rail Fence cipher is a transposition cipher that writes your message in a zigzag pattern across a number of 'rails' (rows), then reads each rail left-to-right to form the ciphertext. For example, with 3 rails, the letters alternate down and up across the rows before being concatenated.

How do I choose the number of rails?

Choose any integer from 2 to 20. More rails create a more complex zigzag pattern. If the rail count equals or exceeds the text length, no transposition occurs and the cipher is trivially broken. Rail count 2 is the simplest variant; 3–5 rails are most commonly used in puzzles and CTF challenges.

What is the Offset (shift) option?

The offset shifts the starting position in the zigzag cycle. With offset 0 the first character lands on rail 0; with offset 2, the zigzag effectively starts partway through its cycle, producing a different ciphertext from the same plaintext and rail count. Valid offsets are 0 to 2×(rails−1)−1.

Is the Rail Fence cipher secure?

No. The Rail Fence cipher provides very little security by modern standards. With a small key space (2–20 rails) and no confusion, it is trivially brute-forced. It is best used for educational purposes, puzzle games, CTF competitions, and escape rooms — never for protecting sensitive data.

Does the tool handle spaces and punctuation?

By default, all characters (spaces, punctuation, digits) are included in the zigzag and preserved in the output. Enable 'Ignore Spaces' to strip whitespace before encoding; the tool will then re-insert spaces at their original positions after decoding.

Can I decode a Rail Fence ciphertext back to the original message?

Yes. Switch to Decode mode, paste your ciphertext, and enter the same number of rails (and offset if used) that was used to encode it. The tool reconstructs the original message by computing rail lengths and reversing the zigzag index mapping.