OlyPrep

§ TOOLS / CIPHER LAB / RAILFENCE

Railfence cipher

The rail fence cipher, written railfence in the lab, is a transposition cipher: it keeps every letter of the message and changes only the order, by writing the text in a zig-zag across a set number of rails and reading it off row by row. It is the simplest transposition most students meet and a good introduction to the whole family.

Free on every planNo sign-in to tryThe lab below runs this cipher only

§ THE CIPHER

What the rail fence cipher is

A substitution cipher swaps letters for other letters or symbols; a transposition cipher leaves the letters alone and moves them. The rail fence is the clearest example. Picture two, three or four horizontal rails. The message is written one letter at a time, starting on the top rail, stepping down a rail with each letter until the bottom, then stepping back up, and so on, tracing a zig-zag like the top of a fence. The ciphertext is the top rail read left to right, then the next rail, and so on down. The key is the number of rails.

Because no letter changes, a rail fence ciphertext has exactly the same letters as the plaintext, which is the first thing to notice about it: if a message’s letters have the ordinary frequencies of English, with plenty of E, T and A, but the message does not read, it is probably a transposition rather than a substitution. Spaces are normally removed before encoding and the ciphertext is printed in groups of five, so the solver also has to put the word breaks back by eye at the end.

Decoding is geometry rather than lookup. The zig-zag repeats every 2 × (rails − 1) letters, so with three rails every fourth letter is on the top rail, every fourth letter starting from the third is on the bottom, and the rest are in the middle. Counting the letters and dividing them up that way tells you how many letters each rail holds; the first group of ciphertext letters fills the top rail, the next group the next rail, and the plaintext is read by walking the zig-zag. The mistakes kids make are miscounting the rail lengths, usually by forgetting that the top and bottom rails hold fewer letters than the middle ones, and reading the rails in the wrong direction. Drawing the dots before writing any letters fixes both.

The cipher is weak by modern standards, since there are only a handful of plausible rail counts to try, and it is usually taught as a stepping stone to stronger transpositions such as the columnar cipher, where the order of the columns is the key. Some versions add an offset, starting the zig-zag partway down, which changes which letters land on which rail but not the method.

§ THE METHOD

How to solve a Railfence cipher

The letters of the message are written in a zig-zag down and up across a set number of rails (rows), then read off one rail at a time. No letter is replaced; every letter of the plaintext is in the ciphertext, just in a different order. The key is the number of rails.

  1. Count the letters in the ciphertext (ignore the spaces; they are only grouping). Call it N.
  2. Work out how many letters land on each rail. The zig-zag repeats every 2 × (rails − 1) letters: with 3 rails that is every 4 letters, and the pattern of rails is top, middle, bottom, middle, then top again.
  3. Draw the zig-zag as N dots on the rails, then number the dots left to right: the top rail takes the first group of ciphertext letters, the next rail the next group, and so on.
  4. Read the dots in zig-zag order, left to right, and the plaintext appears. Put the word breaks back by eye.
  5. If the number of rails is not given, try 2, then 3, then 4; the right count is the one that reads as English after a few letters.
§ WORKED EXAMPLE

A worked example

Decode WRIDA EILQI TSU (3 rails)

13 letters, 3 rails. The zig-zag repeats every 4 letters: top, middle, bottom, middle.
Positions 1, 5, 9, 13 sit on the top rail (4 letters), positions 3, 7, 11 on the bottom (3), and the other 6 on the middle.

Top rail:    W R I D
Middle rail: A E I L Q I
Bottom rail: T S U

Reading the zig-zag: W A T E R I S L I Q U I D
Plaintext: WATER IS LIQUID
§ ON THE TEST

Railfence in Codebusters

Among the Science Olympiad rule sets OlyPrep has read, the rail fence does not appear on the elementary or Division B Codebusters lists. The transposition those lists usually name is the complete columnar cipher, which Division B includes. The rail fence is in the open Cipher Lab because it is the simplest way to learn what a transposition is, and the habit it teaches, counting letters and drawing the layout before writing anything, is the same habit the columnar needs.

When a rail fence is set, the rail count is usually given and the task is the decode: lay out the rails, fill them from the ciphertext in order, and walk the zig-zag. Where the rail count is withheld, trying two, three and four rails in turn settles it within a few letters, because the wrong count produces nonsense at once.

The lab below uses two rails on the easy setting, three on medium and four on hard, strips the spaces and prints the ciphertext in groups of five, which is the form puzzle collections and tournament papers commonly use. The key line beside each challenge states the rail count.

§ PRACTICE

Practice the Railfence cipher

The lab below is set to the Railfence cipher alone. Pick a difficulty, generate a challenge, decode it, then check your answer or reveal the solution. It runs in your browser, needs no account, and is unlimited on every OlyPrep plan.

Scoped to Railfence · 1 cipher type. Generate a challenge, solve it, and we'll track what you've cracked.

Zig-zag transposition
§ PAST CHALLENGES

Your first decode is the hardest.

Pick a cipher above and give it a shot — we'll keep track of what you've solved.

§ WHERE IT IS TESTED

Where the Railfence cipher is tested

None of the Science Olympiad rule sets OlyPrep has read names the rail fence on its elementary or Division B Codebusters list; the transposition those lists name is the columnar cipher. It is in the open lab as the first transposition.

§ ABOUT THE RAILFENCE CIPHER

Questions about the Railfence cipher.

Q·01Is the rail fence a substitution or a transposition cipher?
A transposition. Every letter of the plaintext is present in the ciphertext, unchanged; only the order is different. That is why a rail fence message has the normal letter frequencies of English even though it does not read.
Q·02How do I know how many letters go on each rail?
The zig-zag repeats every 2 × (rails − 1) letters. With 3 rails that is every 4: the top rail holds the 1st, 5th, 9th letter and so on, the bottom rail the 3rd, 7th, 11th, and the middle rail the rest. Count the ciphertext, work out each rail’s share, then fill the rails in order from the ciphertext.
Q·03Is the rail fence cipher on Science Olympiad Codebusters tests?
Not on the elementary or Division B lists OlyPrep has read; the transposition those lists commonly name is the columnar cipher. OlyPrep is independent and is not affiliated with Science Olympiad, Inc. or any state or regional Science Olympiad organization. Each tournament’s rulebook decides its own list.
Q·04How does the OlyPrep lab check a rail fence answer?
The lab encodes a science sentence across the number of rails it shows you, with the spaces removed and the letters grouped in fives, and compares your decoded sentence against the original, ignoring spacing. Check says whether they match; Reveal shows the solution.