#!/usr/bin/env python3 from string import ascii_uppercase from string import digits #### Variables for Rail Fence Transposion #### # numbe of rows k = 3 cipher_text_list = [] cipher_text_list_element = '' count_char = 1 i = 0 decrypted_text = '' #### Variables for ROT #### rot=5 clear_text = "" with open("cipher_text.txt", "r") as f: # we need to remove last char cipher_text = f.read() #### Rail Fence Transposition #### # Compute real text length text_length = len(cipher_text) print(f"Length of text with spaces is: {text_length}") # Compute block length block_length = int(text_length / k) print(f"Length of text with spaces is: {text_length}, length of block is: {block_length}") # Create list with k-number fields for pismeno in cipher_text: if count_char % block_length != 0: cipher_text_list_element = cipher_text_list_element + pismeno elif count_char % block_length == 0: cipher_text_list_element = cipher_text_list_element + pismeno cipher_text_list.append(cipher_text_list_element) cipher_text_list_element = '' count_char+=1 # Display list print("\nThis is Rail Fence Transposition Cipher list") print(cipher_text_list, "\n") # Read first char from list element than move to another element and read first char until end of list # then read second char from element and move to anther element when you read the second char # do it k times while i < block_length: for element in cipher_text_list: decrypted_text+=element[i] i+=1 print(f"\nThis is decrypted text encrypted by Rail Fence Transposition Cipher that is input into ROT{rot} de-cipher: ") print(decrypted_text, "\n") #### ROT Decryption ### cipher_text = decrypted_text # print(f"Cipher text is: '{cipher_text}'") # Generate variable ABC..XYZ0123..9 template = "(,;'<.$![@" + ascii_uppercase + '}>+:&-%]*\\ ' + digits + '_"?=`/~#|){' print(f"Template for ROT{rot} is: {template}") # Get length of template len_template = len(template) print(f"Length of template is: '{len_template}'") for pismeno in cipher_text: # get index of pismeno and add length of rot cipher rot_position = template.find(pismeno) - rot print(f"Char is: {pismeno} and rot position is: {rot_position}") if rot_position < 0: clear_text += template[len_template+rot_position] elif rot_position >= 0: clear_text += template[rot_position] # Print clear_text print(clear_text)