Lab-3 CS-353 (Railfence Cipher)
Lab-3 CS-353 (Railfence Cipher)
Information Security
Class: BSDS-1A
Lab 03: Understanding and implementation of Rail fence cipher. Brute forcing
the rail fence Cipher
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.
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.
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.
Implement the encryption method for the Rail Fence Cipher.Write a function
rail_fence_encrypt(text, key) that:
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).