How to use .pem file with Python M2Crypto

♀尐吖头ヾ 提交于 2019-12-22 11:33:40

问题


To generate an RSA key pair I used openssl:

openssl genrsa -out my_key.private.pem 1024
openssl rsa -in my_key.private.pem -pubout -out my_key.public.pem

Now I want to use this my_key.public.pem file in a function of another .py file:

import M2Crypto
from M2Crypto import RSA,SSL

def encrypt():
    pk = open( 'my_key.public.pem', 'rb' ).read()
    rsa = M2Crypto.RSA.load_pub_key(pk)
    print rsa;

Am I doing it right? Both files are in same directory, but this function is not giving any output.


回答1:


According to the documentation the load_pub_key expects a file name as input. It returns a M2Crypto.RSA.RSA_pub object, which doesn't make sense to print. What exactly are trying to achieve?




回答2:


Try this:

RSA.load_key('mykey.pem')


来源:https://stackoverflow.com/questions/1176055/how-to-use-pem-file-with-python-m2crypto

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