Rail Fence Cipher (Fun Secret Code to Teach Your Kids)


My boys have had so much fun learning different secret codes and ciphers. They zipped through some of the classics pretty quick (Caesar, Ottendorf, Scytale, Pig Pen) and I think it’s time to step things up a little. This is a great time to introduce them to the rail fence cipher – also known as the zig-zag cipher.

I love using this cipher with my no-longer-beginner boys. It takes a little more explanation and it’s trickier to decode (if you don’t know the secret!), but it’s still easy to learn. Let’s take a look at what it is, and how you can use it for some fun activities with your kids.

What is a Rail Fence Cipher (Zig-Zag Cipher)?

The Rail Fence Cipher (also called a Zig-Zag Cipher) is a transposition cipher in which the letters of the original message (plaintext) are written over 2 or more lines in a zig-zag pattern. The letters are then rewritten from left to right in a now rearranged, coded (ciphertext) pattern.

3 rail cipher with dadstuffsite.com
DadStuffSite.com written using a 3-rail cipher becomes DTSC ASUFIEO DFTM

In a transposition cipher, the letters of the original plaintext message are all present in the ciphertext message. The letters are all rearranged in a pre-determined pattern, making it difficult to understand the original message.

It’s called a rail fence cipher because when coding and encoding, the letters are staggered over two or more lines. The pattern looks similar to a fence with horizontal rails.

Graphic of a 3-rail fence with "DadStuffSite.com" encoded over the three rails.

A rail fence cipher is also called a zig-zag cipher because you take the original message written over 2 or more “rails” and follow a zig-zag pattern to create the encoded message.

If you want to try a similar code that’s simpler for younger kids, check out The Scytale Cipher (What It Is and How to Teach Your Kids).

How to Encode and Decode Messages Using a Rail Fence Cipher

There are a couple of rail fence cipher variations with different levels of complexity.

2-Line Rail Fence Cipher

Let’s start by using a simple 2-line rail fence cipher. To encode your message, start by writing out the original message in a single line. For this example I’m going to send my wife the following message:

"Take my advice. I'm not using it"

Rewrite the message over two lines in a zig-zag pattern (we’re going to ignore all punctuation). Take the first letter “T” and put it first in the top line. Put the second letter “A” and put it first on the bottom line. Then put the third letter “K” on the top line and the next letter “E” on the bottom. Continue with this pattern until all the letters are used.

Encoding using a Rail fence or zig-zag cipher

To encode, take the new order of letters from the top row and then the bottom row. The new encoded ciphertext will now look like this:

TKMAVCINTSNI    AEYDIEMOUIGT

You can either group it into a single line of text with no spaces or keep it divided by two lines to let the recipient know it’s a 2-rail cipher.

To decode, take the first letter from the left side, then the first letter from the right side. Then the second letter from the left side, and the second letter from the right side. Continue with this pattern until you go through all the letters.

Dencoding using a Rail fence or zig-zag cipher

If you want to add a layer of mystery to your secret codes, try writing them using invisible ink: Color Changing Invisible Ink (Using Leftover Hand Sanitizer).

3-Line Rail Fence Cipher

To make this message more difficult to decode, we can add more “rails” to the cipher. Let’s use a 3-rail cipher to encode the following message:

"Blunt pencils are pointless"

Start by writing the message over three lines. I added a dummy letter “X” to fill in the pattern. This is not a necessary step. If you do decide to use decoy letters, don’t use obvious letters like “X.”

3 rail fence cipher with the message: Blunt Pencils are Pointless

To encode, take the letters from the top row first, then the middle row, then the last. The new encoded ciphertext will now look like this:

BTCAOLX     LNPNISRPITES     UELENS

It’s simplest to keep the words grouped according to each row. It’s not necessary but it lets whoever you’re passing the message to know that it’s a 3-rail cipher.

To decode, take the first letter of the first grouping, then the first letter of the second grouping, then the first letter of the third grouping. Then go to the second letter of the second grouping, and the second letter of the first grouping. Keep moving right/left using each next letter until you’ve gone through all the letters once.

Decoding "BTCAOLX LNPNISRPITES UELENS" using a 3-rail fence cipher to decode "blunt pencils are pointless"

Looking for more secret codes to teach your kids? Check out The Ottendorf Cipher (What it Is and How to Teach Your Kids).

Encoding and Decoding Using a Grid

Some people like to use a grid to help with encoding and decoding. Begin with a sheet of graph paper. You can also download some free blank grids here:

Encoding on a grid is the same as without. It puts the letters in a nice group of boxes that helps some to see your work better. To decode using a grid, begin by counting how many letters you have. Let’s take our last example which has 25 letters.

BTCAOLX     LNPNISRPITES     UELENS

Lightly put a mark on 25 boxes that follow the pattern for a 3-rail cipher (or however many rails your coded message uses).

Grid with 25 dots following a 3 rail cipher pattern

Next, start filling in the marked boxes with the encoded message. Start with the top left box and fill from left to right.

Grid "BTCAOLX" on the top line replacing placeholder dots. The second and third lines still have placeholder dots.

Continue filling in the letters on the second and third rows of the grid.

"BLUNT PENCILS ARE POINTLESS" spelled out on a 3-rail cipher

Either read your message following a zig-zap pattern or rewrite it in a single line.

If you want to add some adventure to your secret codes, try weathering the paper. Your kids will think they’ve discovered something hundreds of years old: How to Weather Paper to Make it Look Old (The Ultimate Guide).

Rail Fence Cipher Activity Ideas (Plus a Free Worksheet)

A rail fence cipher definitely takes a little more explanation than an A1Z26 Cipher, but once you understand how the letters fit on a grid, it’s pretty straightforward. If you’re looking to teach it to a younger child, consider starting with a Scytale Cipher. It’s essentially a rail fence cipher, but you use a dowel or other cylinder to do the coding and decoding for you.

Learn more: 6 Secret Codes and Ciphers to Teach Young Kids

If you’re a little intimidated by the idea of teaching it, print out the following worksheet and go through it with your child. It’ll give you some step-by-step instructions to do your first rail fence ciphers.

Related: 15 Epic Treasure Hunt Prizes for Kids

If you’re looking to incorporate it into play, hand your child a nerf gun with the message “ACCHAO TAKNLCRSTON TUEIN” (which is a 3-rail cipher that translates to “Attack Uncle Chris at Noon”). Enjoy a nerf ambush on your sibling!

Use a rail fence cipher to send messages to your kids to help plan a secret Mother’s Day surprise. Or maybe you’ve been kidnapped by a double agent and they need to find out where you are (Can you say epic hide and seek?).

If you’re looking to add some more adventure to your weekend, lead your kids on their very own Goonies-style treasure hunt.

You might also like these Treasure Hunt activities:

Gregory Grabowski

Greg Grabowski is the principal creator of DadStuffSite.com, a website for dads by dads. Inspired by his two boys Ben and Sam and his wife Dianna, Greg loves to make things, learn things, and loves doing fun stuff with his family.

Related Posts