Leandro Junes

Rail Fence Cipher

Explore transposition using multiple rails.

The Rail Fence Cipher writes letters along a zigzag path across several rails, then reads each rail from left to right. Transposition: letter positions change, but the letters themselves do not.

Calculator

—
Mode

Zigzag visualization

—

The direction reverses whenever the path reaches the top or bottom rail.

How was this decrypted?

Why is Rail Fence a transposition cipher?

Because nothing is replaced. Every letter of the message is still in the ciphertext, the same number of times; only the order has changed. A substitution cipher does the opposite: it keeps the order and changes the letters.

That is why the letter frequencies of a Rail Fence ciphertext are exactly those of the message, and why counting letters, which breaks a Caesar cipher, says nothing here about the number of rails.

How does the zigzag pattern work?

The path starts on the top rail and goes down one rail per letter. At the bottom rail it turns and climbs one rail per letter; at the top it turns again. With r rails a full trip down and back takes 2(r − 1) letters, and then the pattern repeats.

How does decryption reconstruct the text?

The zigzag depends only on the length of the message and the number of rails, not on the letters. So the receiver can draw the empty zigzag, count how many places each rail has, and cut the ciphertext into stretches of exactly those lengths: the first stretch is the top rail, the next the second rail, and so on.

Walking the zigzag again and taking the next letter of whichever rail the path is on gives the message back, exactly. The section above this one shows those steps for the current text.

Why is the Rail Fence Cipher easy to break?

The only key is the number of rails, and there are very few that matter: with as many rails as letters, or more, the text is not changed at all. An attacker simply tries every count and reads the results; only the right one is language.

Break mode does that and judges each result by how English its letter sequences look. Sequences, not single letters: every rail count gives the same letter frequencies.

The Caesar, Vigenère, Substitution, Playfair and Hill ciphers all replace letters. Unlike substitution ciphers, Rail Fence changes the positions of characters rather than replacing them.