gpt4 book ai didi

从 .pem 文件读取时的 python "RSA key format is not supported"

转载 作者:行者123 更新时间:2023-12-05 01:18:55 24 4
gpt4 key购买 nike

这是我的代码:

from Crypto.PublicKey import RSA

#Write key to file
key = RSA.generate(4096)
privateKey = key.exportKey()
file1 = open('keyfile.pem', 'wb')
file1.write(privateKey)
file1.close()

#Read key from file
file2 = open('keyfile.pem', 'rb')
key = RSA.importKey(file2.read()) #this is the problem

错误是“不支持 RSA key 格式。”任何人都可以帮助我从文件中写入/读取私钥的最佳方法吗?

最佳答案

我的答案和一对 key 有点复杂

from Crypto.PublicKey import RSA
key = RSA.generate(4096)
f = open('/home/john/Desktop/my_rsa_public.pem', 'wb')
f.close()
f.write(key.publickey().exportKey('PEM'))
f = open('/home/john/Desktop/my_rsa_private.pem', 'wb')
f.write(key.exportKey('PEM'))
f.close()

f = open('/home/john/Desktop/my_rsa_public.pem', 'rb')
f1 = open('/home/john/Desktop/my_rsa_private.pem', 'rb')
key = RSA.importKey(f.read())
key1 = RSA.importKey(f1.read())

x = key.encrypt(b"dddddd",32)

print(x)
z = key1.decrypt(x)
print(z)

关于从 .pem 文件读取时的 python "RSA key format is not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42892875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com