[PYTHON-CRYPTO] M2Crypto convert key from PEM to DER ?

Mads Kiilerich mads at KIILERICH.COM
Wed Apr 8 02:19:49 CEST 2009


Hans Lellelid wrote, On 04/08/2009 01:02 AM:
> I'd like to use M2Crypto to to convert a server key from PEM format to 
> DER. Specifically, I'd like to replace the following openssl command:
>
> *openssl rsa –in input.key –inform PEM –out output.key –outform DER
>
> I think I understand how to do something similar with the certs, but 
> haven't figured out how to do this with just the private key. I'm sure 
> I'm missing something; can anyone provide any tips?

Using M2Crypto for that might be overkill, though it would be nice not 
to have to write it from scratch every time.

Untested and mostly from memory which might be wrong:

pem = open('cert.pem')
der = ''.join(l.strip() for l in 
pem.read().split('-----')[2].splitlines()).decode('base64')
open('cert.der', 'w').write(der)

/Mads

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3435 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-crypto/attachments/20090408/238cd73b/attachment.bin>


More information about the python-crypto mailing list