How to load a public RSA key into Python-RSA from a file?

丶灬走出姿态 提交于 2019-11-29 02:06:45
Googuez

Python-RSA uses the PEM RSAPublicKey format and the PEM RSAPublicKey format uses the header and footer lines: openssl NOTES

-----BEGIN RSA PUBLIC KEY-----
-----END RSA PUBLIC KEY-----

Output the public part of a private key in RSAPublicKey format: openssl EXAMPLES

 openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem

If on Python3, You also need to open the key in binary mode, e.g:

with open('private_key.pem', 'rb') as privatefile:

To load an OpenSSL generated public key file with python-rsa library, try

with open('public_key.pub', mode='rb') as public_file:
    key_data = public_file.read()
    public_key = rsa.PublicKey.load_pkcs1_openssl_pem(key_data)

You can generate private key by ssh-keygen:

ssh-keygen -t rsa

and generate public key like this:

ssh-keygen -e -m pem -f xxx > pubkey.pem

http://blog.oddbit.com/2011/05/08/converting-openssh-public-keys/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!