0% found this document useful (0 votes)
20 views

Lab 4

The document describes a lab assignment to implement Rail Fence and Columnar Transposition ciphers. It provides instructions and evaluation criteria for two tasks - to program the encryption and decryption algorithms for each cipher. Sample code is given for both ciphers as guidance.

Uploaded by

alshynbek.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab 4

The document describes a lab assignment to implement Rail Fence and Columnar Transposition ciphers. It provides instructions and evaluation criteria for two tasks - to program the encryption and decryption algorithms for each cipher. Sample code is given for both ciphers as guidance.

Uploaded by

alshynbek.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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)

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.

Lab-4: Implementation of Transposition Ciphers

Objective:
To familiarize students with the implementation and understanding of two common transposition
ciphers: Rail Fence and Columnar Transposition Cipher.

Required Software and Tools:


• Any Integrated Development Environment (IDE) suitable for the programming language of your
choice.
• The programming language can be Python, Java, C++, etc.

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:

1. Rail Fence Cipher


def rail_fence_encrypt(text, key):
rail = [['\n' for i in range(len(text))] for j in range(key)]
dir_down = False
row, col = 0, 0

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))

def rail_fence_decrypt(cipher, key):


rail = [['\n' for i in range(len(cipher))] for j in range(key)]
dir_down = None
row, col = 0, 0
for _ in cipher:
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)

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))

2. Columnar Transposition Cipher


def columnar_encrypt(text, keyword):
column_order = sorted([(char, i) for i, char in enumerate(keyword)], key=lambda x: x[0])
ciphertext = ''

for _, col_num in column_order:


current_col = text[col_num::len(keyword)]
ciphertext += current_col

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)

def columnar_decrypt(cipher, keyword):


column_order = sorted([(char, i) for i, char in enumerate(keyword)], key=lambda x: x[0])
k_len = len(keyword)
rows = len(cipher) // k_len
plain_text = [''] * rows

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.

You might also like