The Rail Fence Cipher Explained: Zigzag Transposition for CTF and Class
How the rail fence cipher works: write letters in a zigzag across N rails, read them off row by row. A worked 3-rail example, why it isn't secure, and how to crack it.
The Rail Fence Cipher Explained: Zigzag Transposition for CTF and Class
The rail fence cipher is the cipher you draw on a napkin. There is no math table to memorize, no key schedule, no modular arithmetic. You pick a number of rails, write your message in a zigzag down and up across those rails, then read the letters off one row at a time. That is the whole algorithm. It is old, it is breakable in seconds, and it is one of the best teaching tools in cryptography precisely because of both.
This post walks through how it works, what a real 3-rail encryption looks like step by step, why it shows up constantly in beginner capture-the-flag puzzles, and why you should never use it to hide anything that actually matters.
What kind of cipher is this?
There are two broad families of classical ciphers. A substitution cipher swaps each letter for a different one: Caesar shifts A to D, B to E, and so on. The letters change. A transposition cipher does the opposite. It keeps every original letter and only moves it to a new position. Nothing is added, nothing is removed, nothing is replaced.
The rail fence cipher is a transposition cipher. That single fact explains most of its behavior. Because it reorders characters instead of mapping an alphabet, it treats every character identically. Spaces, commas, digits and even Chinese characters all get a slot in the zigzag and all get shuffled. A space is just another token that lands on a rail and comes back out somewhere new. This is why "strip the punctuation first" is such a common mistake: if the sender removes spaces and the receiver keeps them, the layout no longer lines up and the message decodes to garbage.
It also gives you a free sanity check. Count the letters in the ciphertext and the plaintext. With a transposition cipher they are identical. If the letter counts match but the order is jumbled, you are almost certainly looking at a transposition, not a substitution.
How the zigzag actually works
Here is the core idea in one sentence: write the text diagonally down to the bottom rail, then diagonally back up to the top, then down again, repeating that bounce until you run out of characters, and finally read each rail left to right, top to bottom.
The key is the rail count, the number of horizontal lines you bounce between. With 2 rails the path just alternates between two rows. With 5 rails the bounce is taller and the scrambling is harder to eyeball. That is the entire secret: one small integer.
A worked example with 3 rails
Let me encrypt the classic textbook string, WEAREDISCOVEREDFLEEATONCE, on 3 rails. I write the letters in a zigzag, going down then up:
Rail 1: W . . . E . . . C . . . R . . . L . . . T . . . E
Rail 2: . E . R . D . S . O . E . E . F . E . A . O . C . .
Rail 3: . . A . . . I . . . V . . . D . . . E . . . N . . .
Now I read off each rail in order. Rail 1 gives WECRLTE, rail 2 gives ERDSOEEFEAOC, and rail 3 gives AIVDEN. Stitch those rows together and the ciphertext is:
WECRLTEERDSOEEFEAOCAIVDEN
Decryption walks the same path backward. You first figure out how many letters belong on each rail (just trace the zigzag without the letters), carve the ciphertext into those rail-sized chunks, drop them back onto the rails, and then read along the original down-up-down route. The output is the plaintext, every character recovered in order.
One subtlety the tool exposes is the offset. By default the path starts at the top rail moving down. A non-zero offset starts the bounce partway through its cycle, which rearranges the whole layout. Offset acts as a small second piece of the key, but both halves must match: decrypting with the right rail count and the wrong offset still yields nonsense. If you want to try all of this live, the rail fence cipher encoder and decoder renders the zigzag grid as you type so you can watch each letter fall into place.
Why it shows up in CTFs and classrooms
Beginner CTF crypto challenges love the rail fence cipher for a reason: it is solvable without any tooling beyond a little patience, yet it looks intimidating to someone who has never seen a transposition. A challenge often hands you a scrambled flag with no key, something like fanlrieca{lfeg_}, and trusts you to recognize the pattern. The braces of flag{...} are still in there, just displaced.
The crack is almost embarrassingly direct. The rail count is the entire key and it is small, normally between 2 and roughly 10. So you try them all. Decrypt at 2 rails, then 3, then 4, and scan the candidates for the row that reads as real words or a valid flag. There is no frequency analysis, no statistical attack, because the letters were never changed. They were only reordered. When flag{...} snaps back together at 4 rails, the rail count it landed on is the answer. Brute force mode automates exactly this sweep, listing every candidate so you read off the one that makes sense.
In a classroom the same simplicity is the lesson. Most intro courses open with Caesar, which substitutes letters. Rail fence is the clean counterpoint: turn on the zigzag grid, type a word, and the class watches the letters cascade diagonally across the rails and read back row by row. Slide the rail count and the same text reorders into something new. Then decrypt and prove the move is fully reversible. It is a vivid way to make the substitution-versus-transposition distinction stick.
My take after using it
I keep the rail fence cipher in my back pocket for escape-room clues. The first time I built one, I encrypted a six-word hint at 4 rails, printed the scrambled line on a prop card, and scribbled the number "4" on a chalkboard across the room. Players who never connected the two stared at the card for ten minutes; the moment someone noticed the lonely "4" and tried it as a rail count, the message fell out clean. What sold me was that I could hand my co-organizer a shareable link that reopened my exact rails and offset, so we never had to re-key the puzzle by hand. It is a genuinely fun tool when the whole point is that the secret should eventually be found.
When not to use it
This is the part that matters most. The rail fence cipher is not encryption in any modern sense. The only secret is a tiny rail count, and as we just saw, an attacker cracks an unknown key by trying every value in a couple of seconds. There is no computational hardness, no key you can make long enough, nothing to fall back on.
So never use it to protect a password, a document, or a private message. Treat it the way you would treat a ROT13 encoder: a reversible obfuscation for puzzles and demos, not a lock. If you actually need confidentiality, you need a real cipher with a real key, run by software designed for it. The rail fence cipher earns its place as a teaching toy and a CTF warm-up, and within those lines it is excellent. Outside them, it offers no protection at all.
Made by Toolora · Updated 2026-06-13