StringMash.com

Rail fence cipher

Write the message in a zigzag, then read it off one row at a time.

Conversion
30 characters
Updates as you type
3 rails by default

Using the rail fence tool

Type on the left and the reordered text appears on the right. Rails sets how deep the zigzag goes, from 2 to 10, and it's the whole key. Swap the boxes to read a message back.

Raise the rail count and the letters scatter further from where they started. Set it as high as the message is long and nothing moves at all, because every letter gets a rail to itself.

Why this one is different

Every other cipher on this site replaces letters. This one keeps them and changes the order. Write the text diagonally down and up across a set of rails, then read each rail left to right.

That distinction matters when you're trying to break it. A substitution cipher changes the letter frequencies, so counting letters tells you something. A transposition doesn't. The output of a rail fence has exactly the same letters as the input, in the same proportions, so a frequency count comes out looking like ordinary English.

It also means the cipher is easy to spot and easy to break. If a scrambled message has English letter frequencies, it's a transposition. There are only nine rail counts worth trying here, so the key space is smaller than the Caesar cipher's 25 shifts.

A cipher for speed, not for safety

Transposition by zigzag is old and anonymous. No inventor is recorded, and it turns up in 19th-century puzzle books as a child's exercise rather than a military tool.

Its one real virtue is that it needs no equipment. A soldier with a stick and a patch of dirt can run a rail fence, and the key is a single digit that fits in a memory under pressure. That put it in the field during the American Civil War, usually as one step inside a longer hand cipher rather than on its own.

Modern block ciphers still transpose. AES calls the step ShiftRows, and it moves bytes around the state in a fixed pattern for the same reason the rail fence does, to spread a change in one place across the whole block. The difference is that AES alternates transposition with substitution ten or more times.

Related conversions

Caesar and Atbash are the substitution side of the family. Run one of them after a rail fence and you get something much harder to read than either alone, which is the principle behind every modern cipher.

Text that looks like an anagram of English is usually a transposition, and the cipher identifier checks for that.

Questions

What is the rail fence cipher?

A transposition cipher. The message is written in a zigzag across a number of rails, then read off one rail at a time, so the letters are reordered rather than replaced.

How many rails should I use?

Three is the usual default and the one most puzzles expect. More rails scatter the letters further, but the key space stays tiny either way.

How do you break a rail fence cipher?

Try every rail count until the text reads. There are rarely more than about 10 worth testing, so it takes under a minute by hand.

Why do spaces and punctuation disappear?

They're stripped before the zigzag, because a space left in place would show word boundaries and give away the pattern. The letters are what the fence reorders.