Answered step by step
Verified Expert Solution
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 readkeyFilekeyFile:
with openkeyFiler as file:
n intfilereadlinestrip
keyMatrix
for in rangen:
row listmapint file.readlinestripsplit
keyMatrix.appendrow
return n keyMatrix
#Convert the plaintext file into lowercase
def readplaintextFileplaintextFile:
with openplaintextFiler as file:
plainText file.readlower
plainText joinfilterstrisalpha, plainText
return plainText
#Pad the plaintext file and also remove any numbers from it
def padplainTextplainText n:
remainder lenplainText n
if remainder :
padding n remainder
plainText x padding
return plainText
#Encrypt the plaintext file into a matrix
def encryptplainText keyMatrix:
n lenkeyMatrix
cipherText
for i in range lenplainText n:
block ordchar orda for char in plainTexti:in
encryptBlock sumkeyMatrixjk blockk for k in rangen orda for j in rangen
cipherText joinchrchar for char in encryptBlock
return cipherText
#Write the output of the plaintext file
def displayoutputkeyMatrix plainText, cipherText:
print
Key matrix:"
for row in keyMatrix:
printjoinstrnum for num in row
print
Plaintext:"
for i in range lenplainText:
printplainTexti:i
print
Ciphertext:"
for i in range lencipherText:
printcipherTexti:i
#Display the output of the plaintext file
def main:
if lensysargv:
printUsage: python papy
return
keyFile sysargv
plaintextFile sysargv
n keyMatrix readkeyFilekeyFile
plainText readplaintextFileplaintextFile
plainText padplainTextplainText n
cipherText encryptplainText keyMatrix
displayoutputkeyMatrix plainText, cipherText
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started