Saving RSA keys to a file, using pycrypto

十年热恋 提交于 2019-11-28 09:51:11
wRAR

keys.exportKey() for the private key, keys.publickey().exportKey() for the public key. You can change the output format with format argument, see the docs at this site.

The following piece of code will create the RSA key pair and store them in PEM files as well as print them.

Original credits : wRAR from this post & AJ poultier from [pyOpenSSL creating a pem file

from Crypto.PublicKey import RSA
private_key = RSA.generate(1024)
public_key = private_key.publickey()
print(private_key.exportKey(format='PEM'))
print(public_key.exportKey(format='PEM'))

with open ("private.pem", "w") as prv_file:
    print("{}".format(private_key.exportKey()), file=prv_file)

with open ("public.pem", "w") as pub_file:
    print("{}".format(public_key.exportKey()), file=pub_file)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!