Loading a PKCS#1 public key using M2Crypto

Marc Aymerich glicerinu at gmail.com
Wed Jan 16 20:58:26 CET 2013

I've been trying very, very hard to load an RSA key using M2Crypto but without any success.

basically this is what I'm trying to do:
>>> from M2Crypto import BIO, RSA
>>> pubkey = """-----BEGIN RSA PUBLIC KEY-----
... MIIBCgKCAQEApwotnfHT9RAmxnuaGEMdI3lYPYE4aaqSD9v4KbTh1E7Le3GNJQb7
... wCpmDe8+n8S5Kp/gBEpWiYuvsVA/T4KseoX7NMcacP+DJMwjmNd9U58USn2vLz0Z
... TMtXpc/FUhW5PZdgCiuNzw6IFgGn9ZCCv85jjUIW3KD8fUNdrUfVSv4olDoL9NkR
... dTRg3Os/znC6l0gv/mqnLaqj2bJ/tx47kUmj6Oq13JuEq34T+DVmsUCFVundQnRp
... c/vVEqQot7Rvj9UmSvTi4WKt/qxiAnyZf3gXOdrXvxfVTGzD5I/Xg+By+a4C2JwB
... A5RGvZP3fyfhkCnnhFDpfws5lc20FA6ryQIDAQAB
... -----END RSA PUBLIC KEY-----"""
>>> bio = BIO.MemoryBuffer(pubkey.encode('ascii'))
>>> RSA.load_pub_key_bio(bio)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/M2Crypto/RSA.py", line 422, in load_pub_key_bio
  File "/usr/lib/python2.7/dist-packages/M2Crypto/RSA.py", line 302, in rsa_error
    raise RSAError, m2.err_reason_error_string(m2.err_get_error())
M2Crypto.RSA.RSAError: no start line

Reading all whats in Internet about this problem it seems that my key is in PKCS#1 format but M2Crypto only reads X.501 RSA keys. 

I know that python-crypto doesn't have any problem loading this key, but I'll prefer to stick with M2Crypto because I already have lots code using M2Crypto.

So my question is, is there any way to load this key using M2Crypto? Can I convert somehow the key to X.501?

I'll be very, very grateful if someone can come up with an interesting idea! 
thanks a lot!!

More information about the Python-list mailing list