使用M2Crypto从xml数据文件生成通用公钥以进行签名验证 [英] Gen public key from xml data file using M2Crypto for signature verification

查看:163
本文介绍了使用M2Crypto从xml数据文件生成通用公钥以进行签名验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有xml格式的发布密钥:

I have pub key in xml format:

<RSAKeyValue><Modulus>xF9y25EXh8n99sXtU/JAsYTwML6PB7gSCE8tWw8Www2KBfDqohQBL8FMs8jzsDQa7WwoEmiVJ1resEC9YXJGbwQyWgb9qgooC9oSnCB/TkRdBybwby0DKuZOzq+609OBGkwWpgnS4QVCBc6eW+10l3qE3/2hKdcSV+08iRYp7zs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

所以我尝试这样:

from M2Crypto import RSA
from xml.dom.minidom import parseString
import base64

dom = parseString(pubKey)
e = base64.b64decode(dom.getElementsByTagName('Exponent')[0].childNodes[0].data)
n = base64.b64decode(dom.getElementsByTagName('Modulus')[0].childNodes[0].data)
rsa = RSA.new_pub_key((e, n))

出现错误:

    ...
    rsa = RSA.new_pub_key((e, n))
  File "/usr/lib/pymodules/python2.6/M2Crypto/RSA.py", line 390, in new_pub_key
    m2.rsa_set_e(rsa, e)
M2Crypto.RSA.RSAError: invalid length

有什么想法吗?

推荐答案

test_rsa.py ,您会看到评论那说:

The RSA.new_pub_key documentation states that e and n need to be in OpenSSL MPINT format (4-byte big-endian bit-count followed by the appropriate number of bits). It seems like at least your e is not in that format. If you take a look at test_rsa.py, you can see comments that say:

'\000\000\000\003\001\000\001' # aka 65537 aka 0xf4

看来您的e只是'\ 001 \ 000 \ 001'.如果我们在其前面加上'\ 000 \ 000 \ 000 \ 003',则示例应用程序会更进一步,但随后尝试设置n失败.我尚未研究如何创建有效的OpenSSL MPINT值,因此,这并不是您所提问题的完整答案.

It seems your e is just '\001\000\001'. If we prepend the '\000\000\000\003' to it, your sample app gets a bit further along, but then fails trying to set n. I haven't looked into how to create valid OpenSSL MPINT values, so this isn't a complete answer to your question.

这篇关于使用M2Crypto从xml数据文件生成通用公钥以进行签名验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆