You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
1015 B
24 lines
1015 B
from OpenSSL import crypto
|
|
import tkinter as tk
|
|
import tkinter.filedialog
|
|
# 生成 RSA 密钥对
|
|
key = crypto.PKey()
|
|
key.generate_key(crypto.TYPE_RSA, 2048) # 2048 是密钥位大小,也可以是 1024, 4096 等
|
|
# 提取私钥并将其转换为 PEM 格式
|
|
private_key_pem = crypto.dump_privatekey(crypto.FILETYPE_ASN1, key)
|
|
# 提取公钥并将其转换为 PEM 格式
|
|
public_key_pem = crypto.dump_publickey(crypto.FILETYPE_PEM, key)
|
|
# 将私钥保存到文件
|
|
root = tk.Tk()
|
|
root.withdraw()
|
|
try:
|
|
file_path = tk.filedialog.asksaveasfilename(title="私钥保存",defaultextension=".pem", filetypes=[("PEM files", "*.pem")])
|
|
with open(file_path, 'wb') as f:
|
|
f.write(private_key_pem)
|
|
except:
|
|
print("私钥保存失败")
|
|
# 将公钥保存到文件
|
|
file_path = tk.filedialog.asksaveasfilename(title="公钥保存",defaultextension=".pem", filetypes=[("PEM files", "*.pem")])
|
|
with open(file_path, 'wb') as f:
|
|
f.write(public_key_pem)
|
|
print("RSA 密钥对已成功生成并保存为 PEM 文件。") |