# Rail Fence Cipher encryption function
def rail_fence_encrypt(text, rails):
rails = min(rails, len(text))
fence = [[' ' for _ in range(len(text))] for _ in range(rails)]
rail, delta = 0, 1
for char in text:
fence[rail][fence[rail].index(' ')] = char
if rail == 0:
delta = 1
elif rail == rails - 1:
delta = -1
rail += delta
encrypted_text = ''.join(''.join(row) for row in fence)
return encrypted_text
# Rail Fence Cipher decryption function
def rail_fence_decrypt(text, rails):
rails = min(rails, len(text))
fence = [[' ' for _ in range(len(text))] for _ in range(rails)]
rail, delta = 0, 1
for i in range(len(text)):
fence[rail][i] = '*'
if rail == 0:
delta = 1
elif rail == rails - 1:
delta = -1
rail += delta
plaintext = [' ' for _ in range(len(text))]
i=0
for row in range(rails):
for col in range(len(text)):
if fence[row][col] == '*':
plaintext[col] = text[i]
i += 1
return ''.join(plaintext)
# Columnar Transposition Cipher encryption function
def columnar_transposition_encrypt(text, key):
key_order = sorted(range(len(key)), key=lambda k: key[k])
num_columns = len(key)
num_rows = -(-len(text) // num_columns)
matrix = [[' ' for _ in range(num_columns)] for _ in range(num_rows)]
for i, char in enumerate(text):
matrix[i % num_rows][key_order[i // num_rows]] = char
encrypted_text = ''.join(''.join(row) for row in matrix)
return encrypted_text
# Columnar Transposition Cipher decryption function
def columnar_transposition_decrypt(text, key):
key_order = sorted(range(len(key)))
num_columns = len(key)
num_rows = -(-len(text) // num_columns) # Calculate the number of rows
# Initialize the matrix
matrix = [[' ' for _ in range(num_columns)] for _ in range(num_rows)]
# Fill the matrix with characters from the encrypted text
i=0
for col in key_order:
for row in range(num_rows):
if i < len(text):
matrix[row][col] = text[i]
i += 1
# Extract the text from the matrix row by row
decrypted_text = ''.join(''.join(row) for row in matrix)
return decrypted_text
while True:
print("\nTransposition Techniques Menu:")
print("1. Rail Fence Cipher")
print("2. Columnar Transposition Cipher")
print("3. Quit")
choice = input("Enter your choice (1/2/3): ")
if choice == '1':
text = input("Enter the text: ")
rails = int(input("Enter the number of rails: "))
encrypt_or_decrypt = input("Encrypt (E) or Decrypt (D)? ").strip().lower()
if encrypt_or_decrypt == 'e':
encrypted_text = rail_fence_encrypt(text, rails)
print("Encrypted Text:", encrypted_text)
elif encrypt_or_decrypt == 'd':
decrypted_text = rail_fence_decrypt(text, rails)
print("Decrypted Text:", decrypted_text)
else:
print("Invalid choice. Enter 'E' for encrypt or 'D' for decrypt.")
elif choice == '2':
text = input("Enter the text: ")
key = input("Enter the columnar transposition key (e.g., 2314): ")
encrypt_or_decrypt = input("Encrypt (E) or Decrypt (D)? ").strip().lower()
if encrypt_or_decrypt == 'e':
encrypted_text = columnar_transposition_encrypt(text, key)
print("Encrypted Text:", encrypted_text)
elif encrypt_or_decrypt == 'd':
decrypted_text = columnar_transposition_decrypt(text, key)
print("Decrypted Text:", decrypted_text)
else:
print("Invalid choice. Enter 'E' for encrypt or 'D' for decrypt.")
elif choice == '3':
print("Goodbye!")
break
else:
print("Invalid choice. Please select a valid option (1/2/3).")