parent
4f68f5f312
commit
c48966f946
@ -0,0 +1,24 @@
|
||||
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_PEM, key).decode('utf-8')
|
||||
# 提取公钥并将其转换为 PEM 格式
|
||||
public_key_pem = crypto.dump_publickey(crypto.FILETYPE_PEM, key).decode('utf-8')
|
||||
# 将私钥保存到文件
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
try:
|
||||
file_path = tk.filedialog.asksaveasfilename(title="私钥保存",defaultextension=".pem", filetypes=[("PEM files", "*.pem")])
|
||||
with open(file_path, 'w') 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, 'w') as f:
|
||||
f.write(public_key_pem)
|
||||
print("RSA 密钥对已成功生成并保存为 PEM 文件。")
|
Loading…
Reference in new issue