这是我的代码:
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密钥格式”。有人能帮助我从文件中写入/读取私钥的最佳方法吗?
我的答案和一对钥匙有点复杂
您的代码有多个问题,主要是您读写密钥的方式。您从不关闭该文件,然后在read函数期间将其打开两次;请尝试将代码更改为:
结果:
相关问题 更多 >
编程相关推荐