Faculty of Computing
Information Security
Class: BSDS-1A
Lab 03: Understanding and implementation of Rail fence cipher. Brute forcing
the rail fence Cipher
Time: 10:00am – 1:00pm
Instructors: Dr. Hasan Tahir Butt
CS353: Information Page 1
Security
Lab 03: Understanding and implementation of Rail fence cipher. Brute forcing
the rail fence Cipher
Introduction
The railfence cipher is an easy-to-apply transposition cipher that jumbles up the order
of the letters of a message in a quick convenient way. It also has the security of a key
to make it a little bit harder to break.
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, the plaintext "defend the
east wall" is written as shown below, with all spaces removed.
Encryption
To encrypt a message using the Rail Fence Cipher, you have to write your message in
zigzag lines across the page, and then read off each row. Firstly, you need to have a
key, which for this cipher is the number of rows you are going to have. You then start
writing the letters of the plaintext diagonally down to the right until you reach the
number of rows specified by the key. You then bounce back up diagonally until you
hit the first row again. This continues until the end of the plaintext.
For the plaintext we used above, "defend the east wall", with a key of 3, we get the
encryption process shown below.
Note that at the end of the message we have inserted two "X"s. These are called nulls,
and act as placeholders. We do this to make the message fit neatly in to the grid (so
that there are the same number of letters on the top row, as on the bottom row.
Although not necessary, it makes the decryption process a lot easier if the message
has this layout.
The ciphertext is read off row by row to get "DNETLEEDHESWLXFTAAX".
Decryption
The decryption process for the Rail Fence Cipher involves reconstructing the diagonal
grid used to encrypt the message. We start writing the message, but leaving a dash in
place of the spaces yet to be occupied. Gradually, you can replace all the dashes with
the corresponding letters, and read off the plaintext from the table.
We start by making a grid with as many rows as the key is, and as many columns as
the length of the ciphertext. We then place the first letter in the top left square, and
dashes diagonally downwards where the letters will be. When we get back to the top
row, we place the next letter in the ciphertext. Continue like this across the row, and
start the next row when you reach the end.
CS353: Information Page 2
Security
For example, if you receive the ciphertext
"TEKOOHRACIRMNREATANFTETYTGHH", encrypted with a key of 4, you start
by placing the "T" in the first square. You then dash the diagonal down spaces until
you get back to the top row, and place the "E" here. Continuing to fill the top row you
get the pattern below.
Tools/Software Requirements
Java (or any other language which you learned in pre-requisite), Socket
Programming
Description
This lab involves researching and implementing the Rail fence cipher. Also, Brute
forcing the rail fence Cipher.
CS353: Information Page 3
Security
Task 1 (Part-A): Implement the Encryption method
Implement the encryption method for the Rail Fence Cipher.Write a function
rail_fence_encrypt(text, key) that:
o Takes a string text (the plaintext to encrypt) and an integer key
(the number of rails).
o Returns the encrypted string using the Rail Fence Cipher technique.
Task 1 (Part-B): Implement the Decryption method
Implement the decryption method for the Rail Fence Cipher. Write a function
rail_fence_decrypt(ciphertext, key) that:
o Takes a string ciphertext (the encrypted text) and an integer key (the
number of rails).
o Returns the original decrypted string (plaintext).
Task 2: Brute Force Rail Fence Cipher Decryption
Implement a brute force method to decrypt the Rail Fence Cipher without knowing
the key.
1. Goal: Write a function rail_fence_brute_force(ciphertext) that:
o Takes the ciphertext and attempts to decrypt it using different possible
keys (from 2 up to the length of the ciphertext).
o For each possible key, apply the decryption method and print or return
the potential plaintexts.
2. Steps:
o Loop through possible key values from 2 to the length of the ciphertext.
o Use the rail_fence_decrypt() function with each key.
o Print or store the decrypted strings for manual inspection to find the
correct key.
3. Test Cases: Test the brute force method with different encrypted messages
and manually identify the correct key. Test cases:
o Ciphertext: "HOLELWRDLO"
o Ciphertext: "TAKTANTKDAAW", where the original plaintext is
"ATTACKATDAWN"
o Ciphertext: "CTCROPHYARG", where the original plaintext is
"CRYPTOGRAPHY"
Deliverable:
Students are required to upload the complete report and code on LMS before the
deadline.
CS353: Information Page 4
Security