Question

I have problem with XmlDsigC14NTransform. I trying to repeat example from http://www.di-mgt.com.au/xmldsig2.html (part Compose the canonicalized SignedInfo element and compute its SignatureValue) but my code loses whitespaces from xml and i cant get correct hexdump.

My C# code:

 XmlDocument signedInfoXml = new XmlDocument();
 signedInfoXml.Load(@"C:\temp\new_sign.txt");

 XmlDsigC14NTransform xmlTransform = new XmlDsigC14NTransform();
 xmlTransform.LoadInput(signedInfoXml);
 MemoryStream memoryStream = (MemoryStream)xmlTransform.GetOutput();
 return  BitConverter.ToString(memoryStream.ToArray()).Replace("-"," ");

Source Xml(from file C:\temp\new_sign.txt):

<SignedInfo  xmlns="http://www.w3.org/2000/09/xmldsig#">
      <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" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>UWuYTYug10J1k5hKfonxthgrAR8=</DigestValue>
      </Reference>
    </SignedInfo>

How i can save whitespaces into my xml and get canonicalized xml like in sample (http://www.di-mgt.com.au/xmldsig2.html)?

Was it helpful?

Solution

You can set a flag on the XMLDocument:

    // Create a new XML document.
    XmlDocument xmlDocument = new XmlDocument();

    // Format using white spaces.
    xmlDocument.PreserveWhitespace = true;

    // Load the XML file into the document. 
    xmlDocument.Load("file.xml");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top