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 hash_func(message):
- return sum(ord(c) for c in message)
- def generate_numbers():
- p = int(entry_p.get("1.0", tk.END))
- g = random.randint(2, p)
- x = random.randint(2, p)
- y = (g ** x) % p
- global open_keys, closed_keys
- open_keys = [x]
- closed_keys = [y, g, p]
- def calculate_k(p):
- g = 0
- k = 3
- while g != 1:
- k = random.randrange(1, p-2)
- if is_prime(k):
- g = gcd(k, p)
- return k
- def create_signature():
- generate_numbers()
- message = entry_plaintext.get("1.0", tk.END)[0:-1]
- p = closed_keys[2]
- g = closed_keys[1]
- x = open_keys[0]
- message = hash_func(message)
- while True:
- k = random.randint(2, p - 2)
- if gcd(k, p - 1) == 1:
- break
- a = (g ** k) % p
- b = ((message - x * a) * k) % (p - 1)
- a = int(a)
- b = int(b)
- text_area.delete('1.0', tk.END)
- text_area.insert('1.0', ','.join(map(str, [a, b])))
- def check_signature():
- a, b = [float(char) for char in text_area.get('1.0', tk.END).split(',')]
- a, b = int(a), int(b)
- message = entry_plaintext.get('1.0', tk.END)[0:-1]
- y = closed_keys[0]
- p = closed_keys[2]
- g = closed_keys[1]
- m = hash_func(message)
- print(y, a, b, g, m)
- left = ((y ** a) * (a ** b)) % p
- right = (g ** m) % p
- print(left, right)
- if left == right:
- messagebox.showinfo("Результат", "текст: Signature is correct!")
- else:
- messagebox.showinfo("Результат", "текст: Signature is incorrect")
- 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
- root = tk.Tk()
- root.title("Алгоритм Эль-Гамая")
- closed_keys = []
- open_keys = []
- frame_keys = tk.Frame(root)
- frame_keys.pack(pady=10)
- 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=check_signature)
- 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_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=create_signature)
- button_encrypt.pack(side=tk.LEFT)
- root.mainloop()
- #rsa--------------------------------------------------------------------------------------------
- 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_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, open_keys, defText):
- p, q, phi, d = private_key
- n, e = open_keys
- plaintext = []
- for num in ciphertext:
- plaintext.append(get_key(abc, ((num ** e) % n)))
- if len(plaintext) != len(defText):
- return "signature is incorrect!"
- for i in range(len(plaintext)):
- if defText[i] != plaintext[i]:
- return "signature is incorrect!"
- return "signature is correct!"
- def calculate_closed_key_c(text, d, n):
- ciphertext = [(abc[char] ** d) % n for char in text]
- return ciphertext
- 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 = 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]
- d = closed_keys[3]
- c = calculate_closed_key_c(plaintext, d, n)
- text_area.delete('1.0', tk.END)
- text_area.insert('1.0', ','.join(map(str, c)))
- messagebox.showinfo("Результат шифрования", f"Зашифрованный текст: {c}")
- def check_signature():
- defText = entry_plaintext.get("1.0", tk.END)[0:-1]
- ciphertext = [int(char) for char in text_area.get('1.0', tk.END).split(',')]
- signature = decrypt(closed_keys, ciphertext, open_keys, defText)
- messagebox.showinfo("Результат проверки", f"Результат проверки: {signature}")
- def check_signature2():
- defText = entry_plaintext.get("1.0", tk.END)[0:-1]
- ciphertext = [int(char) for char in text_area2.get('1.0', tk.END).split(',')]
- signature = decrypt(closed_keys, ciphertext, open_keys, defText)
- messagebox.showinfo("Результат проверки", f"Результат проверки: {signature}")
- 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)
- 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=check_signature)
- button_decrypt.pack(side=tk.RIGHT)
- frame_input_text = tk.Frame(root)
- frame_input_text.pack(pady=10)
- label_input_text = tk.Label(frame_input_text, text="Введите ключ:") #
- label_input_text.pack(side=tk.LEFT)
- button_decrypt2 = tk.Button(frame_input_text, text="Проверить", command=check_signature2)
- button_decrypt2.pack(side=tk.RIGHT)
- text_area2 = tk.Text(frame_input_text, height=2, width=15)
- text_area2.pack(side=tk.LEFT)
- 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