Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- import random
- abc = {chr(i): (ord(chr(i)) - ord('а') + 1) for i in range(ord('а'), ord('я') + 1)}
- def calculate_n(p, q):
- return p * q
- def calculate_phi(p, q):
- return (p - 1) * (q - 1)
- def calculate_e(phi):
- g = 0
- e = 3
- while g != 1:
- e = random.randrange(1, phi)
- if is_prime(e):
- g = gcd(e, phi)
- return e
- def calculate_e_v2(phi_n):
- while True:
- e = random.randint(2, phi_n)
- if gcd(e, phi_n) == 1:
- return e
- def calculate_d(e, phi):
- d = 0
- while (e * d) % phi != 1:
- d += 1
- return d
- def is_prime(n):
- d = 2
- while n % d != 0:
- d += 1
- return d == n
- def gcd(a, b):
- while b != 0:
- a, b = b, a % b
- return a
- def get_key(d, value):
- for k, v in d.items():
- if v == value:
- return k
- def encrypt(e, n, plaintext):
- ciphertext = [(abc[char] ** e) % n for char in plaintext]
- text = [abc[char] for char in plaintext]
- print(text)
- return ciphertext
- def decrypt(private_key, ciphertext):
- p, q, phi, d = private_key
- n = p * q
- plaintext = []
- for i in ciphertext:
- print(i, (i ** d) % n, get_key(abc, ((i ** d) % n)))
- for num in ciphertext:
- plaintext.append(get_key(abc, ((num ** d) % n)))
- return ''.join(plaintext)
- def extended_gcd(a, b):
- if a == 0:
- return b, 0, 1
- else:
- g, x, y = extended_gcd(b % a, a)
- return g, y - (b // a) * x, x
- def solve_comparison_equation(a, n):
- b = 1
- g, x0, _ = extended_gcd(a, n)
- if b % g != 0:
- return -1
- else:
- x0 = (x0 * (b // g)) % n
- return x0
- def create_keys():
- p = int(entry_p.get("1.0", tk.END))
- q = int(entry_q.get("1.0", tk.END))
- n = calculate_n(p, q)
- phi = calculate_phi(p, q)
- e = calculate_e(phi)
- # d = solve_comparison_equation(e, phi)
- d = calculate_d(e, phi)
- global open_keys, closed_keys
- open_keys = [n, e]
- closed_keys = [p, q, phi, d]
- messagebox.showinfo("Результат дешифрования", f"Расшифрованный текст: {open_keys, closed_keys}")
- def encrypt_message():
- plaintext = entry_plaintext.get("1.0", tk.END)[0:-1]
- entry_plaintext.delete('1.0', tk.END)
- e = open_keys[1]
- n = open_keys[0]
- ciphertext = encrypt(e, n, plaintext)
- text_area.delete('1.0', tk.END)
- text_area.insert('1.0', ','.join(map(str, ciphertext)))
- messagebox.showinfo("Результат шифрования", f"Зашифрованный текст: {ciphertext}")
- def decrypt_message():
- ciphertext = [int(char) for char in text_area.get('1.0', tk.END).split(',')]
- plaintext = decrypt(closed_keys, ciphertext)
- messagebox.showinfo("Результат дешифрования", f"Расшифрованный текст: {plaintext}")
- root = tk.Tk()
- root.title("Алгоритм RSA")
- closed_keys = [] # p, q, phi, d
- open_keys = [] # e,n
- frame_keys = tk.Frame(root)
- frame_keys.pack(pady=10)
- button_create_keys = tk.Button(frame_keys, text="Создать ключи", command=create_keys) # todo
- button_create_keys.pack()
- frame_encrypted_text = tk.Frame(root)
- frame_encrypted_text.pack(pady=10)
- label_encrypted_text = tk.Label(frame_encrypted_text, text="зашифрованное сообщение:")
- label_encrypted_text.pack(side=tk.LEFT)
- text_area = tk.Text(frame_encrypted_text, height=2, width=15)
- text_area.pack(side=tk.LEFT)
- button_decrypt = tk.Button(frame_encrypted_text, text="Расшифровать", command=decrypt_message)
- button_decrypt.pack(side=tk.RIGHT)
- frame_encrypt = tk.Frame(root)
- frame_encrypt.pack(pady=10)
- label_p = tk.Label(frame_encrypt, text="p:")
- label_p.pack(side=tk.LEFT)
- entry_p = tk.Text(frame_encrypt, height=1, width=15)
- entry_p.pack(side=tk.LEFT)
- label_q = tk.Label(frame_encrypt, text="q:")
- label_q.pack(side=tk.LEFT)
- entry_q = tk.Text(frame_encrypt, height=1, width=15)
- entry_q.pack(side=tk.LEFT)
- label_plaintext = tk.Label(frame_encrypt, text="Текст:")
- label_plaintext.pack(side=tk.LEFT)
- entry_plaintext = tk.Text(frame_encrypt, height=1, width=15)
- entry_plaintext.pack(side=tk.LEFT)
- button_encrypt = tk.Button(frame_encrypt, text="Зашифровать", command=encrypt_message)
- button_encrypt.pack(side=tk.LEFT)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement