Здравствуйте! Помогите пожалуйста! Нужно написать шифр тритемиуса на питоне с интерфейсом чтобы он зашифровывал набранный текст и расшифровывал набранный на шифре тритемиуса текст.
Ответы
Если захотите, чтобы он шифровал не только английский текст, нужно будет заменить алфавит с английского на русский:
Английский:
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Русский алфавит:
alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я']
Код на языке Python:
import tkinter as tk
def trithemius_cipher():
def encrypt_button_clicked():
encrypt_message = encrypt_text.get("1.0", "end-1c")
initial_shift_enc = int(shift_entry.get()) if shift_entry.get() else 0
encrypted_message = encrypt(encrypt_message.lower(), initial_shift_enc)
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, encrypted_message)
def decrypt_button_clicked():
decrypt_message = decrypt_text.get("1.0", "end-1c")
initial_shift_dec = int(shift_entry.get())
decrypted_message = decrypt(decrypt_message.lower(), initial_shift_dec)
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, decrypted_message)
root = tk.Tk()
root.title("Trithemius Cipher")
label1 = tk.Label(root, text="Введите текст для шифрования:")
label1.pack()
encrypt_text = tk.Text(root, height=5, width=30)
encrypt_text.pack()
label2 = tk.Label(root, text="Введите шаг (0 по умолчанию):")
label2.pack()
shift_entry = tk.Entry(root)
shift_entry.pack()
encrypt_button = tk.Button(root, text="Зашифровать", command=encrypt_button_clicked)
encrypt_button.pack()
label3 = tk.Label(root, text="Введите текст для расшифровки:")
label3.pack()
decrypt_text = tk.Text(root, height=5, width=30)
decrypt_text.pack()
decrypt_button = tk.Button(root, text="Расшифровать", command=decrypt_button_clicked)
decrypt_button.pack()
result_label = tk.Label(root, text="Результат:")
result_label.pack()
result_text = tk.Text(root, height=5, width=30)
result_text.pack()
root.mainloop()
def encrypt(secret_message, shift):
encrypted_message = ""
for char in secret_message:
if char != ' ':
encrypted_message += alphabet[(alphabet.index(char) + shift) % len(alphabet)]
shift += 1
else:
encrypted_message += char
return encrypted_message
def decrypt(secret_message, shift):
decrypted_message = ""
for char in secret_message:
if char != ' ':
decrypted_message += alphabet[(alphabet.index(char) - shift + len(alphabet)) % len(alphabet)]
shift += 1
else:
decrypted_message += char
return decrypted_message
alphabet = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]
trithemius_cipher()
Код на Python с текстовым меню:
def trithemius_cipher():
print("Вы хотите Зашифровать или Расшифровать??\n1. Зашифровать\n2. Расшифровать\n")
option = int(input())
if option == 1:
encrypt_message = input("\nВведите текст для зашифровки: ")
initial_shift_enc = int(input("\nВведите шаг (0 по умолчанию): "))
encrypted_message = encrypt(encrypt_message.lower(), initial_shift_enc)
print("\nЗашифрованный текст:", encrypted_message)
elif option == 2:
decrypt_message = input("\nВведдите текст для расшифровки: ")
initial_shift_dec = int(input("\nВведите шаг (0 по умолчанию): "))
decrypted_message = decrypt(decrypt_message.lower(), initial_shift_dec)
print("\nРасшифрованный текст:", decrypted_message)
else:
print("Введите правильное значение.")
def encrypt(secret_message, shift):
encrypted_message = ""
for char in secret_message:
if char != ' ':
encrypted_message += alphabet[(alphabet.index(char) + shift) % 26]
shift += 1
else:
encrypted_message += char
return encrypted_message
def decrypt(secret_message, shift):
decrypted_message = ""
for char in secret_message:
if char != ' ':
decrypted_message += alphabet[(alphabet.index(char) - shift + 26) % 26]
shift += 1
else:
decrypted_message += char
return decrypted_message
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
trithemius_cipher()

