Форум сайта python.su
вот алгоритм
import random def crypto(message, alphabet, offset): result = '' for sym in message: if sym in alphabet: index = alphabet.find(sym) result += alphabet[(index + offset) % len(alphabet)] else: result += sym return result def decrypto(message, alphabet, offset): result = '' for sym in message: if sym in alphabet: index = alphabet.find(sym) result += alphabet[(index - offset) % len(alphabet)] else: result += sym return result ab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ' alphabet = ab + ab.lower() offset = int(random.uniform(0, 9)) message = input('введите текст: ') crypto = crypto(message, alphabet, offset) print(crypto) decrypto = decrypto(crypto, alphabet, offset) print(decrypto)
[code python]
import tkinter as tk
from typing import List
win = tk.Tk()
win.title('Алгоритм шифрования подстановкой')
win.geometry('800x800+200+200')
win.resizable(False, False)
label1 = tk.Label(win,text='Текст для шифрования')
label2 = tk.Label(win,text='Ключ')
text = tk.Entry(win)
key = tk.Entry(win)
rslt1 = tk.Entry(win)
rslt2 = tk.Entry(win)
def dlt():
text1.delete(0, tk.END)
key.delete(0, tk.END)
rslt1.delete(0, tk.END)
rslt2.delete(0, tk.END)
btn1 = tk.Button(win,text='Зашифровать')
btn3 = tk.Button(win,text='Создать рандомный ключ')
btn4 = tk.Button(win,text='Очистить', command=dlt)
btn5 = tk.Button(win,text='Расшифровать')
label1.grid(row=0,column=0,stick='wens')
label2.grid(row=0,column=1,stick='wens')
text.grid(row=1,column=0,stick='wens')
key.grid(row=1,column=1,stick='wens')
btn1.grid(row=2,column=0,columnspan=2,stick='wens')
btn3.grid(row=3,column=0,columnspan=2,stick='wens')
btn4.grid(row=4,column=0,columnspan=2,stick='wens')
btn5.grid(row=5,column=0,columnspan=2,stick='wens')
rslt1.grid(row=6,column=0,stick='wens')
rslt2.grid(row=6,column=1,stick='wens')
win.grid_columnconfigure(0,minsize=400)
win.grid_columnconfigure(1,minsize=400)
win.grid_rowconfigure(0,minsize=40)
win.grid_rowconfigure(1,minsize=40)
win.grid_rowconfigure(2,minsize=40)
win.grid_rowconfigure(3,minsize=40)
win.grid_rowconfigure(4,minsize=40)
win.grid_rowconfigure(5,minsize=40)
win.grid_rowconfigure(6,minsize=40)
win.mainloop()
[/code]
Отредактировано Dispa (Сен. 14, 2022 13:26:02)
Офлайн
добавьте тег КОД
Офлайн
как образец
import random import tkinter as tk def crypto(message, alphabet, offset): result = '' for sym in message: if sym in alphabet: index = alphabet.find(sym) result += alphabet[(index + offset) % len(alphabet)] else: result += sym return result def decrypto(message, alphabet, offset): result = '' for sym in message: if sym in alphabet: index = alphabet.find(sym) result += alphabet[(index - offset) % len(alphabet)] else: result += sym return result ab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ' alphabet = ab + ab.lower() offset = random.randrange(0, 10) def process(): text = entry_in.get() text_crypto = crypto(text, alphabet, offset) entry_out.delete(0, 'end') entry_out.insert(0, text_crypto) root = tk.Tk() root.geometry('300x200') lbl1 = tk.Label(text='текст который нужно зашифровать').pack() entry_in = tk.Entry(width=20) entry_in.pack() lbl2 = tk.Label(text='зашифрованный текст').pack() entry_out = tk.Entry(width=20) entry_out.pack() lbl3 = tk.Label(text=f'ключ шифрования: {offset}').pack() btn = tk.Button(text='зашифровать', command=process) btn.pack() root.mainloop()
Офлайн