Код:
from cryptograph.cryptobomb.ceaser_for_cryptobomb import cipher as cc # У меня есть прога, которая шифрует шифром Цезаря from cryptograph.cryptobomb.vijiner_cipher_for_cryptobomb import cipher as vc # и прога, шифрующая шифром Вижинера from random import randrange, choice class cryptobomb: def __init__(self): self.nameismain = True if __name__ == "__main__" else False def main(self, num_of_times=None, text=None): """ Проверка на то, была ли программа запущена пользователем или модулем. Если модуль не ввел нужные параметры, сработает ошибка AttributeError """ if self.nameismain: try: num_of_times = int(input("Number of times of crypting: ")) except ValueError as v: print("There is the error", v) print("Due to the error, number\n" "of times of crypting will\n" "set to 10. Nice day!\n") num_of_times = 10 text = str(input("Enter text:")) else: if not num_of_times or not text: raise AttributeError("module need give all parameters to the main function") elif not num_of_times and not text: raise AttributeError("module need give all parameters to the main function") else: pass ciphers = [cc, vc] encrypted_text = text key_log = [] for t in range(0, num_of_times): # Начинаем шифровать chosen_cipher = choice(ciphers) if chosen_cipher == vc: crypt_key = "" key_for_crypt = [chr(k) for k in range(ord("а"), ord("я") + 1)] for g in range(5,15): crypt_key += str(choice(key_for_crypt)) # Шифр Вижинера это шифр, в котором позиция буквы ключа и позиция буквы текста складываются и заменяются буквой в алфавите на позиции которая равна этой сумме key_log.append(crypt_key) encrypted_text = chosen_cipher(text=encrypted_text, key=crypt_key, task=True) else: key_for_crypt = randrange(-26, 26) # TODO: возможно вместо -26, 26 надо -27, 27 из-за пробелов key_log.append(key_for_crypt) encrypted_text = chosen_cipher(text=encrypted_text, key=key_for_crypt, task=True) with open("key_list.txt", "w") as key_list_clear: # очищаем список ключей key_list_clear.write("") key_list_clear.close() with open("key_list.txt", "a") as key_list_append: # добавляем новые ключи for key in key_log: key_list_append.write(str(key) + '\n') key_list_append.close() print("Encrypted text: ") print(str(encrypted_text).lower()) print("Number of encrypting times: " + str(num_of_times)) print("Used cipher:\n" "1. Ceaser's cipher\n" "2. Vijiner's cipher\n") print("Author - Damilkrose") if __name__ == "__main__": cryptobomb().main() else: pass
Получившийся лист ключей прикреплен(Шифровал слово ‘упси’)
Вывод:
Encrypted text: ожьл Number of encrypting times: 15 Used cipher: 1. Ceaser's cipher 2. Vijiner's cipher Author - Damilkrose