问题
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