File accessing in load_pub_key

谁说我不能喝 提交于 2019-12-25 04:51:53

问题


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

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