Rail Fence Cipher

Encrypt and decrypt text with the Rail Fence cipher using a configurable number of rails. Learn how zigzag transposition works and experiment with different rail counts.

Input
0 chars · 0 bytes
Try:
Result
✓ Classical transposition cipher ✓ Customizable rail count ✓ We never store your messages ✓ Processed on our server
Examples
Encrypt with 3 rails Shift: 3
Input WEAREDISCOVEREDFLEEATONCE
Output WECRLTEERDSOEEFEAOCAIVDEN

A classic Rail Fence example with three rails.

Decrypt with 3 rails Shift: 3
Input WECRLTEERDSOEEFEAOCAIVDEN
Output WEAREDISCOVEREDFLEEATONCE

The same rail count is required to restore the plaintext.

Encrypt with 4 rails Shift: 4
Input DEFENDTHEEASTWALLOFTHECASTLE
Output DNETLHSEEDHESWLFTEATEFATAOLC

Changing the rail count changes the zigzag pattern and the resulting ciphertext.

Message with spaces Shift: 3
Input MEET AT NOON
Output M NETA ONETO

Spaces are preserved and participate in the zigzag transposition pattern like other characters.

How the Rail Fence cipher works

The Rail Fence cipher is a classical transposition cipher that rearranges the positions of characters without changing the characters themselves. The message is written in a zigzag pattern across a chosen number of rails and then read row by row to produce the ciphertext.

For example, with three rails, the text moves diagonally down and up across the rows. Increasing the number of rails creates a different pattern and therefore a different ciphertext.

To decrypt a message, the same rail count must be used. The zigzag structure is reconstructed first, then the characters are placed back into their original positions.

Rail Fence zigzag example

With three rails, the message is written diagonally down and up before the rows are read from left to right:

W . . . E . . . C
. E . R . D . S .
. . A . . . I . .

The dots show empty positions in the zigzag pattern. The ciphertext is created by reading the first rail, then the second rail, then the third rail.

Why Rail Fence is different

Unlike substitution ciphers such as Caesar or Vigenere, Rail Fence does not replace letters with other letters. It only changes their order.

Because the original characters remain unchanged, frequency analysis is less useful, but the cipher is still easy to break by testing possible rail counts. Today Rail Fence is mainly used for education, puzzles, and introducing the concept of transposition ciphers.

FAQ

No. Rail Fence does not replace letters. It changes only their order, so it is a transposition cipher.

The key is the number of rails used for the zigzag pattern. The same number is needed for decryption.

No. It is easy to test possible rail counts automatically. Today it is mainly useful for learning, puzzles and demonstrations.

Using a different rail count during decryption produces incorrect text because the zigzag pattern will not match the original encryption process.

Yes. Rail Fence is one of the best-known transposition ciphers. It keeps the original characters but rearranges their positions according to a zigzag pattern.
Related tools

Caesar Cipher

Classic letter-shift cipher with custom shift values.

Playfair Cipher

Classic digraph substitution cipher with keyword matrix encryption.

Vigenere Cipher

Keyword-based polyalphabetic encryption and decryption.

Vernam Cipher

XOR-based Vernam encryption with Base64 output.