# программа частотного анализа # первоисточник: https://github.com/wb-08/frequency_analysis # https://habr.com/ru/post/513926/ # модифицирована для эксперимента эффективности кодирования смешанных текстов # использовались две таблицы частотности from collections import Counter def caesar_cipher(): file = open("input.txt") text_for_encrypt = file.read().lower().replace(',', '') print(text_for_encrypt) letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцшщъыьэюя0123456789!?., ' l0=len(letters) # длина алфавита print(l0) arr = [] step = 3 for i in text_for_encrypt: if i == ' ': arr.append(' ') else: arr.append(letters[(letters.find(i) + step) % l0]) text_for_decrypt = ''.join(arr) print(text_for_decrypt) return text_for_decrypt def number_of_letters(text_for_decrypt): counter = Counter(text_for_decrypt) arr_decrypt_letters = [] for i in range(len(counter)): arr_decrypt_letters.append(counter.most_common(len(counter))[i][0]) print(arr_decrypt_letters) return arr_decrypt_letters def decrypt_text(text_for_decrypt, arr_decrypt_letters): arr_encrypt_text = [] arr_encrypt_letters = [' ', 'о', 'а', 'е', 'и', 'т', 'н', 'л', 'р', 'с', 'в', 'к', 'м', 'д', 'у', 'п', 'б', 'г', 'ы', 'ч', 'ь', 'з', 'я', 'й', 'х', 'ж', 'ш', 'ю', 'ф', 'э', 'щ', 'ё', 'ц', 'ъ'] dictionary = dict(zip(arr_decrypt_letters, arr_encrypt_letters)) for i in text_for_decrypt: arr_encrypt_text.append(dictionary.get(i)) text_for_decrypt = ''.join(arr_encrypt_text) print(text_for_decrypt) def decrypt_text2(text_for_decrypt, arr_decrypt_letters): arr_encrypt_text = [] arr_encrypt_letters = [' ', 'о', 'е', 'а', 'и', 'н', 'т','с','л', 'р', 'в', 'к', 'м', 'д', 'у', 'п', 'б', 'г', 'ы', 'ч', 'ь', 'з', 'я', 'й', 'х', 'ж', 'ш', 'ю', 'ф', 'э', 'щ', 'ё', 'ц', 'ъ'] dictionary = dict(zip(arr_decrypt_letters, arr_encrypt_letters)) for i in text_for_decrypt: arr_encrypt_text.append(dictionary.get(i)) text_for_decrypt = ''.join(arr_encrypt_text) print(text_for_decrypt) def decrypt_text3(text_for_decrypt, arr_decrypt_letters): arr_encrypt_text = [] arr_encrypt_letters = [' ', 'e', 't', 'a', 'o', 'i', 'n','h','s', 'р', 'в', 'к', 'м', 'д', 'у', 'п', 'б', 'г', 'ы', 'ч', 'ь', 'з', 'я', 'й', 'х', 'ж', 'ш', 'ю', 'ф', 'э', 'щ', 'ё', 'ц', 'ъ'] #etaoinhsrdlmuwycfgpbvkxjqz def decrypt_text4(text_for_decrypt, arr_decrypt_letters): arr_encrypt_text = [] arr_encrypt_letters = [' ', ',', '.', '!', ':', 'i', 'n','h','s', 'р', 'в', 'к', 'м', 'д', 'у', 'п', 'б', 'г', 'ы', 'ч', 'ь', 'з', 'я', 'й', 'х', 'ж', 'ш', 'ю', 'ф', 'э', 'щ', 'ё', 'ц', 'ъ'] dictionary = dict(zip(arr_decrypt_letters, arr_encrypt_letters)) for i in text_for_decrypt: arr_encrypt_text.append(dictionary.get(i)) text_for_decrypt = ''.join(arr_encrypt_text) print(text_for_decrypt) if __name__ == '__main__': text_for_decrypt = caesar_cipher() print(text_for_decrypt) arr_decrypt_letters = number_of_letters(text_for_decrypt) print(arr_decrypt_letters) decrypt_text(text_for_decrypt, arr_decrypt_letters) decrypt_text2(text_for_decrypt, arr_decrypt_letters)