How to use the rail fence cipher
- Enter your plaintext or ciphertext in the Text field.
- Select Encode to encrypt or Decode to decrypt.
- Choose the number of Rails between 2 and 10.
- Read the output text and inspect the zigzag grid below it.
How the Rail Fence Cipher Works
The rail fence cipher, also called the zigzag cipher, is a transposition cipher that rearranges characters without changing them. To encode, write the message diagonally down across a given number of horizontal rails, bouncing back up when you hit the bottom, then read off each rail from left to right.
For example, encoding WEAREDISCOVEREDFLEEATONCE with 3 rails produces this zigzag pattern:
| Rail | Characters |
|---|---|
| 0 | W . . . E . . . C . . . R . . . L . . . T . . . E |
| 1 | . E . R . D . S . O . E . E . F . E . A . O . C . |
| 2 | . . A . . . I . . . V . . . D . . . E . . . N . . |
Reading rail 0, then rail 1, then rail 2 gives the ciphertext: WECRLTEERDSOEEFEAOCAIVDEN.
Decoding Step by Step
To use the tool as a rail fence cipher decoder, you must reverse the process. First, determine how many characters belong on each rail by simulating the zigzag path for the length of the ciphertext. Then split the ciphertext into those segments and place them back onto their respective rails. Finally, read the characters off in the original zigzag order.
Using the ciphertext WECRLTEERDSOEEFEAOCAIVDEN with 3 rails: the lengths are 7, 12, and 6. Rail 0 gets WECRLTE, rail 1 gets ERDSOEEFEAOC, and rail 2 gets AIVDEN. Tracing the zigzag path through these rails reconstructs WEAREDISCOVEREDFLEEATONCE.
Common Rail Lengths Reference
The table below shows how a 25-character message distributes across different rail counts. This helps verify your zigzag cipher setup before decoding manually.
| Rails | Rail 0 | Rail 1 | Rail 2 | Rail 3 | Rail 4 |
|---|---|---|---|---|---|
| 2 | 13 | 12 | |||
| 3 | 7 | 12 | 6 | ||
| 4 | 5 | 8 | 8 | 4 | |
| 5 | 4 | 6 | 6 | 6 | 3 |
As the number of rails approaches the message length, the ciphertext becomes nearly identical to the plaintext because each rail holds only one or two characters.
Security and Practical Use
The rail fence cipher offers no real security against modern analysis. Because it is a pure transposition cipher, letter frequencies remain identical to the plaintext. An attacker can simply try every possible rail count from 2 up to the message length and check which output forms readable words.
Despite its weakness, the zigzag cipher remains popular in puzzles, capture-the-flag competitions, and introductory cryptography courses. It pairs well with substitution ciphers like the caesar cipher translator or the vigenere cipher to create a stronger combined scheme where both position and character values are altered.
Handling Spaces, Case, and Punctuation
This tool preserves spaces, punctuation, uppercase, and lowercase letters exactly as entered. Every character occupies a position in the zigzag grid. If you want a stricter classical approach, convert your text to uppercase and remove spaces first using a case converter before pasting it here.
Non-letter characters such as digits and symbols are treated identically to letters during transposition. They move to new positions but are never modified or dropped.
Worked examples
| Example | Input | Result |
|---|---|---|
| Classic example (3 rails) | Text: WEAREDISCOVEREDFLEEATONCE; Direction: encode; Rails: 3 | Ciphertext: WECRLTEERDSOEEFEAOCAIVDEN; Rails: 3; Length: 25 |
| Decode with 3 rails | Text: WECRLTEERDSOEEFEAOCAIVDEN; Direction: decode; Rails: 3 | Plaintext: WEAREDISCOVEREDFLEEATONCE; Rails: 3; Length: 25 |
| Two rails encode | Text: HELLO WORLD; Direction: encode; Rails: 2 | Ciphertext: HLOWRDEL OL; Rails: 2; Length: 11 |
| Five rails encode | Text: DEFEND THE EAST WALL; Direction: encode; Rails: 5 | Ciphertext: DHWETE AF TLEDESLNA; Rails: 5; Length: 20 |
FAQ
What is a rail fence cipher?
It is a transposition cipher that writes a message in a zigzag pattern across multiple horizontal rails, then reads each rail sequentially to produce the ciphertext.
How many rails should I use?
Between 2 and 10. Using more rails than half the message length provides almost no scrambling because most rails will contain only a single character.
Is the zigzag cipher secure?
No. It preserves letter frequencies and can be broken by trying every possible rail count. It is best used for puzzles or combined with a substitution cipher.
Does this tool work as a rail fence cipher decoder?
Yes. Select Decode from the direction dropdown, enter the ciphertext, and specify the correct number of rails to recover the original message.
Are spaces and punctuation preserved?
Yes. Every character including spaces, numbers, and symbols is placed in the zigzag grid and transposed along with the letters.
Can I use this offline?
The tool runs entirely in your browser. Once the page loads, no data is sent to any server, so it works without an internet connection.