0% found this document useful (0 votes)
22 views4 pages

Cryptprac 1

The document describes functions for encrypting and decrypting text using the Rail Fence Cipher and Columnar Transposition Cipher techniques. It provides a menu for selecting either the Rail Fence or Columnar Transposition cipher, then encrypting or decrypting text using the selected cipher's functions with the appropriate inputs.

Uploaded by

Dhruv Sangani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Cryptprac 1

The document describes functions for encrypting and decrypting text using the Rail Fence Cipher and Columnar Transposition Cipher techniques. It provides a menu for selecting either the Rail Fence or Columnar Transposition cipher, then encrypting or decrypting text using the selected cipher's functions with the appropriate inputs.

Uploaded by

Dhruv Sangani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# 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).")

You might also like