Question: Could you modify this code, a hill cipher, to look exactly how the output is supposed to look like? ( In Python ) I need

Could you modify this code, a hill cipher, to look exactly how the output is supposed to look like? (In Python) I need to be able to encrypt the cipher that has special characters, letters, and numbers, since the commas and special characters are there but I need to match the code on the right. Please show your output as well in the answer. Thank you!
My code:
import sys
#Read the key file and then return the size of the key file matrix and its numbers
def read_keyFile(keyFile):
with open(keyFile,'r') as file:
n = int(file.readline().strip())
keyMatrix =[]
for _ in range(n):
row = list(map(int, file.readline().strip().split()))
keyMatrix.append(row)
return n, keyMatrix
#Convert the plaintext file into lowercase
def read_plaintextFile(plaintextFile):
with open(plaintextFile,'r') as file:
plainText = file.read().lower()
plainText =''.join(filter(str.isalpha, plainText))
return plainText
#Pad the plaintext file and also remove any numbers from it
def pad_plainText(plainText, n):
remainder = len(plainText)% n
if remainder !=0:
padding = n - remainder
plainText +='x'* padding
return plainText
#Encrypt the plaintext file into a matrix
def encrypt(plainText, keyMatrix):
n = len(keyMatrix)
cipherText =''
for i in range(0, len(plainText), n):
block =[ord(char)- ord('a') for char in plainText[i:i+n]]
encryptBlock =[(sum([keyMatrix[j][k]* block[k] for k in range(n)])%26)+ ord('a') for j in range(n)]
cipherText +=''.join(chr(char) for char in encryptBlock)
return cipherText
#Write the output of the plaintext file
def display_output(keyMatrix, plainText, cipherText):
print("
Key matrix:")
for row in keyMatrix:
print("",''.join(str(num) for num in row))
print("
Plaintext:")
for i in range(0, len(plainText),80):
print(plainText[i:i+80])
print("
Ciphertext:")
for i in range(0, len(cipherText),80):
print(cipherText[i:i+80])
#Display the output of the plaintext file
def main():
if len(sys.argv)!=3:
print("Usage: python3 pa01.py ")
return
keyFile = sys.argv[1]
plaintextFile = sys.argv[2]
n, keyMatrix = read_keyFile(keyFile)
plainText = read_plaintextFile(plaintextFile)
plainText = pad_plainText(plainText, n)
cipherText = encrypt(plainText, keyMatrix)
display_output(keyMatrix, plainText, cipherText)
if __name__=='__main__':
main()
 Could you modify this code, a hill cipher, to look exactly

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!