Pythondm.XML秒“签名验证失败”

2024-05-16 03:09:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用公钥验证Windows Mobile InApp收据
我有从.pem文件中获得的证书(.cer),并从中获得公钥

以下是我的代码:

import dm.xmlsec.binding as xmlsec
xmlsec.initialize()
from os.path import dirname, basename
from lxml import etree
from lxml.etree import tostring
def verify_file(xml_file, key_file):
     """verify signature in *xml_file* with key in *key_file*.

     *xml_file* contains the signed XML document.
     It can be a file, a filename string or an HTTP/FTP url.

     *key_file* contains the PEM public key. It must be a filename.

     An exception is raised when the verification fails.
     """
     from lxml.etree import parse
     doc = etree.parse(xml_file)
     node = doc.find(".//{%s}Signature" % xmlsec.DSigNs)
     dsigCtx = xmlsec.DSigCtx()
     # Note: we do not provide read access to `dsigCtx.signKey`.
     #  Therefore, unlike the `xmlsec` example, we must set the key name
     #  before we assign it to `dsigCtx`

     signKey = xmlsec.Key.load(key_file, xmlsec.KeyDataFormatPem, None)
     signKey.name = basename(key_file)
     # Note: the assignment below effectively copies the key
     dsigCtx.signKey = signKey
     dsigCtx.verify(node)

from StringIO import StringIO
if __name__ =="__main__" :
     verify_file('path/of/ReceiptSHA256.xml', 'path/of/pubkey.pem')



但问题是每次运行时都会出错dsigCtx.verify(node)

^{pr2}$

当我调试它的时候,我发现dsigCtx.verify(node)给了我错误
tuple: ('verifying failed with return value', -1)

我有有效的证书,.pem文件,公钥和收据,但它仍然给我错误

请在这方面帮助我


Tags: thepathkeyfromimportnodexmlpem