Converters & translators
Rail Fence Cipher Decoder & Encoder
Encode and decode the rail fence (zigzag) cipher with 2 to 10 rails, see the zigzag drawn out, or crack a message when you do not know the key.
Updated · Free, no sign-up
Share this tool
Know someone who’d love it?
What is the rail fence cipher?
The rail fence cipher, also called the zigzag cipher, hides a message by changing the order of its letters, not the letters themselves. You write the message in a zigzag across a number of rows, the “rails” of the fence, and then read the rows one after the other. With 3 rails, MEETATTHEPARKATNOON becomes MAEKOETTHPRANOETATN.
That makes it a transposition cipher. Ciphers like the Caesar cipher swap each letter for another one (substitution); the rail fence keeps every letter and only moves it. You can see the difference at a glance: a rail fence cipher text has exactly the same letters as the original, just scrambled.
How to use the rail fence cipher encoder and decoder
- Choose Encode or Decode.
- Set the number of rails (the key). Both sides have to use the same number.
- Type or paste your text. The result updates as you type, and the zigzag view in the tool shows where every letter lands on the fence.
- Tap Copy to save the result.
Do not know the number of rails? The tool can crack it for you: it tries every number of rails from 2 to 10, so you only have to spot the result that reads as real words. The section on cracking below explains why that works so well.
Rail fence cipher chart
How to encode a rail fence cipher by hand
- Remove the spaces and write the message in capitals (most puzzles do this, so word lengths do not give the answer away).
- Draw as many rows as you have rails. Write the first letter on the top rail, the next one rail lower, and keep going down diagonally.
- When you reach the bottom rail, turn around and go back up; at the top, turn down again.
- Read the top rail from left to right, then the second rail, and so on down to the bottom.
Here is MEETATTHEPARKATNOON on 3 rails:
| 1 | M | A | E | K | O | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | E | T | T | H | P | R | A | N | O | ||||||||||
| 3 | E | T | A | T | N |
Reading the rails in order gives MAEKO, ETTHPRANO and ETATN, which joined together is the cipher text MAEKOETTHPRANOETATN.
The tool treats spaces and punctuation as characters on the fence, just like letters, so WE MEET AT DAWN on 3 rails becomes WEAAEME TDW T N. If you want the classic look, remove the spaces before you encode.
Rail fence cipher with 2, 3, 4 and 5 rails
The pattern repeats every 2 × (rails − 1) letters: down to the bottom and back up to just below the top. That is the cycle. On 3 rails the cycle is 4, which is why you may see a key written as “height 3, cycle 4”. The same message with different numbers of rails:
| Rails | Cycle | Letters per rail | Cipher text |
|---|---|---|---|
| 2 | 2 | 10 + 9 | MEATEAKTONETTHPRANO |
| 3 | 4 | 5 + 9 + 5 | MAEKOETTHPRANOETATN |
| 4 | 6 | 4 + 6 + 6 + 3 | MTKNETHRAOEAEATOTPN |
| 5 | 8 | 3 + 5 + 5 + 4 + 2 | MEOEHPNOETATNTTRAAK |
With 2 rails the cipher is simply every other letter (the 1st, 3rd, 5th…) followed by the rest, which makes it a quick one to do in your head. With 4 rails the same message looks like this:
| 1 | M | T | K | N | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | E | T | H | R | A | O | |||||||||||||
| 3 | E | A | E | A | T | O | |||||||||||||
| 4 | T | P | N |
How to decode a rail fence cipher by hand
Decoding means rebuilding the fence. The trick is to work out how many letters sit on each rail before you place any of them. Take MAEKOETTHPRANOETATN with 3 rails:
- Count the letters: there are 19.
- Mark the zigzag. Draw 3 rows with 19 columns and put a dot where each letter would go, going down and up as when encoding. Count the dots on each rail: 5, 9, 5.
- Cut the cipher text into those lengths: rail 1 gets
MAEKO; rail 2 getsETTHPRANO; rail 3 getsETATN. - Fill each rail with its piece, left to right, on the dots.
- Read the zigzag down and up again, one letter per column: MEETATTHEPARKATNOON.
Counting the dots is the step people skip, and it is the one that makes everything else fall into place. Graph paper helps a lot.
How to crack a rail fence cipher without the key
The rail fence has very few possible keys. A message of 19 letters can only use 2 to 18 rails (with as many rails as letters, nothing moves), and short messages are almost always written with 2 to 6. So you can simply try them all. Here is MAEKOETTHPRANOETATN decoded with every number of rails from 2 to 8:
| Rails | Decoded text |
|---|---|
| 2 | MRAAENKOOEETTATTHNP |
| 3 | MEETATTHEPARKATNOON |
| 4 | MORAAEATNTOTEHENTPK |
| 5 | MKHOTEPOAERTNAATETN |
| 6 | MEEPOTERTKAOTATNANH |
| 7 | MEEHAETTNPTKAOTROAN |
| 8 | MEEHAEANTTNPTKAOTRO |
Only one row reads as English, so the key was 3. The tool’s crack option does exactly this. A second clue is in the letters themselves: since nothing is substituted, the letter-frequency panel of a rail fence cipher text looks just like normal English (lots of E, T, A and O). If the frequencies look normal but the words do not, you are probably looking at a transposition cipher such as this one.
Variations of the rail fence cipher
- Offset: the zigzag can start partway down the fence instead of on the top rail, which adds a second number to the key. To decode one by hand, draw the zigzag as if it started on the top rail and leave the first few spots empty.
- Reading order: some puzzles read the rails from the bottom up, or read the zigzag in reverse. If no number of rails gives words, reverse the cipher text and try again.
- Combined ciphers: run the result through a substitution cipher such as Atbash or the Vigenère cipher. Mixing transposition and substitution is much stronger than either alone, and it is the idea behind many serious historical ciphers.
Is the rail fence cipher secure?
No. With so few keys, anyone can try them all in a minute, by hand or with this tool. It is still well worth learning, because it shows the second big family of classical ciphers. Transposition is an old idea: the scytale, a strip of material wound around a rod that ancient Greek writers describe, also hid a message by changing the order of its letters. Who first wrote messages in a zigzag is not recorded.
Rail fence cipher in Python
The algorithm is short enough to write in a few lines, which is why it is a classic programming exercise. Encoding appends each character to its rail; decoding works out the zigzag pattern first, cuts the cipher text into one piece per rail, then walks the zigzag again:
def rail_fence_encode(text, rails):
if rails < 2:
return text
rows = [""] * rails
row, step = 0, 1
for ch in text:
rows[row] += ch
if row == 0:
step = 1
elif row == rails - 1:
step = -1
row += step
return "".join(rows)
def rail_fence_decode(cipher, rails):
if rails < 2:
return cipher
# 1. Which rail does each position fall on?
pattern, row, step = [], 0, 1
for _ in cipher:
pattern.append(row)
if row == 0:
step = 1
elif row == rails - 1:
step = -1
row += step
# 2. Cut the cipher text into one piece per rail.
pieces, start = [], 0
for r in range(rails):
count = pattern.count(r)
pieces.append(list(cipher[start:start + count]))
start += count
# 3. Walk the zigzag again, taking the next letter from each rail.
return "".join(pieces[r].pop(0) for r in pattern)
print(rail_fence_encode("MEETATTHEPARKATNOON", 3)) # MAEKOETTHPRANOETATNRail fence cipher practice puzzles
Three messages to decode, with the spaces removed as in most puzzles. Draw the fence on paper first, then check your answer with the decoder.
Puzzle 1 (Easy)
2 rails.
SCECDERTOE
Show the answer
SECRET CODE
Puzzle 2 (Medium)
3 rails.
LURMOKNETEAODHT
Show the answer
LOOK UNDER THE MAT
Puzzle 3 (Hard)
The number of rails is up to you to find. Try them one by one, or use the crack option.
TIEOHYSHBBXEEITLEKNU
Show the answer
THE KEY IS IN THE BLUE BOX (4 rails)
Rail fence puzzles for classrooms and escape rooms
- Classrooms: have students draw the fence on graph paper, encode a short sentence, and swap with a partner who has to find the number of rails. It teaches patterns, counting and why short keys are weak.
- Escape rooms: paint a fence with three rails on the wall or print the cipher text on a picket sign. The number of rails is the hint, and the decoded text gives the next clue or lock code.
- Layered puzzles: encode a message with the Polybius square, then scramble the digits with a rail fence. Solvers have to take down the fence before the number pairs make sense.
Free and private
This rail fence cipher tool runs entirely in your browser. It is free, needs no sign-up, and nothing you type is uploaded or stored.
For teachers, bloggers & clubs
Put the Rail Fence Cipher on your website
Give your readers this tool right on your page. Copy the code, paste it into your site, done: no sign-up, no account.
- Free for any website
- No ads, no cookies
- Fits phones and sidebars
- Always up to date
Please keep the small credit line under the tool; you may add rel="nofollow" to its link. How it works & the rules
› Show the code
Frequently asked questions
- What is the rail fence cipher?
- The rail fence or zigzag cipher writes a message diagonally down and up across a number of rows, called rails, then reads the rows one after another. It changes the order of the letters but not the letters themselves.
- How do you encode a rail fence cipher?
- Write the first letter on the top rail and each next letter one rail lower, turning back up at the bottom rail and down again at the top. Then read the top rail left to right, followed by each rail below it.
- How do you decode a rail fence cipher?
- Rebuild the fence: mark the zigzag positions for the message length, count how many letters fall on each rail, cut the cipher text into those lengths, fill each rail, and read the zigzag again. The tool does this when you choose Decode and set the rails.
- How do you decrypt a rail fence cipher without the key?
- Try every number of rails. A message can only use 2 up to one less than its length, and short messages almost always use 2 to 6, so only one attempt will read as words. The tool's crack option tries 2 to 10 rails for you.
- How many rails should I use?
- Three is the classic choice. More rails scramble a short message more, but the sender and receiver must use the same number, because the number of rails is the key.
- What is the cycle of a rail fence cipher?
- The zigzag repeats every 2 x (rails - 1) letters. On 3 rails the cycle is 4, which is why a key is sometimes written as height 3, cycle 4.
- Is the rail fence a substitution or a transposition cipher?
- It is a transposition cipher: every letter stays the same and only moves. A rail fence cipher text therefore has normal English letter frequencies, which helps you recognize it.
- Does the rail fence cipher keep spaces?
- Most puzzles remove spaces before encoding so word lengths do not give the message away. If you keep them, they are treated as characters and land on the fence like letters.
- Is the rail fence cipher secure?
- No. It has very few possible keys, so anyone can try them all in a minute. It is a great way to learn how transposition ciphers work.
About this page
The Rail Fence Cipher is a free tool from Linguaholic, made by Marcel Iseli. It runs in your browser, with no sign-up. Last updated .
The code breaker's English letter frequencies and word checks come from Linguaholic’s English dictionary, built from the open ENABLE and SCOWL word lists. Licenses and notices.
Keep exploring
More free tools
Caesar CipherShift letters to encode or decode, or crack a Caesar message without knowing the key.
Vigenère CipherEncode or decode with a keyword, or crack a Vigenère message without the key.
Substitution CipherEncode with any alphabet or keyword, or crack a cryptogram and fix letters yourself.
Atbash CipherMirror the alphabet (A↔Z, B↔Y) to encode or decode, with the full Atbash chart.
Polybius SquareTurn letters into row-column number pairs on the 5×5 grid and back, with keyword squares.
Morse Code TranslatorTranslate text to Morse code and back, hear it as sound at any speed, and see the full Morse alphabet chart.
