0

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?

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • exact duplicate: http://stackoverflow.com/questions/1176055/how-to-use-pem-file-with-python-m2crypto – SilentGhost Aug 25 '09 at 10:31
  • your code doesn't correspond to your error. straighten your story. – SilentGhost Aug 25 '09 at 10:32
  • no, it isn't. answer to that question quite clearly indicates that you need to pass filename to `load_pub_key`. if you want anything to be written to your file you'd need to flush the buffer by closing the `fileHandle`. – SilentGhost Aug 25 '09 at 10:40
  • ya its duplicate but here the problem in accessing the file in the method.is there any other way to pass the file i.e with the directory? –  Aug 25 '09 at 10:41
  • problem is why the file is not opening after simply passing in the method –  Aug 25 '09 at 10:49
  • 1
    **answers to previous question clearly state that you must pass filename to `load_pub_key`**. is that clear now? – SilentGhost Aug 25 '09 at 10:56

3 Answers3

0

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.

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
0

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)
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • thanks its seems cool but after applying this code again error : Traceback (most recent call last): File "RetEnc.py", line 17, in ? pub_key = M2Crypto.RSA.load_pub_key(fname) File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 344, in load_pub_key return load_pub_key_bio(bio) File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 360, in load_pub_key_bio rsa_error() File "/usr/lib/python2.4/site-packages/M2Crypto/RSA.py", line 240, in rsa_error raise RSAError, m2.err_reason_error_string(m2.err_get_error()) M2Crypto.RSA.RSAError: no start line –  Aug 25 '09 at 11:00
  • so content of your file is malformed. and it's outside of the scope of this question. – SilentGhost Aug 25 '09 at 11:05
0

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

Community
  • 1
  • 1
Sayat Satybald
  • 6,300
  • 5
  • 35
  • 52