0% found this document useful (0 votes)
59 views7 pages

Mah D Assignment

The student encrypted a message from an input file using a Caesar cipher with a key of "3456", wrote the encrypted cipher to an output file, then decrypted the cipher and wrote the original plaintext to another file. The student provided the Python code used to encrypt and decrypt the message by creating a matrix based on the key and message and reading it column-wise for encryption and row-wise for decryption.

Uploaded by

Skylink Seller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views7 pages

Mah D Assignment

The student encrypted a message from an input file using a Caesar cipher with a key of "3456", wrote the encrypted cipher to an output file, then decrypted the cipher and wrote the original plaintext to another file. The student provided the Python code used to encrypt and decrypt the message by creating a matrix based on the key and message and reading it column-wise for encryption and row-wise for decryption.

Uploaded by

Skylink Seller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

IS (INFORMATION SECURITY)

ASSIGNMENT # 02

NAME : Mahd-ul-haq

Reg. No : 3872 FBAS BSSE F 18 C

To : Sir Anwar Ghani


 I have used the Key (3456) in the key

 Reading the Data from (input.txt) and cipher


is written in another file (output.txt) .

 I have decrypt the cipher in (output.txt) and


save it in the another file (plain,txt).
Basically , its the original text.

Python Code

import math

key='3456'

with open("input.txt","r") as file:


data=file.readlines()
msg=""
for line in data:
msg+=line

# Encryption
def encryptMessage(msg):
cipher = ""

# track key indices


k_indx = 0

msg_len = float(len(msg))
msg_lst = list(msg)
key_lst = sorted(list(key))
# calculate column of the matrix
col = len(key)

# calculate maximum row of the matrix


row = int(math.ceil(msg_len / col))

# add the padding character '_' in empty


# the empty cell of the matix
fill_null = int((row * col) - msg_len)
msg_lst.extend('_' * fill_null)

# create Matrix and insert message and


# padding characters row-wise
matrix = [msg_lst[i: i + col]
for i in range(0, len(msg_lst), col)]

# read matrix column-wise using key


for _ in range(col):
curr_idx = key.index(key_lst[k_indx])
cipher += ''.join([row[curr_idx]
for row in matrix])
k_indx += 1

return cipher

# Decryption
def decryptMessage(cipher):
msg = ""

# track key indices


k_indx = 0

# track msg indices


msg_indx = 0
msg_len = float(len(cipher))
msg_lst = list(cipher)

# calculate column of the matrix


col = len(key)

# calculate maximum row of the matrix


row = int(math.ceil(msg_len / col))
# convert key into list and sort
# alphabetically so we can access
# each character by its alphabetical position.
key_lst = sorted(list(key))

# create an empty matrix to


# store deciphered message
dec_cipher = []
for _ in range(row):
dec_cipher += [[None] * col]

# Arrange the matrix column wise according


# to permutation order by adding into new matrix
for _ in range(col):
curr_idx = key.index(key_lst[k_indx])

for j in range(row):
dec_cipher[j][curr_idx] = msg_lst[msg_indx]
msg_indx += 1
k_indx += 1

# convert decrypted msg matrix into a string


try:
msg = ''.join(sum(dec_cipher, []))
except TypeError:
raise TypeError("This program cannot",
"handle repeating words.")

null_count = msg.count('_')

if null_count > 0:
return msg[: -null_count]

return msg

cipher=encryptMessage(msg)
print("Encrypted Message : ",cipher)

with open("output.txt",'w') as file:


file.write(cipher)

plaintext=decryptMessage(cipher)
with open("plain.txt","w") as file:
file.write(plaintext)

print("Decrypted Message : ",plaintext)

Implementation In VS Code

You might also like