写一个基于RSA算法的数字签名的程序包含GUI界面
时间: 2024-02-20 18:56:12 浏览: 82
以下是一个基于Python语言实现的RSA数字签名程序,包含GUI界面:
```python
import random
import hashlib
import tkinter as tk
# 生成质数
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5)+1):
if num % i == 0:
return False
return True
def generate_prime():
while True:
num = random.randint(100, 1000)
if is_prime(num):
return num
# 扩展欧几里得算法求解模反元素
def extended_gcd(a, b):
if b == 0:
return a, 1, 0
else:
gcd, x, y = extended_gcd(b, a % b)
return gcd, y, x - (a // b) * y
# 生成公钥和私钥
def generate_key_pair():
# 随机生成两个质数p和q
p = generate_prime()
q = generate_prime()
n = p * q
phi_n = (p - 1) * (q - 1)
# 选择一个与phi_n互质的整数e作为公钥
while True:
e = random.randint(2, phi_n-1)
if extended_gcd(e, phi_n)[0] == 1:
break
# 计算e的模反元素d作为私钥
gcd, d, _ = extended_gcd(e, phi_n)
d = d % phi_n
if d < 0:
d += phi_n
return (n, e), (n, d)
# 加密
def encrypt(m, public_key):
n, e = public_key
c = pow(m, e, n)
return c
# 解密
def decrypt(c, private_key):
n, d = private_key
m = pow(c, d, n)
return m
# 签名
def sign(message, private_key):
n, d = private_key
hash = hashlib.sha256(message.encode('utf-8')).hexdigest()
h = int(hash, 16)
signature = pow(h, d, n)
return signature
# 验证签名
def verify(message, signature, public_key):
n, e = public_key
hash = hashlib.sha256(message.encode('utf-8')).hexdigest()
h = int(hash, 16)
h_new = pow(signature, e, n)
return h == h_new
# GUI界面
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title('RSA数字签名')
self.pack()
self.create_widgets()
def create_widgets(self):
# 待签名消息
self.message_label = tk.Label(self, text='待签名消息:')
self.message_label.grid(row=0, column=0, padx=5, pady=5)
self.message_entry = tk.Entry(self, width=40)
self.message_entry.grid(row=0, column=1, padx=5, pady=5)
# 生成密钥对
self.generate_key_button = tk.Button(self, text='生成密钥对', command=self.generate_key)
self.generate_key_button.grid(row=1, column=0, padx=5, pady=5)
# 公钥
self.public_key_label = tk.Label(self, text='公钥:')
self.public_key_label.grid(row=2, column=0, padx=5, pady=5)
self.public_key_text = tk.Text(self, height=3, width=40)
self.public_key_text.grid(row=2, column=1, padx=5, pady=5)
# 私钥
self.private_key_label = tk.Label(self, text='私钥:')
self.private_key_label.grid(row=3, column=0, padx=5, pady=5)
self.private_key_text = tk.Text(self, height=3, width=40)
self.private_key_text.grid(row=3, column=1, padx=5, pady=5)
# 签名
self.sign_button = tk.Button(self, text='签名', command=self.sign_message)
self.sign_button.grid(row=4, column=0, padx=5, pady=5)
self.signature_label = tk.Label(self, text='签名结果:')
self.signature_label.grid(row=4, column=1, padx=5, pady=5)
self.signature_text = tk.Text(self, height=3, width=40)
self.signature_text.grid(row=5, column=1, padx=5, pady=5)
# 验证签名
self.verify_button = tk.Button(self, text='验证签名', command=self.verify_signature)
self.verify_button.grid(row=6, column=0, padx=5, pady=5)
self.is_valid_label = tk.Label(self, text='签名是否有效:')
self.is_valid_label.grid(row=6, column=1, padx=5, pady=5)
self.is_valid_text = tk.Text(self, height=1, width=10)
self.is_valid_text.grid(row=7, column=1, padx=5, pady=5)
# 退出
self.quit_button = tk.Button(self, text='退出', command=self.master.quit)
self.quit_button.grid(row=8, column=1, padx=5, pady=5)
# 生成密钥对
def generate_key(self):
self.public_key, self.private_key = generate_key_pair()
self.public_key_text.delete(1.0, tk.END)
self.public_key_text.insert(tk.END, str(self.public_key))
self.private_key_text.delete(1.0, tk.END)
self.private_key_text.insert(tk.END, str(self.private_key))
# 签名
def sign_message(self):
message = self.message_entry.get()
if message:
signature = sign(message, self.private_key)
self.signature_text.delete(1.0, tk.END)
self.signature_text.insert(tk.END, str(signature))
# 验证签名
def verify_signature(self):
message = self.message_entry.get()
signature = self.signature_text.get(1.0, tk.END)
if message and signature:
is_valid = verify(message, int(signature), self.public_key)
self.is_valid_text.delete(1.0, tk.END)
self.is_valid_text.insert(tk.END, str(is_valid))
# 启动GUI程序
if __name__ == '__main__':
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
在程序中,首先定义了一些辅助函数,包括生成质数、扩展欧几里得算法求解模反元素等。然后,利用这些函数实现了RSA算法的核心功能,包括生成密钥对、加密、解密、签名和验证签名。最后,使用tkinter库创建了一个简单的GUI界面,包括待签名消息、生成密钥对、公钥和私钥、签名结果、验证签名和退出按钮。用户可以在GUI界面中输入待签名消息,点击“生成密钥对”按钮生成公钥和私钥,点击“签名”按钮进行签名,点击“验证签名”按钮验证签名是否有效。
阅读全文