Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have python code I need help with. When decrypting, it is just returning whatever I am inputting. It encrypts and decrypts by file input.

I have python code I need help with. When decrypting, it is just returning whatever I am inputting. It encrypts and decrypts by file input. Usage is "python script.py -e input.txt " for encryption and -d for decryption. It is a double transposition cipher, need help fixing it to where it outputs the proper decrypted message. Encryption works just fine.
# Global constants
NUM_ROWS =6
NUM_COLS =4
ROW_TRANSPOSITION =[3,5,0,2,1,4]
COL_TRANSPOSITION =[3,2,0,1]
def encrypt(text):
table =[['' for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
index =0
for i in range(NUM_ROWS):
for j in range(NUM_COLS):
if index len(text):
table [i][j]= text[index]
index +=1
else:
table[i][j]='' # Padding if text length is less than table size
# Transpose rows
table =[table [i] for i in ROW_TRANSPOSITION]
# Transpose columns
table =[list (row [i] for i in COL_TRANSPOSITION) for row in table]
# Output encrypted text
for row in table:
print("".join(row), end='')
def decrypt(text):
table =[['' for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
index =0
for i in ROW_TRANSPOSITION:
for j in COL_TRANSPOSITION:
table index]
index +=1
# Reverse column transposition
table =[list(row[i] for i in sorted(range(NUM_COLS), key=lambda x: COL_TRANSPOSITION.index(x))) for row in table]
# Reverse row transposition
table table [i] for i in ROW_TRANSPOSITION]
# Output decrypted text
for row in table:
print("'.join(row), end='')
def main():
if len(sys.argv)!=3 or sys.argv[1] not in ['-e','-d']:
print("Usage: python
script.py [-e |-d]=r
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

More Books

Students also viewed these Databases questions