Rail Fence Cipher

Steph Leung on Flickr

A simple zigzag pattern can scramble your messages beyond recognition.

In the last two issues, we’ve been exploring ciphers that use simple substitution. The first swapped letters of the alphabet around, which the second used ‘pigpens’ to designate each letter then used them to deliver an encrypted code. This issue, we’re going to be looking at a code that doesn’t simply replace letters with other symbols!

This is the case for the Rail Fence Cipher, a very interesting way of delivering messages. We’ll still be sending normal-looking letters as our message, but it’ll be so scrambled up that anyone who doesn’t know the code won’t know what it says. Even better, unlike simple substitution, it’s not just regular letters interchanged with other ones, meaning it’s a little trickier to crack!

So, let’s explore how you make a more complicated code with the Rail Fence Cipher. To start with, think of a message you want to send. For this example, we want to send the message “HELLO WORLD”.

Then, decide on the amount of ‘rails’ you want to use. For this example, and a good starting point in general, we’re going to use three rails. However many rails you choose, draw them out like this:

Now, simply write your code out on the rails, so that it ‘zig zags’ up and down on these lines. This is why this code is also known as the ‘zig zag cipher’! The first five letters of our message, “HELLO”, will go on the rails like this:

You can see that the word goes down, then up. You can choose to go up first, but whatever route you pick, make sure you remember it, as it’s vital for your recipient to decode your message! Once you’ve gotten started it’s a case of continuing the zig zag until our message is entirely on the rails:

So now we have our message on the rails; but how do we make a cipher out of this? Even if we sent this code as-is, it’s easy to decipher by just reading the message as it goes up and down. That’s why we don’t send it as-is; instead, we’ll write down the letters as they appear on each rail. We’ll also make sure to put a space when we change rails, so our recipient knows to do the same when they come to decipher this message.

So, let’s get started on “HELLO WORLD”. If we look at the top line of our finished rail cipher, we can see it reads “HOL”. The second line reads“ELWRD”, and the third rail reads “LO”:

Therefore, when we go to encrypt “HELLO WORLD” via a rail cipher, it will become “HOL ELWRD LO”!

Then, all we need to do is send “HOL ELWRD LO” it to a friend who knows about our rail cipher. Let them know how many rails the code uses, and whether you started on the top or bottom rail when writing your message.

When your friend receives the message, all they have to do is put the letters back onto the rails. Ideally, they’ll leave plenty of space in between each letter to make room for the other rails. In our example, they’ll see “HOL” as the first word, which represents the letters on the top rail. Therefore, they’ll write “HOL” on the top rail:

Then, “ELWRD” is for the letters on the second rail, so they’ll write that on the second rail’s spots:

Finally, the third rail has “LO” written onto it:

And we’re done! Your friend can now read your code via the zigzag method.

Now it’s your turn. Let’s say you and a friend have agreed to use a 3-rail system when transmitting codes. You want to send them the message “WHERE SHALL WE MEET?”. After you placed the message onto the rails, how does the encrypted message read?

Similarly, after you’ve given your friend the code, they send a message back that’s to be decrypted on three rails. The message reads: “BEEFS YHCNMATRI TIAEX”. What is your friend trying to tell you? Draw out the three rails and put the message onto them starting from the top rail, remembering to leave enough space on each rail for the others!

If you’re interested in reading more about this kind of cipher, there’s plenty out there for you to read! This specific kind of cipher is called a ‘transposition cipher’. Much like last issues had ‘substitution ciphers’ because we substituted letters with other letters, this is called a ‘transposition cipher’ because we’re taking a message and scrambling the letters around in a logical manner so our target can ‘unravel’ the message on their side.

Answers:

“WHERE SHALL WE MEET?” becomes “WELET HRSALWME ELHE” and the rails look like this:

The secret code your friend sends translates to “BY THE CINEMA AFTER SIX” and reads like this:

Learn More

Rail Fence Ciphers

https://en.wikipedia.org/wiki/Rail_fence_cipher

Transposition Ciphers

https://learncryptography.com/classical-encryption/transposition-ciphers

Rail Fence Cipher Auto-Decoder

http://www.dcode.fr/rail-fence-cipher

Also In The December 2017 Issue

Learn some nifty coding tricks while creating a Formula EV3 Race Car.

With binary search, you don’t have to be a mind reader to beat this simple coding activity.

Controllers have come a long way in 50 years. Let’s appreciate the bulky, awkward ancestors that led us here!

Take your Scratch games to the next level by adding multiple modes with multiple sprites.

Let the CSS Zen Gardens wow you with variety and elegance. Making a pretty website isn’t so complicated after all!

An introduction to a hip new alternative to Java. It’s all the rage in android development!

Resources to transform you into a web virtuoso, from messaging apps to testers to code converters.

New treasures and new enemies await in two unique, dangerous realms.

Create your own 3D digital museum and learn how you can help preserve cultural artifacts.

Interesting stories about computer science, software programming, and technology for December 2017.

Add data types to your new programming language in this third installment of the series.

Links from the bottom of all the December 2017 articles, collected in one place for you to print, share, or bookmark.