Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you modify this code, a hill cipher, to look exactly how the output is supposed to look like? ( In Python ) Right now

Could you modify this code, a hill cipher, to look exactly how the output is supposed to look like? (In Python) Right now it is outputting the plaintext on the left, while it should be the output on the right. My spacing also might be wrong as well. Feel free to add numpy if you wish. Thanks!
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()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

Have issues been prioritized?

Answered: 1 week ago

Question

d. How will lack of trust be handled?

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago