Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36930
| X-Received | by 10.224.192.199 with SMTP id dr7mr2769035qab.4.1358414181233; Thu, 17 Jan 2013 01:16:21 -0800 (PST) |
|---|---|
| X-Received | by 10.49.116.34 with SMTP id jt2mr949475qeb.38.1358414181208; Thu, 17 Jan 2013 01:16:21 -0800 (PST) |
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!p13no2798035qai.0!news-out.google.com!k2ni675qap.0!nntp.google.com!p13no948807qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Thu, 17 Jan 2013 01:16:21 -0800 (PST) |
| In-Reply-To | <m2k3rcr6na.fsf@cochabamba.vanoostrum.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=77.228.176.219; posting-account=C3vKYAoAAACZxvTRSmvXeNIMesCMgFgp |
| NNTP-Posting-Host | 77.228.176.219 |
| References | <922c97d0-77cf-4e68-8edc-d65c6d7e7831@googlegroups.com> <m2k3rcr6na.fsf@cochabamba.vanoostrum.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <1fde980b-ca96-4e4d-867d-edb5e6f4871f@googlegroups.com> (permalink) |
| Subject | Re: Loading a PKCS#1 public key using M2Crypto |
| From | Marc Aymerich <glicerinu@gmail.com> |
| Injection-Date | Thu, 17 Jan 2013 09:16:21 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Received-Bytes | 4340 |
| Xref | csiph.com comp.lang.python:36930 |
Show key headers only | View raw
On Thursday, January 17, 2013 1:32:25 AM UTC+1, Piet van Oostrum wrote:
> Marc Aymerich <glicerinu@gmail.com> writes:
>
>
>
> > Hi,
>
> > 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
>
> > rsa_error()
>
> > 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?
>
> >
>
> Converting to X.501 isn't difficult (assuming this is a 2048 bit key):
>
> Get rid of the 'RSA' in header and trailer
>
> Prepend X.501 header 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' to the data
>
> Reformat the lines to 64 characters.
>
>
>
> 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-----
>
> """
>
>
>
> pk = pubkey.split('\n')
>
> pk = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' + ''.join(pk[1:-2])
>
> pk = [pk[i:i+64] for i in range(0, len(pk), 64)]
>
> pk = '-----BEGIN PUBLIC KEY-----\n' + '\n'.join(pk) + '\n-----END PUBLIC KEY-----'
>
>
>
> bio = BIO.MemoryBuffer(pk) # pk is ASCII, don't encode
>
> key = RSA.load_pub_key_bio(bio)
>
>
Piet, that was really awesome, it seems so easy to do now :)
Thank you very, very much! really.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Loading a PKCS#1 public key using M2Crypto Marc Aymerich <glicerinu@gmail.com> - 2013-01-16 11:58 -0800
Re: Loading a PKCS#1 public key using M2Crypto Piet van Oostrum <piet@vanoostrum.org> - 2013-01-17 01:32 +0100
Re: Loading a PKCS#1 public key using M2Crypto Marc Aymerich <glicerinu@gmail.com> - 2013-01-17 01:16 -0800
Re: Loading a PKCS#1 public key using M2Crypto Piet van Oostrum <piet@vanoostrum.org> - 2013-01-17 17:39 +0100
Re: Loading a PKCS#1 public key using M2Crypto Marc Aymerich <glicerinu@gmail.com> - 2013-01-17 09:13 -0800
Re: Loading a PKCS#1 public key using M2Crypto Piet van Oostrum <piet@vanoostrum.org> - 2013-01-18 00:10 +0100
Re: Loading a PKCS#1 public key using M2Crypto Marc Aymerich <glicerinu@gmail.com> - 2013-01-18 10:13 +0100
csiph-web