python tkinter 实现 带界面(GUI)的RSA加密、签名
代码环境,python3.5.2
RSA加密的过程是:使用公钥加密,私钥解密
RSA签名的过程是:使用私钥签名,公钥验证
所以核心代码就是,生成公钥私钥,使用公钥私钥分别进行加密解密。
在实际编码的时候,使用python自带的RSA库,需要特别处理编码问题,因为默认的加密解密函数是接收bytes类型的数据。
加密的核心代码如下:
# -*- coding: utf-8 -*- import rsa import base64 (public_key, private_key) = rsa.newkeys(1024) msg = "M2Crypto python关于RSA的库还是蛮多的,当然也可以直接用openSSLqweasasd123" msg = msg.encode(encoding="utf-8") encrypt_msg = rsa.encrypt(msg, public_key) decrypt_msg = rsa.decrypt(encrypt_msg, private_key) print("公钥:") print(public_key.save_pkcs1().decode('utf8')) print("\n私钥:") print(private_key.save_pkcs1().decode('utf8')) print("\n原始文本:") print(msg.decode('utf8')) print("\n加密后的文本:") print(base64.encodebytes( encrypt_msg).decode('utf8')) print("\n解密后的文本:") print(decrypt_msg.decode('utf8'))
输出结果如下:
结合tkinter的代码如下,使用tkinter是因为绘制界面很简单:
# -*- coding:utf-8 -*- from tkinter import * import rsa import base64 def GenerateKey(): (public_key, private_key) = rsa.newkeys(1024) print("\n生成公钥:"+public_key.save_pkcs1().decode('utf8')) print("\n生成私钥:"+private_key.save_pkcs1().decode('utf8')) publicKeyText.delete(0.0, END) publicKeyText.insert(END, public_key.save_pkcs1().decode('utf8')) privateKeyText.delete(0.0, END) privateKeyText.insert(END, private_key.save_pkcs1().decode('utf8')) def EncryptionByPublickey(): #用公钥加密 public_key_str = publicKeyText.get("0.0", "end").encode(encoding="utf-8") public_key = rsa.PublicKey.load_pkcs1(public_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8") encrypt_msg = rsa.encrypt(entry_str, public_key) print("公钥加密后的文本为:\n"+base64.encodebytes(encrypt_msg).decode('utf8')) outputText.delete(0.0, END) outputText.insert(END, base64.encodebytes(encrypt_msg).decode('utf8')) def EncryptionByPrivatekey(): private_key_str = privateKeyText.get("0.0", "end").encode(encoding="utf-8") private_key = rsa.PrivateKey.load_pkcs1(private_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8") encrypt_msg = rsa.encrypt(entry_str, private_key) print("私钥加密后的文本为:\n"+base64.encodebytes(encrypt_msg).decode('utf8')) outputText.delete(0.0, END) outputText.insert(END, base64.encodebytes(encrypt_msg).decode('utf8')) def DeryptionByPublickey(): public_key_str = privateKeyText.get("0.0", "end").encode(encoding="utf-8") public_key = rsa.PrivateKey.load_pkcs1(public_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8") encrypt_msg = base64.decodebytes( entry_str) outputText.delete(0.0, END) try: decrypt_msg = rsa.decrypt(encrypt_msg, public_key) print("公钥解密后的文本为:\n"+decrypt_msg.decode('utf8')) outputText.insert(END,decrypt_msg.decode('utf8')) except: decrypt_msg = "公钥解密失败" print(decrypt_msg) outputText.insert(END,decrypt_msg) def DecryptionByPrivatekey(): private_key_str = privateKeyText.get("0.0", "end").encode(encoding="utf-8") private_key = rsa.PrivateKey.load_pkcs1(private_key_str) entry_str = entryText.get("0.0", "end").encode(encoding="utf-8") encrypt_msg = base64.decodebytes( entry_str) outputText.delete(0.0, END) try: decrypt_msg = rsa.decrypt(encrypt_msg, private_key) print("私钥解密后的文本为:\n"+decrypt_msg.decode('utf8')) outputText.insert(END,decrypt_msg.decode('utf8')) except: decrypt_msg = "私钥解密失败" print(decrypt_msg) outputText.insert(END,decrypt_msg) window = Tk() window.title("RSA加密解密软件") frame = Frame(window) frame.pack() label = Label(frame, text = "公钥:") label.grid(row = 1, column = 1,columnspan= 4) publicKeyText = Text(frame,width=50,height=8) publicKeyText.grid(row = 2, column = 1,columnspan = 4) label = Label(frame, text = "私钥:") label.grid(row = 3, column = 1,columnspan= 4) privateKeyText = Text(frame,width=50,height=12) privateKeyText.grid(row = 4, column = 1,columnspan = 4) btGenerateKey = Button(frame, text = "生成公钥/私钥",command=GenerateKey) btGenerateKey.grid(row = 5, column = 1,columnspan = 4) label = Label(frame, text = "请输入加密/解密的文本:") label.grid(row = 6, column = 1,columnspan = 4) entryText = Text(frame,width=50,height=5) entryText.grid(row = 7, column = 1,columnspan = 4) btEncryptionByPublickey = Button(frame, text = "公钥加密",command=EncryptionByPublickey) btEncryptionByPublickey.grid(row = 8, column = 1,pady = 10) btDeryptionByPublickey = Button(frame, text = "公钥解密",command=DeryptionByPublickey) btDeryptionByPublickey.grid(row = 8, column = 2) btEncryptionByPrivatekey = Button(frame, text = "私钥加密",command=EncryptionByPrivatekey) btEncryptionByPrivatekey.grid(row = 8, column = 3) btDecryptionByPrivatekey = Button(frame, text = "私钥解密",command=DecryptionByPrivatekey) btDecryptionByPrivatekey.grid(row = 8, column = 4) outputText = Text(frame,width=50,height=5) outputText.grid(row = 9, column = 1,columnspan = 4) print("欢迎使用本软件……") GenerateKey(); mainloop() print("欢迎再次使用本软件……")
加密过程:
使用公钥加密:
使用私钥解密:
数字签名过程:
使用私钥签名:
使用公钥验证: