基于 AES 算法的 CBC 工作模式可视化演示程序实现,使用python语言,用tkinter实现
时间: 2023-06-11 14:10:04 浏览: 106
AES算法演示程序
以下是一个基于AES算法的CBC工作模式可视化演示程序实现的示例代码,使用Python语言和Tkinter库:
```python
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import tkinter as tk
from tkinter import messagebox
class CBCDemo:
def __init__(self, master):
self.master = master
self.master.title("CBC工作模式可视化演示程序")
self.master.geometry("600x400")
self.master.resizable(False, False)
self.master.protocol("WM_DELETE_WINDOW", self.exit_app)
# 创建输入框和标签
self.plain_text_var = tk.StringVar()
self.cipher_text_var = tk.StringVar()
self.iv_var = tk.StringVar()
self.key_var = tk.StringVar()
self.padding_var = tk.StringVar()
self.padding_var.set("PKCS7")
tk.Label(self.master, text="明文:").grid(row=0, column=0, sticky=tk.W)
tk.Entry(self.master, width=50, textvariable=self.plain_text_var).grid(row=0, column=1, columnspan=2)
tk.Label(self.master, text="密文:").grid(row=1, column=0, sticky=tk.W)
tk.Entry(self.master, width=50, textvariable=self.cipher_text_var).grid(row=1, column=1, columnspan=2)
tk.Label(self.master, text="IV:").grid(row=2, column=0, sticky=tk.W)
tk.Entry(self.master, width=50, textvariable=self.iv_var).grid(row=2, column=1, columnspan=2)
tk.Label(self.master, text="密钥:").grid(row=3, column=0, sticky=tk.W)
tk.Entry(self.master, width=50, textvariable=self.key_var).grid(row=3, column=1, columnspan=2)
tk.Label(self.master, text="填充方式:").grid(row=4, column=0, sticky=tk.W)
tk.OptionMenu(self.master, self.padding_var, "PKCS7", "ISO7816").grid(row=4, column=1, sticky=tk.W)
# 创建按钮
tk.Button(self.master, text="加密", command=self.encrypt).grid(row=5, column=1, pady=10)
tk.Button(self.master, text="解密", command=self.decrypt).grid(row=5, column=2, pady=10)
def encrypt(self):
plain_text = self.plain_text_var.get()
iv = self.iv_var.get()
key = self.key_var.get()
padding = self.padding_var.get()
if not plain_text or not iv or not key:
messagebox.showerror("错误", "请输入明文、IV和密钥!")
return
if len(iv) != 16 or len(key) != 16:
messagebox.showerror("错误", "IV和密钥长度必须为16个字符!")
return
cipher = AES.new(key.encode(), AES.MODE_CBC, iv.encode())
if padding == "PKCS7":
plain_text = pad(plain_text.encode(), AES.block_size)
elif padding == "ISO7816":
plain_text = pad(plain_text.encode(), AES.block_size, style="ISO7816")
cipher_text = cipher.encrypt(plain_text)
self.cipher_text_var.set(cipher_text.hex())
def decrypt(self):
cipher_text = self.cipher_text_var.get()
iv = self.iv_var.get()
key = self.key_var.get()
padding = self.padding_var.get()
if not cipher_text or not iv or not key:
messagebox.showerror("错误", "请输入密文、IV和密钥!")
return
if len(iv) != 16 or len(key) != 16:
messagebox.showerror("错误", "IV和密钥长度必须为16个字符!")
return
cipher = AES.new(key.encode(), AES.MODE_CBC, iv.encode())
plain_text = cipher.decrypt(bytes.fromhex(cipher_text))
if padding == "PKCS7":
plain_text = unpad(plain_text, AES.block_size)
elif padding == "ISO7816":
plain_text = unpad(plain_text, AES.block_size, style="ISO7816")
self.plain_text_var.set(plain_text.decode())
def exit_app(self):
if messagebox.askokcancel("退出", "确定退出程序吗?"):
self.master.destroy()
if __name__ == "__main__":
root = tk.Tk()
CBCDemo(root)
root.mainloop()
```
在这个示例代码中,我们使用Crypto库中的AES模块和Padding模块实现加密和解密功能,并使用Tkinter库实现了一个简单的图形用户界面。在界面中,用户可以输入明文、IV、密钥和填充方式,并点击“加密”按钮或“解密”按钮执行相应的操作。加密和解密过程中使用的是CBC工作模式,填充方式可以选择PKCS7或ISO7816。加密后的密文将以十六进制字符串的形式显示在界面上,解密后的明文将显示在明文输入框中。
阅读全文