问题
Consider the following code:
fileHandle = open ( 'test8.pem','w' )
fileHandle.write (data)
pub_key = M2Crypto.RSA.load_pub_key(open('test8.pem'))
Which produces the following error:
File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 343, in load_pub_key
bio = BIO.openfile(file)
File "/usr/lib/python2.4/site-packages/M2Crypto/BIO.py", line 186, in openfile
return File(open(filename, mode))
IOError: [Errno 2] No such file or directory: ''
How do I pass the file into load_pub_key
method so it can be accessible by simply passing the file name?
回答1:
If you pass test8.pem without quotes, Python interprets it as the name of a variable, which is not defined, hence the error.
I don't know the specific library you are using but I would guess that you need to pass fileHandle instead.
回答2:
this should work for you:
fname = 'test8.pem'
fileHandle = open(fname, 'w')
fileHandle.write(data)
fileHandle.close()
pub_key = M2Crypto.RSA.load_pub_key(fname)
回答3:
I also have the same issue. I tried loading a file handler instead of path but it didn't help.
The thing that workout was using X509 module from M2Crypto. You can try use this functions to obtain a public key instance:
certificate = M2Crypto.X509.load_cert(cert_path)
pubkey = certificate.get_pubkey()
More details in the following answer: RSACryptoServiceProvider message signature verification with m2crypto
来源:https://stackoverflow.com/questions/1327211/file-accessing-in-load-pub-key