Rail Fence Cipher

Zig-zag transposition cipher across N rails.

Open tool

Overview

The rail fence cipher is a transposition cipher that writes your message in a zig-zag across N rails (rows), then reads it row by row to produce the ciphertext. It doesn't replace any letters — it just rearranges them — which makes it a "transposition" rather than a substitution.

Cryptography students, CTF players, and history buffs studying American Civil War-era ciphers reach for it. It's also a common puzzle stop on ARGs and escape rooms, since the encoded text looks scrambled but contains every original letter, making it solvable by anyone willing to draw the zig-zag.

How it works

Choose a number of rails N. Write the plaintext left to right, moving diagonally down and back up between rails like a zig-zag fence. Once the message is written, read it off row by row, top to bottom.

Worked example: encoding "HELLOWORLD" on 3 rails:

Rail 1: H . . . O . . . L .
Rail 2: . E . L . W . R . D
Rail 3: . . L . . . O . . .

Reading row by row: "HOL" + "ELWRD" + "LO" = "HOLELWRDLO".

To decode, you need to know N. Reconstruct the zig-zag pattern, fill the rails with the cipher letters in order, then read along the zig-zag to recover the original.

Examples

Plaintext: HELLOWORLD     Rails: 3
Ciphertext: HOLELWRDLO
Plaintext: WEAREDISCOVERED   Rails: 4
Ciphertext: WSREDCVAIODEEER

FAQ

Is it secure?

No. The key is just the number of rails — typically 2 to 10 — so a brute-force attack tries every plausible value in seconds. Combine it with another cipher if you need real security.

Why is it called a "fence"?

The zig-zag path traced by the plaintext as you write it across the rails looks like the rails of a fence. The name dates to Civil War-era American codebooks.

Can the rails change mid-message?

Standard rail fence keeps N constant. Variants like the redefence cipher add a permutation step to make the attack harder, at the cost of complexity.

Try Rail Fence Cipher

An unhandled error has occurred. Reload ×