当我尝试使用https://pypi.org/project/dm.xmlsec.binding/加密 SOAP XML 消息的正文时出现错误。
Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_from_command_line(sys.argv)
File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/romeroqj/.virtualenv/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/romeroqj/Desktop/management/commands/encrypt.py", line 63, in handle
encrypted_xml = enc_ctx.encryptXml(enc_data, doc.getroot())
File "_xmlsec.pyx", line 555, in dm.xmlsec.binding._xmlsec.EncCtx.encryptXml (src/_xmlsec.c:7336)
File "_xmlsec.pyx", line 637, in dm.xmlsec.binding._xmlsec.lxml_safe_dealloc (src/_xmlsec.c:8132)
File "src/lxml/public-api.pxi", line 29, in lxml.etree.elementFactory
File "src/lxml/etree.pyx", line 1607, in lxml.etree._elementFactory
File "src/lxml/classlookup.pxi", line 403, in lxml.etree._parser_class_lookup
File "src/lxml/nsclasses.pxi", line 174, in lxml.etree._find_nselement_class
File "src/lxml/classlookup.pxi", line 257, in lxml.etree._callLookupFallback
File "src/lxml/classlookup.pxi", line 336, in lxml.etree._lookupDefaultElementClass
AssertionError: Unknown node type: 3
当我尝试加密正文的内容 (xmlsec.TypeEncContent) 时会发生错误,但是当我尝试加密正文元素 (xmlsec.TypeEncElement) 时它会起作用。这是代码:
import dm.xmlsec.binding as xmlsec
from dm.xmlsec.binding.tmpl import EncData, fromstring
key_mngr = xmlsec.KeysMngr()
key = xmlsec.Key.load('cert.pem', xmlsec.KeyDataFormatCertPem)
key.name = 'cert.pem'
key_mngr.addKey(key)
enc_data = EncData(
xmlsec.TransformAes128Cbc, type=xmlsec.TypeEncContent
)
enc_data.ensureCipherValue() # target for encryption result
enc_ctx = xmlsec.EncCtx(key_mngr)
enc_ctx.encKey = xmlsec.Key.generate(
xmlsec.KeyDataAes, 192, xmlsec.KeyDataTypeSession
)
doc = fromstring(xml_data)
body = doc.find('soap:Body', namespaces=NAMESPACES) # NAMESPACES is redacted for brevity
encrypted_xml = enc_ctx.encryptXml(enc_data, doc.getroot())
XML 数据如下所示:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
...
</soap:Header>
<soap:Body Id="Body">
...
</soap:Body>
</soap:Envelope>
这里有什么问题?
我不确定这是否相关或相关,但我尝试使用不同的库https://github.com/mehcode/python-xmlsec/issues/83做同样的事情,但是我收到了 Segmentation fault 错误,当我尝试加密元素时它起作用了。这种模式让我相信这种方法有问题。
任何提示表示赞赏。提前致谢!