0% found this document useful (0 votes)
4 views3 pages

Demo_3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Demo_3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import tkinter as tk

from tkinter import ttk, filedialog, messagebox


import random

# (Các hàm liên quan đến mã hóa và giải mã ElGamal được giữ nguyên)

# Hàm mã hóa ElGamal theo từng từ


def encrypt_file(file_path, public_key):
p, g, h = public_key
with open(file_path, "r", encoding="utf-8") as file:
data = file.read()
words = data.split() # Tách nội dung thành danh sách từ
cipher = []
for word in words:
k = random.randint(1, p - 2)
c1 = power_mod(g, k, p)
m = sum(ord(char) for char in word) % p # Tổng mã Unicode của các ký tự
trong từ
c2 = (m * power_mod(h, k, p)) % p
cipher.append((c1, c2))
encrypted_file = file_path + ".enc"
with open(encrypted_file, "w", encoding="utf-8") as file:
file.write(str(cipher))
return encrypted_file

# Hàm giải mã ElGamal theo từng từ


def decrypt_file(file_path, private_key):
p, g, x = private_key
with open(file_path, "r", encoding="utf-8") as file:
cipher = eval(file.read())
words = []
for c1, c2 in cipher:
s = power_mod(c1, x, p)
s_inv = pow(s, -1, p)
m = (c2 * s_inv) % p
word = ''.join(chr((m // (256 ** i)) % 256) for i in range(len(str(m)))) #
Chuyển đổi số thành từ
words.append(word)
decrypted_file = file_path.replace(".enc", ".dec")
with open(decrypted_file, "w", encoding="utf-8") as file:
file.write(' '.join(words))
return decrypted_file

# Hàm tạo giao diện đẹp hơn


def create_gui():
def generate_keys_action():
global public_key, private_key
public_key, private_key = generate_keys()
public_key_label.config(text=f"Public Key: {public_key}")
private_key_label.config(text=f"Private Key: {private_key}")
messagebox.showinfo("Key Generation", "Keys generated successfully!")

def encrypt_action():
if not public_key:
messagebox.showerror("Error", "Please generate keys first!")
return
file_path = filedialog.askopenfilename(title="Select File to Encrypt")
if not file_path:
return
try:
encrypted_file = encrypt_file(file_path, public_key)
messagebox.showinfo("Encryption", f"File encrypted successfully:
{encrypted_file}")
except Exception as e:
messagebox.showerror("Error", f"Encryption failed: {e}")

def decrypt_action():
if not private_key:
messagebox.showerror("Error", "Please generate keys first!")
return
file_path = filedialog.askopenfilename(title="Select File to Decrypt")
if not file_path:
return
try:
decrypted_file = decrypt_file(file_path, private_key)
messagebox.showinfo("Decryption", f"File decrypted successfully:
{decrypted_file}")
except Exception as e:
messagebox.showerror("Error", f"Decryption failed: {e}")

# Giao diện chính


root = tk.Tk()
root.title("ElGamal File Encryption")
root.geometry("700x400")
root.resizable(False, False)

# Khung bao
frame = ttk.Frame(root, padding="20")
frame.pack(fill=tk.BOTH, expand=True)

# Tiêu đề
title_label = ttk.Label(frame, text="ElGamal File Encryption", font=("Arial",
18, "bold"))
title_label.pack(pady=10)

# Các nút chức năng


button_frame = ttk.Frame(frame)
button_frame.pack(pady=20)

ttk.Button(button_frame, text="Generate Keys", command=generate_keys_action,


width=20).grid(row=0, column=0, padx=10, pady=10)
ttk.Button(button_frame, text="Encrypt File", command=encrypt_action,
width=20).grid(row=0, column=1, padx=10, pady=10)
ttk.Button(button_frame, text="Decrypt File", command=decrypt_action,
width=20).grid(row=0, column=2, padx=10, pady=10)

# Thông tin khóa


global public_key_label, private_key_label
public_key_label = ttk.Label(frame, text="Public Key: None", wraplength=650,
justify="left")
public_key_label.pack(pady=5)
private_key_label = ttk.Label(frame, text="Private Key: None", wraplength=650,
justify="left")
private_key_label.pack(pady=5)

# Chạy giao diện


root.mainloop()
# Biến toàn cục để lưu khóa
public_key = None
private_key = None

# Chạy chương trình


if __name__ == "__main__":
create_gui()

You might also like