Lab 4
Lab 4
Lab-4: Points-2.5
Note: Individual lab completion and demonstration are required of all students. Students
will be asked questions about the code to ensure that they fully understand the lab. There
could be possibility of few bugs in the code that should be fixed.
Objective:
To familiarize students with the implementation and understanding of two common transposition
ciphers: Rail Fence and Columnar Transposition Cipher.
Instructions:
1. Make sure to comment on your code for clarity.
2. Properly indent and format your code to ensure readability.
3. Test your program with various inputs to ensure its accuracy.
Tasks:
Task 1. Rail Fence Cipher: Implement the Rail Fence Cipher encryption and decryption algorithm.
Encryption: Given a plaintext message and a key (number of rails), write down the message in a zigzag
manner from top to bottom and then read it from left to right to get the ciphertext.
Decryption: Given a ciphertext and a key, recreate the zigzag structure and fill it with the ciphertext.
Then read the original message in a top to bottom zigzag manner.
Task 2. Columnar Transposition Cipher: Implement the Columnar Transposition Cipher encryption and
decryption algorithm.
Encryption: Given a plaintext message and a keyword, arrange the message in rows such that the
number of columns matches the keyword length. Read the message column-wise based on the order of
the keyword to get the ciphertext.
Decryption: Given a ciphertext and a keyword, arrange the ciphertext in columns and then read it row-
wise to obtain the original plaintext message.
Submission:
1. Your source code files.
2. Screenshots showing the output of your code for both encryption and decryption tasks.
Department of Computer Engineering and Information Security, IITU Almaty Kazakhstan
Cryptographic Methods of Information Security- Lab-4
Dr. Syed Atif Moqurrab, PhD & Postdoc (Associate Professor)
Evaluation Criteria:
Your lab will be evaluated based on the following:
1. Correctness and efficiency of the implemented algorithms.
2. Proper structure and organization of your code.
3. The clarity of your code (use of comments, proper variable naming, etc.).
4. Successful decryption of given encrypted messages during the evaluation.
Important Note:
Plagiarism will not be tolerated. If found copying from classmates or online sources, strict action will be
taken.
Lab Duration:
This lab is expected to be completed within 2 hours.
Sample Code:
for i in range(len(text)):
if (row == 0) or (row == key - 1):
dir_down = not dir_down
rail[row][col] = text[i]
col += 1
if dir_down:
row += 1
else:
row -= 1
result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])
return("" . join(result))
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
rail[row][col] = '*'
col += 1
if dir_down:
row += 1
else:
row -= 1
idx = 0
for i in range(key):
for j in range(len(cipher)):
if rail[i][j] == '*' and idx < len(cipher):
rail[i][j] = cipher[idx]
idx += 1
result = []
row, col = 0, 0
for _ in range(len(cipher)):
if row == 0:
dir_down = True
if row == key-1:
dir_down = False
if rail[row][col] != '*':
result.append(rail[row][col])
col += 1
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
return ciphertext
Department of Computer Engineering and Information Security, IITU Almaty Kazakhstan
Cryptographic Methods of Information Security- Lab-4
Dr. Syed Atif Moqurrab, PhD & Postdoc (Associate Professor)
start = 0
for _, col_num in column_order:
current_col = cipher[start:start+rows]
start += rows
for r, char in enumerate(current_col):
plain_text[r] += char
return ''.join(plain_text)
This lab is designed to be comprehensive, ensuring students not only code but understand the
underlying principles of the two transposition ciphers.