DOUBLE COLUMNAR DECRYPTION - PE
def columnar_decrypt(ciphertext, key):
#Create order based on the key's alphabetic order
n = len(key)
order = sorted(list(enumerate(key)), key=lambda x: x[1])
#Determine the number of rows and columns
num_rows = len(ciphertext) // n
grid = [''] * n
k = 0
#Fill the grid according to the sorted key order
for idx, _ in order:
grid[idx] = ciphertext[k:k + num_rows]
k += num_rows
#Read the grid column by column
plaintext = ''
for i in range(num_rows):
for j in range(n):
plaintext += grid[j][i]
return plaintext
#Take user input in Google Colab
ciphertext = input("Enter ciphertext: ").replace(" ", "").upper() # Remove spaces and convert to uppercase
key1 = input("Enter first key: ").upper() # Convert to uppercase
key2 = input("Enter second key: ").upper() # Convert to uppercase
#Decrypt the ciphertext using Double Columnar Transposition
step1 = columnar_decrypt(ciphertext, key2) # First key decryption
original = columnar_decrypt(step1, key1) # Second key decryption
#Display the decrypted text
print("Decrypted text:", original)