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]")
import sys
# Global constants
NUM_ROWS = 6
NUM_COLS = 4
ROW_TRANSPOSITION = [3, 5, 0, 2]
COL_TRANSPOSITION = [3, 2, 0, 1]
def encrypt(text):
for in range(NUM_COLS)] for in range(NUM_ROWS)]
index = 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 = for 1 in ROW_TRANSPOSITION:
for j in COL_TRANSPOSITION:
table[i][j] = text[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', :[.p-
print("Usage: python script.py [-e | -d] <input_file>")
sys.exit(1)
mode = sys.argv[1]
input_file = sys.argv[2]
with open(input_file, 'r') as file:
text = file.read().strip()
if mode == '-e':
encrypt(text)
elif mode == '-d':
decrypt(text)
if __name == 'main':
main()