Loading a DER-encoded RSA key using M2Crypto

自作多情 提交于 2019-12-13 15:22:11

问题


The method M2Crypto.RSA.RSA().save_key_der() can be used to save a key in the DER format. However, I do not see a corresponding method M2Crypto.RSA.load_key_der() as I would expect.

Is there a way to load a DER-encoded RSA key using M2Crypto?


回答1:


The PEM format is base64-encoded DER data with some additional header and footer lines. You can just read DER as binary, transform it to PEM and pass that 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

Output:

<M2Crypto.RSA.RSA instance at 0x10eb710>


来源:https://stackoverflow.com/questions/5764960/loading-a-der-encoded-rsa-key-using-m2crypto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!