The PEM format is base64 encoded DER data with some additional header and footer lines. You can just read DER as binary, convert it to PEM, and pass it to RSA.load_key_string
:
import base64 from M2Crypto import RSA TEMPLATE = """ -----BEGIN RSA PRIVATE KEY----- %s -----END RSA PRIVATE KEY----- """ raw = open('key.der', 'rb').read() data = TEMPLATE % base64.encodestring(raw).rstrip() key = RSA.load_key_string(data) print key
Conclusion:
<M2Crypto.RSA.RSA instance at 0x10eb710>
source share