Advertisement
Teammasik

lr_3_krypta

May 28th, 2024 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. import random
  4.  
  5. abc = {chr(i): (ord(chr(i)) - ord('а') + 1) for i in range(ord('а'), ord('я') + 1)}
  6.  
  7.  
  8. def calculate_n(p, q):
  9.     return p * q
  10.  
  11.  
  12. def calculate_phi(p, q):
  13.     return (p - 1) * (q - 1)
  14.  
  15.  
  16. def calculate_e(phi):
  17.     g = 0
  18.     e = 3
  19.     while g != 1:
  20.         e = random.randrange(1, phi)
  21.         if is_prime(e):
  22.             g = gcd(e, phi)
  23.     return e
  24.  
  25.  
  26. def calculate_e_v2(phi_n):
  27.     while True:
  28.         e = random.randint(2, phi_n)
  29.         if gcd(e, phi_n) == 1:
  30.             return e
  31.  
  32.  
  33. def calculate_d(e, phi):
  34.     d = 0
  35.     while (e * d) % phi != 1:
  36.         d += 1
  37.     return d
  38.  
  39.  
  40. def is_prime(n):
  41.     d = 2
  42.     while n % d != 0:
  43.         d += 1
  44.     return d == n
  45.  
  46.  
  47. def gcd(a, b):
  48.     while b != 0:
  49.         a, b = b, a % b
  50.     return a
  51.  
  52.  
  53. def get_key(d, value):
  54.     for k, v in d.items():
  55.         if v == value:
  56.             return k
  57.  
  58.  
  59. def encrypt(e, n, plaintext):
  60.     ciphertext = [(abc[char] ** e) % n for char in plaintext]
  61.     text = [abc[char] for char in plaintext]
  62.     print(text)
  63.     return ciphertext
  64.  
  65.  
  66. def decrypt(private_key, ciphertext):
  67.     p, q, phi, d = private_key
  68.     n = p * q
  69.     plaintext = []
  70.  
  71.     for i in ciphertext:
  72.         print(i, (i ** d) % n, get_key(abc, ((i ** d) % n)))
  73.  
  74.     for num in ciphertext:
  75.         plaintext.append(get_key(abc, ((num ** d) % n)))
  76.     return ''.join(plaintext)
  77.  
  78.  
  79. def extended_gcd(a, b):
  80.     if a == 0:
  81.         return b, 0, 1
  82.     else:
  83.         g, x, y = extended_gcd(b % a, a)
  84.         return g, y - (b // a) * x, x
  85.  
  86.  
  87. def solve_comparison_equation(a, n):
  88.     b = 1
  89.     g, x0, _ = extended_gcd(a, n)
  90.  
  91.     if b % g != 0:
  92.         return -1
  93.     else:
  94.         x0 = (x0 * (b // g)) % n
  95.         return x0
  96.  
  97.  
  98. def create_keys():
  99.     p = int(entry_p.get("1.0", tk.END))
  100.     q = int(entry_q.get("1.0", tk.END))
  101.  
  102.     n = calculate_n(p, q)
  103.     phi = calculate_phi(p, q)
  104.     e = calculate_e(phi)
  105.     # d = solve_comparison_equation(e, phi)
  106.     d = calculate_d(e, phi)
  107.  
  108.     global open_keys, closed_keys
  109.     open_keys = [n, e]
  110.     closed_keys = [p, q, phi, d]
  111.     messagebox.showinfo("Результат дешифрования", f"Расшифрованный текст: {open_keys, closed_keys}")
  112.  
  113.  
  114. def encrypt_message():
  115.     plaintext = entry_plaintext.get("1.0", tk.END)[0:-1]
  116.     entry_plaintext.delete('1.0', tk.END)
  117.     e = open_keys[1]
  118.     n = open_keys[0]
  119.  
  120.     ciphertext = encrypt(e, n, plaintext)
  121.     text_area.delete('1.0', tk.END)
  122.     text_area.insert('1.0', ','.join(map(str, ciphertext)))
  123.     messagebox.showinfo("Результат шифрования", f"Зашифрованный текст: {ciphertext}")
  124.  
  125.  
  126. def decrypt_message():
  127.     ciphertext = [int(char) for char in text_area.get('1.0', tk.END).split(',')]
  128.     plaintext = decrypt(closed_keys, ciphertext)
  129.     messagebox.showinfo("Результат дешифрования", f"Расшифрованный текст: {plaintext}")
  130.  
  131.  
  132. root = tk.Tk()
  133. root.title("Алгоритм RSA")
  134.  
  135. closed_keys = []  # p, q, phi, d
  136. open_keys = []  # e,n
  137.  
  138.  
  139. frame_keys = tk.Frame(root)
  140. frame_keys.pack(pady=10)
  141.  
  142. button_create_keys = tk.Button(frame_keys, text="Создать ключи", command=create_keys)  # todo
  143. button_create_keys.pack()
  144.  
  145. frame_encrypted_text = tk.Frame(root)
  146. frame_encrypted_text.pack(pady=10)
  147.  
  148. label_encrypted_text = tk.Label(frame_encrypted_text, text="зашифрованное сообщение:")
  149. label_encrypted_text.pack(side=tk.LEFT)
  150.  
  151. text_area = tk.Text(frame_encrypted_text, height=2, width=15)
  152. text_area.pack(side=tk.LEFT)
  153.  
  154. button_decrypt = tk.Button(frame_encrypted_text, text="Расшифровать", command=decrypt_message)
  155. button_decrypt.pack(side=tk.RIGHT)
  156.  
  157. frame_encrypt = tk.Frame(root)
  158. frame_encrypt.pack(pady=10)
  159.  
  160. label_p = tk.Label(frame_encrypt, text="p:")
  161. label_p.pack(side=tk.LEFT)
  162.  
  163. entry_p = tk.Text(frame_encrypt, height=1, width=15)
  164. entry_p.pack(side=tk.LEFT)
  165.  
  166. label_q = tk.Label(frame_encrypt, text="q:")
  167. label_q.pack(side=tk.LEFT)
  168.  
  169. entry_q = tk.Text(frame_encrypt, height=1, width=15)
  170. entry_q.pack(side=tk.LEFT)
  171.  
  172. label_plaintext = tk.Label(frame_encrypt, text="Текст:")
  173. label_plaintext.pack(side=tk.LEFT)
  174.  
  175. entry_plaintext = tk.Text(frame_encrypt, height=1, width=15)
  176. entry_plaintext.pack(side=tk.LEFT)
  177.  
  178. button_encrypt = tk.Button(frame_encrypt, text="Зашифровать", command=encrypt_message)
  179. button_encrypt.pack(side=tk.LEFT)
  180.  
  181. root.mainloop()
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement