The Rail Fence Cipher

rail-fenceThe rail fence cipher (sometimes called zigzag cipher) is a transposition cipher that jumbles up the order of the letters of a message using a basic algorithm.

The rail fence cipher works by writing your message on alternate lines across the page, and then reading off each line in turn.

For example, let’s consider the plaintext “This is a secret message”.

To encode this message we will first write over two lines (the “rails of the fence”) as follows:
Note that all white spaces have been removed from the plain text.

The ciphertext is then read off by writing the top row first, followed by the bottom row:

Your Challenge

For this challenge, you will have to write two python programs, one to encrypt a message (plaintext to ciphertext), one to decrypt an encoded message (ciphertext to plaintext). To help you with this challenge we have created the flowcharts of both the encoder and the decoder algorithms.

Encoder AlgorithmDecoder Algorithm

Rail Fence Cipher – Encoder

Rail Fence Cipher - Encoder Algorithm

Rail Fence Cipher – Encoder Algorithm

Python Code

Rail Fence Cipher – Decoder

The Rail Fence Cipher - Decoder Algorithm

The Rail Fence Cipher – Decoder Algorithm

Python Code

Break the code!

Use your python script to decipher the following encoded message:


Extension Task

More complex Rail Fence Ciphers have more “rails”. For instance instead of writing the code over two lines (“rails”) you can write over three or four or more lines. The number of lines used in a Rail Fence Cipher is called the key.

Key = 3

A Rail Fence Cipher with 3 "rails" (Key = 3)

A Rail Fence Cipher with 3 “rails” (Key = 3)

Key = 4

A Rail Fence Cipher with 4 "rails" (Key = 4)

A Rail Fence Cipher with 4 “rails” (Key = 4)

Investigate how you could adapt both your encoding and decoding python programmes to enable to encrypt and decrypt messages with different keys.



The solution for this challenge is available to full members!
Find out how to become a member:
Members Area
Share Button
Tagged with: ,