Rail Fence Cipher
Write letters diagonally across rails, then read each rail to rearrange the message.
What is the Rail Fence Cipher?
Rail Fence is a transposition cipher. The actual letters do not change; only their positions do.
To encrypt, the plaintext is written in a zigzag down and up across a chosen number of rails. The ciphertext is formed by reading the rails row by row.
The cipher mechanics on this page are self-contained. Science Olympiad event formats, allowed variants, and tournament constraints can change by season; the current official Rules Manual and official clarifications take precedence.
No outside reference is assumed. Work through Foundations → Complete Reference → Encryption → Decryption in order, then use the competition and cryptanalysis sections.
What you need to know
- A rail is one row of the zigzag.
- For r rails, the basic zigzag cycle length is 2(r−1).
- Spaces are commonly removed before arranging the letters.
Beginner glossary
| Term | Meaning |
|---|---|
| Rail | One horizontal row of the zigzag. |
| Zigzag | The down-then-up path used to place plaintext letters. |
| Cycle length | For r rails, the rail pattern repeats every 2(r−1) positions. |
| Transposition | A cipher that rearranges letters instead of replacing them. |
Keep this beside you while solving
Rail paths you can sketch quickly
| Rails | Repeating rail-number path | Cycle length |
|---|---|---|
| 2 | 1 2 1 2 … | 2 |
| 3 | 1 2 3 2 … | 4 |
| 4 | 1 2 3 4 3 2 … | 6 |
| 5 | 1 2 3 4 5 4 3 2 … | 8 |
How encryption works
Remove separators if required
Use a continuous letter stream.
Draw the zigzag
Move downward one rail per letter, then reverse direction at the bottom.
Fill every letter
Continue the up/down pattern to the end.
Read across rails
Top rail first, then the next rail, until all letters are collected.
How decryption works
Determine the rail count
This defines the zigzag path.
Mark the path positions
Draw placeholders in the zigzag for the ciphertext length.
Fill rail by rail
Place ciphertext letters into the marked positions across each rail.
Read the zigzag
Follow the path down and up to recover plaintext order.
How to approach it in Codebusters practice
- Do not treat Rail Fence like substitution—letter frequencies are unchanged.
- The rail count may be supplied or inferred by testing plausible rail counts.
- A clean grid prevents most errors.
What the problem gives you vs. what you produce
| Part | What to expect |
|---|---|
| You may be given | Ciphertext and the number of rails, or a ciphertext where a small range of rail counts can be tested. |
| You must find | The reordered plaintext. |
| Fastest first move | Draw the rail-number path before placing any letters. |
How to attack an unknown or partially known key
- Try small rail counts first; practical puzzles usually use a manageable number of rails.
- Look for likely word fragments after reconstructing each candidate rail count.
- Because all original letters remain, unusual letter-frequency distributions are not created by the cipher itself.
Follow one example from start to finish
WEAREDISCOVERED
WECRERDSOEEAIVD
Before moving on, make sure you can answer these without another site:
- Can you write the rail path for 4 rails without drawing letters?
- Can you explain why decryption must fill rail positions before following the zigzag?
Common mistakes
Reading diagonally instead of row-by-row during encryption.
Changing direction one position too early.
Forgetting that the top and bottom rails contain fewer positions.
Inserting spaces into the grid as if they were letters when the problem removed them.
Competition speed strategies
Draw rail numbers 0,1,2,… beside the grid.
For decryption, mark the zigzag shape first before placing any ciphertext letters.
Use cycle length 2(r−1) to predict top/bottom positions.
What to remember under time pressure
See every transformation
Use the lab to change inputs and keys, keep the relevant reference material visible, inspect each intermediate transformation, and then read the “How to reverse it” panel so encryption and decryption connect.