- 首页 > it技术 > >
from Crypto.PublicKey import RSA
class create_rsa():
# 生成公私钥
def getkey(self):
x = RSA.generate(2048)
# y = RSA.generate(2048, Random.new().read)#可以使用伪随机数来辅助生成
s_key = x.export_key()# 私钥
g_key = x.publickey().export_key()# 公钥
print(s_key, '\n', g_key)
# 写入文件--1
with open(r"D:\vicky\demo\rsa_private.pem", "wb") as x:
x.write(s_key)
with open(r"D:\vicky\demo\rsa_public.pem", "wb") as x:
x.write(g_key)
print("公私钥对成功生成")
"""
# 从文件导入密钥 -- 通过私钥生成公钥(公钥不会变 -- 用于只知道私钥的情况)--2
with open('c.pem', 'rb')as x:
s_key = RSA.importKey(x.read())
new_g_key = s_key.publickey().export_key()
print(new_g_key)
cert = s_key.export_key("DER")# 生成证书 -- 它和私钥是唯一对应的
print(cert)
"""if __name__ == '__main__':
r = create_rsa()
r.getkey()
推荐阅读