CrosswordPrint Free crossword maker and word puzzle tools

Rail Fence Cipher Decoder and Encoder

Type your text and pick the number of rails. The zigzag and the rail fence result appear as you type; paste cipher text and press the search button if you do not know the rails.

The zigzag

Decode without knowing the rails

Paste the cipher text into the box at the top and press the button. The tool decodes it with every rail count from 2 to 12 and every start offset, then lists the readings that look most like English.

Rail Fence Cipher Worksheet

Name: ____________________      Date: ______________

Setting:

Cipher text:

The rail fence cipher is a free online tool for encoding and decoding messages with the zigzag method, and it runs entirely in your browser. You do not need to sign up, and nothing you type is sent to a server. Choose Encode or Decode, set the number of rails and an optional start offset, and the result updates as you type. Use it to check a homework answer, build a puzzle, or see how a zigzag cipher rearranges letters. The same controls handle rail fence cipher encryption and decryption, and a separate option can decode a message when you don’t know the rail count. This page is a rail fence decoder and encoder in one place.

Rail fence cipher example

How to use the Rail Fence Cipher

  1. Choose Encode to scramble a message, or Decode to unscramble one.
  2. Type or paste your message into the input box. The output updates as you type.
  3. Set Rails to a number from 2 to 12.
  4. Set Start offset. The value 0 starts on the top rail and goes down. The largest allowed value is 2×(rails−1)−1.
  5. Leave Keep spaces and punctuation in the zigzag unticked to send only letters and digits along the rails. Tick it to keep spaces and punctuation in their places on the zigzag.
  6. If spaces are stripped, you can tick Groups of five to split the output into blocks of five.
  7. Read the output. The page also shows each rail’s letters on its own line and draws the zigzag grid for the first 120 characters.

Click Try an Example to load a sample message, or Clear to start again. Copy Result copies the output, and Use Result as Input feeds it back into the input box so you can run another step. Print Worksheet prints the zigzag grid, filled in or blank (untick Fill in the letters for a blank grid). The worksheet covers up to 30 characters and includes the cipher text. Your settings are saved in the address bar, so you can bookmark or share a link that reopens them. Worksheets are free to use, including commercially.

What is the rail fence cipher and how does it work

The rail fence cipher is a transposition cipher that writes a message in a zigzag across a set number of rails, then reads the rails one at a time. Each letter keeps its identity, but its position changes. To encode, write the first letter on the top rail, the next on the rail below, and so on until you reach the bottom rail. Then turn back up, moving diagonally, until you reach the top again. Finally, read each rail from left to right, starting with the top rail.

For example, the message WE ARE DISCOVERED. FLEE AT ONCE with 3 rails and offset 0, with spaces stripped, gives these rails:

  • Rail 1: WECRLTE
  • Rail 2: ERDSOEEFEAOC
  • Rail 3: AIVDEN

Joining them gives WECRLTEERDSOEEFEAOCAIVDEN. With only 2 rails, ABCDEF becomes ACEBDF, because the letters alternate between the two rails.

How to decode a rail fence cipher without knowing the rails

If you don’t know the rail count, use Try Every Rail Count. It tries rails 2 to 12 with every offset, which is 132 combinations. It ranks each result by how English-like it looks and lists the top 8, each with a Use these settings button. In tests, it ranked the correct setting first 10 out of 10 times. Some offsets with the correct rail count also score well, because they only rotate the text. If the top result is almost right but starts in the wrong place, try the nearby offsets listed.

To decode by hand, you need the rail counts. Each full cycle covers 2×(rails−1) positions. The top and bottom rails get one letter per cycle, and the middle rails get two. Once you know how many letters sit on each rail, you can fill the rails in order, then read the zigzag back to recover the message. With 3 rails and 25 letters, for example, the top rail holds 7 letters, the middle rail 12 and the bottom rail 6, just as in the example above.

Frequently asked questions

What is the offset?

The offset sets where the zigzag starts in its repeating pattern. An offset of 0 starts on the top rail and goes down. Other values start partway through, such as on a middle rail going up, which changes the cipher text. The person decoding must use the same offset as the person encoding.

Is the rail fence cipher secure?

No. It is a transposition cipher, so the letters stay the same and only their order changes. With only a few dozen possible keys, anyone can break it by trying them all. Use it for puzzles, classroom lessons and learning, not for keeping secrets.

Why are my spaces and punctuation missing from the output?

The Keep spaces and punctuation in the zigzag box is off by default. In that mode, only letters and digits travel along the rails. Tick the box if you want spaces and punctuation kept on the zigzag.

Does the zigzag diagram show my whole message?

No. The diagram shows only the first 120 characters, and it scrolls inside its own box. Each rail’s letters are still listed on their own line for the whole message.

Can I print the rail fence cipher as a worksheet?

Yes. Print Worksheet prints the zigzag grid and the cipher text. The worksheet is limited to 30 characters. Tick Fill in the letters for a grid with letters, or untick it for a blank grid that students can complete.

Can I use the printed worksheets for commercial purposes, such as Amazon KDP or Etsy?

Yes. Worksheets and coded messages you make here are free to use for any purpose, including classroom packs, escape-room kits, KDP puzzle books and Etsy printables, royalty-free and with no attribution needed. The only limit is that you may not copy the tool itself to run a competing service.

  • Playfair Cipher: Use it when you want a digraph substitution cipher that works on pairs of letters.
  • Caesar Cipher: Use it for a simple letter-shift cipher, which is a good first step before trying a transposition cipher.
  • Cipher Identifier: Use it when you have a message and want to guess which cipher made it.
  • Polybius Square: Use it to encode letters as pairs of row and column numbers.
  • Atbash Cipher: Use it for a reversed-alphabet substitution, where A becomes Z and B becomes Y.
  • Morse Code Translator: Use it to convert text to and from dots and dashes.
  • Cryptogram Solver: Use it to work out a substitution cipher where each letter is replaced by another letter.

About the rail fence cipher

The rail fence cipher (also called a zigzag cipher) is a classical type of transposition cipher. It derives its name from the manner in which encryption is performed, in analogy to a fence built with horizontal rails.

Encryption

In the rail fence cipher, the plaintext is written downwards diagonally on successive "rails" of an imaginary fence, then moving up when the bottom rail is reached, down again when the top rail is reached, and so on until the whole plaintext is written out. The ciphertext is then read off in rows.

For example, to encrypt the message 'WE ARE DISCOVERED. RUN AT ONCE.' with 3 "rails", write the text as:

W... E... C... R... U... O.... E. R. D. S. O. E. E. R. N. T. N. E.. A... I... V... D... A... C.

(Spaces and punctuation are omitted.) Then read off the text horizontally to get the ciphertext:

Decryption

Let be the number of rails used during encryption. Observe that as the plaintext is written, the sequence of each letter's vertical position on the rails varies up and down in a repeating cycle. In the above example (where ) the vertical position repeats with a period of 4. In general the sequence repeats with a period of.

Let be the length of the string to be decrypted. Suppose for a moment that is a multiple of and let. One begins by splitting the ciphertext into strings such that the length of the first and last string is and the length of each intermediate string is. For the above example with, we have, so we split the ciphertext as follows:

Write each string on a separate line with spaces after each letter in the first and last line:

Then one can read off the plaintext down the first column, diagonally up, down the next column, and so on.

If is not a multiple of, the determination of how to split up the ciphertext is slightly more complicated than as described above, but the basic approach is the same. Alternatively, for simplicity in decrypting, one can pad the plaintext with extra letters to make its length a multiple of.

Text adapted from the Wikipedia article “Rail fence cipher” (revision 1356745953, 2026-05-29), authors, licensed under CC BY-SA 4.0. Shortened: some sections and paragraphs were left out, and reference markers, images and templates were removed; wording is otherwise unchanged. This text remains available under the same licence.

Updated