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 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
