下面是一个示例代码,实现了一个基于Tkinter的Base64加密程序。该程序可以选择纯文本、图片或文件,并可以进行加密和解密操作。同时,程序还会生成一对RSA密钥作为加密和解密的密钥。
import tkinter as tk
from tkinter import filedialog
import base64
from Crypto.PublicKey import RSA
def generate_rsa_key():
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
return private_key, public_key
def encrypt(base64_str, rsa_public_key):
key = RSA.import_key(rsa_public_key)
cipher = key.encrypt(base64_str.encode(), 0)[0]
return base64.b64encode(cipher)
def decrypt(encrypted_base64_str, rsa_private_key):
key = RSA.import_key(rsa_private_key)
encrypted_data = base64.b64decode(encrypted_base64_str)
return key.decrypt(encrypted_data).decode()
def select_file():
file_path = filedialog.askopenfilename(initialdir="/", title="Select a File")
file_path_entry.delete(0, tk.END)
file_path_entry.insert(tk.END, file_path)
def encrypt_text():
text = text_entry.get("1.0", tk.END).strip()
public_key = public_key_text.get("1.0", tk.END).strip()
encrypted_text = encrypt(text, public_key)
encrypted_text_entry.delete(0, tk.END)
encrypted_text_entry.insert(tk.END, encrypted_text)
def decrypt_text():
encrypted_text = encrypted_text_entry.get().strip()
private_key = private_key_text.get("1.0", tk.END).strip()
decrypted_text = decrypt(encrypted_text, private_key)
decrypted_text_entry.delete("1.0", tk.END)
decrypted_text_entry.insert(tk.END, decrypted_text)
def encrypt_file():
file_path = file_path_entry.get().strip()
public_key = public_key_file.get("1.0", tk.END).strip()
with open(file_path, "rb") as file:
data = file.read()
base64_data = base64.b64encode(data).decode()
encrypted_data = encrypt(base64_data, public_key)
encrypted_file_path = filedialog.asksaveasfilename(initialdir="/", title="Save Encrypted File")
with open(encrypted_file_path, "wb") as encrypted_file:
encrypted_file.write(encrypted_data)
def decrypt_file():
file_path = file_path_entry.get().strip()
private_key = private_key_file.get("1.0", tk.END).strip()
with open(file_path, "rb") as encrypted_file:
encrypted_data = encrypted_file.read()
decrypted_data = decrypt(encrypted_data, private_key)
decrypted_data = base64.b64decode(decrypted_data)
decrypted_file_path = filedialog.asksaveasfilename(initialdir="/", title="Save Decrypted File")
with open(decrypted_file_path, "wb") as decrypted_file:
decrypted_file.write(decrypted_data)
# 创建主窗口
window = tk.Tk()
window.title("Base64加密程序")
# 创建文本输入框和按钮
text_label = tk.Label(window, text="请输入要加密的文本:")
text_label.pack()
text_entry = tk.Text(window, height=3)
text_entry.pack()
encrypt_text_button = tk.Button(window, text="加密文本", command=encrypt_text)
encrypt_text_button.pack()
encrypted_text_entry = tk.Entry(window)
encrypted_text_entry.pack()
# 创建文本解密区域
encrypted_text_label = tk.Label(window, text="请输入要解密的文本:")
encrypted_text_label.pack()
encrypted_text_entry = tk.Entry(window)
encrypted_text_entry.pack()
decrypt_text_button = tk.Button(window, text="解密文本", command=decrypt_text)
decrypt_text_button.pack()
decrypted_text_entry = tk.Text(window, height=3)
decrypted_text_entry.pack()
# 创建文件选择区域和按钮
file_path_label = tk.Label(window, text="请选择要加密或解密的文件:")
file_path_label.pack()
file_path_entry = tk.Entry(window)
file_path_entry.pack()
select_file_button = tk.Button(window, text="选择文件", command=select_file)
select_file_button.pack()
# 创建公钥和私钥生成按钮
generate_rsa_key_button = tk.Button(window, text="生成RSA公钥和私钥", command=generate_rsa_key)
generate_rsa_key_button.pack()
public_key_label = tk.Label(window, text="公钥:")
public_key_label.pack()
public_key_text = tk.Text(window, height=10, width=50)
public_key_text.pack()
private_key_label = tk.Label(window, text="私钥:")
private_key_label.pack()
private_key_text = tk.Text(window, height=10, width=50)
private_key_text.pack()
# 创建加密和解密文件按钮
encrypt_file_button = tk.Button(window, text="加密文件", command=encrypt_file)
encrypt_file_button.pack()
decrypt_file_button = tk.Button(window, text="解密文件", command=decrypt_file)
decrypt_file_button.pack()
# 运行主循环
window.mainloop()
上述代码使用了Tkinter来实现用户界面,可以执行加密和解密操作,并且可以选择文本、图片或文件。此外,程序还支持通过生成公钥和私钥来加密和解密数据。由于窗口的美观程度是主观的,本示例程序仅提供了基本的布局样式,你可以根据自己的需求进行美化。
请注意,该代码使用了Crypto.PublicKey.RSA
模块来生成RSA密钥对,并进行加密和解密操作。确保你已经安装了pycryptodome
库,可以使用pip install pycryptodome
进行安装。
这只是一个基本的示例程序,你可以根据自己的需求进行修改和定制。