2017-04-17 4 views
0

Python의 서명 된 XML에서 Signature, SignatureValue 및 SignedInfo를 ElementTree와 함께 읽으려고하지만 None으로 읽습니다. 다른 xml 속성은 잘 읽혀집니다. Signature, SignatureValue 및 SignedInfo는 어떻게 읽을 수 있습니까?Python elementtree find 함수가 서명을 비어있는 것으로 읽습니다. (없음)

xml_file = open(settings.STATIC_ROOT + '/file/test.xml', 'rt').read() 
response_xml = xml_et.fromstring(xml_file.encode('utf-8')) 
print response_xml.find('Signature') # prints None 
print response_xml.find('SignatureValue') # prints None 
print response_xml.find('SignedInfo') # prints None 
print response_xml.find('OrderID').text # works fine 

여기 내 테스트 XML의 :

여기 내 코드의

<?xml version="1.0" encoding="UTF-8"?> 
<Message date="08/09/2016 06:47:20"> 
    <Version>1.0</Version> 
    <OrderID>ABCD:123456</OrderID> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
     <SignedInfo> 
     <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
     <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> 
     <Reference URI=""> 
      <Transforms> 
       <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
       <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
      </Transforms> 
      <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
      <DigestValue>blabla=</DigestValue> 
     </Reference> 
     </SignedInfo> 
     <SignatureValue>blabla==</SignatureValue> 
     <KeyInfo> 
     <KeyName>Public key of certificate</KeyName> 
     <KeyValue> 
      <RSAKeyValue> 
       <Modulus>blabla==</Modulus> 
       <Exponent>AQAB</Exponent> 
      </RSAKeyValue> 
     </KeyValue> 
     <X509Data> 
      <X509Certificate>blabla</X509Certificate> 
     </X509Data> 
     </KeyInfo> 
    </Signature> 
</Message> 

답변

1

그것 당신이 서명 요소의 네임 스페이스를 가지고 있기 때문에, 무엇 당신이 할 수있는 것은 네임 스페이스를 가진 요소를 찾을 수있다

print response_xml.find('{http://www.w3.org/2000/09/xmldsig#}Signature') 

그러면 elem 엔트 및

뭔가 이런 식으로 모든 하위 요소를 찾을 수 :

namespace = "{http://www.w3.org/2000/09/xmldsig#}" 
signature_elem = response_xml.find(namespace +'Signature') 
print signature_elem 
print signature_elem.find(namespace+'SignatureValue') 
print signature_elem.find(namespace+'SignedInfo')