Advertisement
Teammasik

lr5_krypta

Jun 5th, 2024
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.21 KB | None | 0 0
  1. # эль гамай
  2. import tkinter as tk
  3. from tkinter import messagebox
  4. import random
  5.  
  6. abc = {chr(i): (ord(chr(i)) - ord('а') + 1) for i in range(ord('а'), ord('я') + 1)}
  7.  
  8.  
  9. def hash_func(message):
  10.     return sum(ord(c) for c in message)
  11.  
  12.  
  13. def generate_numbers():
  14.     p = int(entry_p.get("1.0", tk.END))
  15.     g = random.randint(2, p)
  16.     x = random.randint(2, p)
  17.  
  18.     y = (g ** x) % p
  19.  
  20.     global open_keys, closed_keys
  21.     open_keys = [x]
  22.     closed_keys = [y, g, p]
  23.  
  24.  
  25. def calculate_k(p):
  26.     g = 0
  27.     k = 3
  28.     while g != 1:
  29.         k = random.randrange(1, p-2)
  30.         if is_prime(k):
  31.             g = gcd(k, p)
  32.     return k
  33.  
  34.  
  35. def create_signature():
  36.     generate_numbers()
  37.     message = entry_plaintext.get("1.0", tk.END)[0:-1]
  38.     p = closed_keys[2]
  39.     g = closed_keys[1]
  40.     x = open_keys[0]
  41.  
  42.     message = hash_func(message)
  43.     while True:
  44.         k = random.randint(2, p - 2)
  45.         if gcd(k, p - 1) == 1:
  46.             break
  47.  
  48.     a = (g ** k) % p
  49.     b = ((message - x * a) * k) % (p - 1)
  50.     a = int(a)
  51.     b = int(b)
  52.  
  53.     text_area.delete('1.0', tk.END)
  54.     text_area.insert('1.0', ','.join(map(str, [a, b])))
  55.  
  56.  
  57. def check_signature():
  58.     a, b = [float(char) for char in text_area.get('1.0', tk.END).split(',')]
  59.     a, b = int(a), int(b)
  60.  
  61.     message = entry_plaintext.get('1.0', tk.END)[0:-1]
  62.     y = closed_keys[0]
  63.     p = closed_keys[2]
  64.     g = closed_keys[1]
  65.  
  66.     m = hash_func(message)
  67.  
  68.     print(y, a, b, g, m)
  69.     left = ((y ** a) * (a ** b)) % p
  70.     right = (g ** m) % p
  71.     print(left, right)
  72.     if left == right:
  73.         messagebox.showinfo("Результат", "текст: Signature is correct!")
  74.     else:
  75.         messagebox.showinfo("Результат", "текст: Signature is incorrect")
  76.  
  77.  
  78. def is_prime(n):
  79.     d = 2
  80.     while n % d != 0:
  81.         d += 1
  82.     return d == n
  83.  
  84.  
  85. def gcd(a, b):
  86.     while b != 0:
  87.         a, b = b, a % b
  88.     return a
  89.  
  90.  
  91. root = tk.Tk()
  92. root.title("Алгоритм Эль-Гамая")
  93.  
  94. closed_keys = []
  95. open_keys = []
  96.  
  97. frame_keys = tk.Frame(root)
  98. frame_keys.pack(pady=10)
  99.  
  100.  
  101. frame_encrypted_text = tk.Frame(root)
  102. frame_encrypted_text.pack(pady=10)
  103.  
  104. label_encrypted_text = tk.Label(frame_encrypted_text, text="зашифрованное сообщение:")
  105. label_encrypted_text.pack(side=tk.LEFT)
  106.  
  107. text_area = tk.Text(frame_encrypted_text, height=2, width=15)
  108. text_area.pack(side=tk.LEFT)
  109.  
  110. button_decrypt = tk.Button(frame_encrypted_text, text="Проверить", command=check_signature)
  111. button_decrypt.pack(side=tk.RIGHT)
  112.  
  113. frame_encrypt = tk.Frame(root)
  114. frame_encrypt.pack(pady=10)
  115.  
  116. label_p = tk.Label(frame_encrypt, text="p:")
  117. label_p.pack(side=tk.LEFT)
  118.  
  119. entry_p = tk.Text(frame_encrypt, height=1, width=15)
  120. entry_p.pack(side=tk.LEFT)
  121.  
  122. label_plaintext = tk.Label(frame_encrypt, text="Текст:")
  123. label_plaintext.pack(side=tk.LEFT)
  124.  
  125. entry_plaintext = tk.Text(frame_encrypt, height=1, width=15)
  126. entry_plaintext.pack(side=tk.LEFT)
  127.  
  128. button_encrypt = tk.Button(frame_encrypt, text="Зашифровать", command=create_signature)
  129. button_encrypt.pack(side=tk.LEFT)
  130.  
  131. root.mainloop()
  132.  
  133.  
  134.  
  135.  
  136. #rsa--------------------------------------------------------------------------------------------
  137. import tkinter as tk
  138. from tkinter import messagebox
  139. import random
  140.  
  141. abc = {chr(i): (ord(chr(i)) - ord('а') + 1) for i in range(ord('а'), ord('я') + 1)}
  142.  
  143.  
  144. def calculate_n(p, q):
  145.     return p * q
  146.  
  147.  
  148. def calculate_phi(p, q):
  149.     return (p - 1) * (q - 1)
  150.  
  151.  
  152. def calculate_e(phi):
  153.     g = 0
  154.     e = 3
  155.     while g != 1:
  156.         e = random.randrange(1, phi)
  157.         if is_prime(e):
  158.             g = gcd(e, phi)
  159.     return e
  160.  
  161.  
  162. def calculate_d(e, phi):
  163.     d = 0
  164.     while (e * d) % phi != 1:
  165.         d += 1
  166.     return d
  167.  
  168.  
  169. def is_prime(n):
  170.     d = 2
  171.     while n % d != 0:
  172.         d += 1
  173.     return d == n
  174.  
  175.  
  176. def gcd(a, b):
  177.     while b != 0:
  178.         a, b = b, a % b
  179.     return a
  180.  
  181.  
  182. def get_key(d, value):
  183.     for k, v in d.items():
  184.         if v == value:
  185.             return k
  186.  
  187.  
  188. def encrypt(e, n, plaintext):
  189.     ciphertext = [(abc[char] ** e) % n for char in plaintext]
  190.     text = [abc[char] for char in plaintext]
  191.     print(text)
  192.     return ciphertext
  193.  
  194.  
  195. def decrypt(private_key, ciphertext, open_keys, defText):
  196.     p, q, phi, d = private_key
  197.     n, e = open_keys
  198.     plaintext = []
  199.  
  200.     for num in ciphertext:
  201.         plaintext.append(get_key(abc, ((num ** e) % n)))
  202.     if len(plaintext) != len(defText):
  203.         return "signature is incorrect!"
  204.     for i in range(len(plaintext)):
  205.         if defText[i] != plaintext[i]:
  206.             return "signature is incorrect!"
  207.     return "signature is correct!"
  208.  
  209.  
  210. def calculate_closed_key_c(text, d, n):
  211.     ciphertext = [(abc[char] ** d) % n for char in text]
  212.     return ciphertext
  213.  
  214.  
  215. def create_keys():
  216.     p = int(entry_p.get("1.0", tk.END))
  217.     q = int(entry_q.get("1.0", tk.END))
  218.  
  219.     n = calculate_n(p, q)
  220.     phi = calculate_phi(p, q)
  221.     e = calculate_e(phi)
  222.     d = calculate_d(e, phi)
  223.  
  224.     global open_keys, closed_keys
  225.     open_keys = [n, e]
  226.     closed_keys = [p, q, phi, d]
  227.     messagebox.showinfo("Результат дешифрования", f"Созданные ключи: {open_keys, closed_keys}")
  228.  
  229.  
  230. def encrypt_message():
  231.     plaintext = entry_plaintext.get("1.0", tk.END)[0:-1]
  232.     #entry_plaintext.delete('1.0', tk.END)
  233.     e = open_keys[1]
  234.     n = open_keys[0]
  235.     d = closed_keys[3]
  236.     c = calculate_closed_key_c(plaintext, d, n)
  237.  
  238.     text_area.delete('1.0', tk.END)
  239.     text_area.insert('1.0', ','.join(map(str, c)))
  240.     messagebox.showinfo("Результат шифрования", f"Зашифрованный текст: {c}")
  241.  
  242.  
  243. def check_signature():
  244.     defText = entry_plaintext.get("1.0", tk.END)[0:-1]
  245.     ciphertext = [int(char) for char in text_area.get('1.0', tk.END).split(',')]
  246.     signature = decrypt(closed_keys, ciphertext, open_keys, defText)
  247.  
  248.     messagebox.showinfo("Результат проверки", f"Результат проверки: {signature}")
  249.  
  250.  
  251. def check_signature2():
  252.     defText = entry_plaintext.get("1.0", tk.END)[0:-1]
  253.     ciphertext = [int(char) for char in text_area2.get('1.0', tk.END).split(',')]
  254.     signature = decrypt(closed_keys, ciphertext, open_keys, defText)
  255.  
  256.     messagebox.showinfo("Результат проверки", f"Результат проверки: {signature}")
  257.  
  258.  
  259. root = tk.Tk()
  260. root.title("Алгоритм RSA")
  261.  
  262. closed_keys = []  # p, q, phi, d
  263. open_keys = []  # e,n
  264.  
  265.  
  266. frame_keys = tk.Frame(root)
  267. frame_keys.pack(pady=10)
  268.  
  269. button_create_keys = tk.Button(frame_keys, text="Создать ключи", command=create_keys)
  270. button_create_keys.pack()
  271.  
  272. frame_encrypted_text = tk.Frame(root)
  273. frame_encrypted_text.pack(pady=10)
  274.  
  275. label_encrypted_text = tk.Label(frame_encrypted_text, text="зашифрованное сообщение:")
  276. label_encrypted_text.pack(side=tk.LEFT)
  277.  
  278. text_area = tk.Text(frame_encrypted_text, height=2, width=15)
  279. text_area.pack(side=tk.LEFT)
  280.  
  281. button_decrypt = tk.Button(frame_encrypted_text, text="Проверить", command=check_signature)
  282. button_decrypt.pack(side=tk.RIGHT)
  283.  
  284. frame_input_text = tk.Frame(root)
  285. frame_input_text.pack(pady=10)
  286.  
  287. label_input_text = tk.Label(frame_input_text, text="Введите ключ:") #
  288. label_input_text.pack(side=tk.LEFT)
  289.  
  290. button_decrypt2 = tk.Button(frame_input_text, text="Проверить", command=check_signature2)
  291. button_decrypt2.pack(side=tk.RIGHT)
  292.  
  293. text_area2 = tk.Text(frame_input_text, height=2, width=15)
  294. text_area2.pack(side=tk.LEFT)
  295.  
  296. frame_encrypt = tk.Frame(root)
  297. frame_encrypt.pack(pady=10)
  298.  
  299. label_p = tk.Label(frame_encrypt, text="p:")
  300. label_p.pack(side=tk.LEFT)
  301.  
  302. entry_p = tk.Text(frame_encrypt, height=1, width=15)
  303. entry_p.pack(side=tk.LEFT)
  304.  
  305. label_q = tk.Label(frame_encrypt, text="q:")
  306. label_q.pack(side=tk.LEFT)
  307.  
  308. entry_q = tk.Text(frame_encrypt, height=1, width=15)
  309. entry_q.pack(side=tk.LEFT)
  310.  
  311. label_plaintext = tk.Label(frame_encrypt, text="Текст:")
  312. label_plaintext.pack(side=tk.LEFT)
  313.  
  314. entry_plaintext = tk.Text(frame_encrypt, height=1, width=15)
  315. entry_plaintext.pack(side=tk.LEFT)
  316.  
  317. button_encrypt = tk.Button(frame_encrypt, text="Зашифровать", command=encrypt_message)
  318. button_encrypt.pack(side=tk.LEFT)
  319.  
  320. root.mainloop()
  321.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement