Xerobit

Rail Fence Cipher Encoder and Decoder

The rail fence cipher rearranges text by writing it in a zigzag pattern across a set number of rails, then reading each row left to right. Use this tool as a rail fence cipher encoder or rail fence cipher decoder by selecting the direction, entering your message, and choosing the number of rails. Everything runs in your browser and nothing is uploaded.

Result

WECRLTEERDSOEEFEAOCAIVDEN
  • Ciphertext
    WECRLTEERDSOEEFEAOCAIVDEN
  • Rails
    3
  • Length
    25
RailGrid
1W...E...C...R...L...T...E
2.E.R.D.S.O.E.E.F.E.A.O.C.
3..A...I...V...D...E...N..

How to use the rail fence cipher

  1. Enter your plaintext or ciphertext in the Text field.
  2. Select Encode to encrypt or Decode to decrypt.
  3. Choose the number of Rails between 2 and 10.
  4. 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:

RailCharacters
0W . . . 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.

RailsRail 0Rail 1Rail 2Rail 3Rail 4
21312
37126
45884
546663

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

ExampleInputResult
Classic example (3 rails)Text: WEAREDISCOVEREDFLEEATONCE; Direction: encode; Rails: 3Ciphertext: WECRLTEERDSOEEFEAOCAIVDEN; Rails: 3; Length: 25
Decode with 3 railsText: WECRLTEERDSOEEFEAOCAIVDEN; Direction: decode; Rails: 3Plaintext: WEAREDISCOVEREDFLEEATONCE; Rails: 3; Length: 25
Two rails encodeText: HELLO WORLD; Direction: encode; Rails: 2Ciphertext: HLOWRDEL OL; Rails: 2; Length: 11
Five rails encodeText: DEFEND THE EAST WALL; Direction: encode; Rails: 5Ciphertext: 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.