如何将已签名的Xml文档导入到另一个Xml文档中

本文关键字:Xml 文档 导入 另一个 | 更新日期: 2023-09-27 18:06:06

我正在尝试将签名的xml文档导入另一个xml文档。当我导入文档,并导出它时没有更改,我不能再验证它。

甚至下面的代码也会破坏验证。

OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
if (ofd.FileName != "")
{
   XmlDocument XmlDocument = new XmlDocument();
   XmlDocument.Load(ofd.FileName);
   FolderBrowserDialog fbd = new FolderBrowserDialog();
   fbd.ShowDialog();
   if (fbd.SelectedPath != "")
   {
      XmlDocument.Save(fbd.SelectedPath + @"'Doc.xml");
   }
}

如何导入/导出xml文档而不破坏签名验证?

我的Xml文件是:

  • 信封:dl.omerharmansa.com/envelope.xml

  • 发票:dl.omerharmansa.com/invoice.xml

invoice.xml是签名的文档。我想把invoice.xml导入envelope.xml。下面显示了invoice.xml将要导入的envelope.xml元素。

如何将已签名的Xml文档导入到另一个Xml文档中

给定以下带符号的XML:

<?xml version="1.0"?>
<MySignedXMLRoot xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <MySignedXMLNode>TextToBeSigned</MySignedXMLNode>
  <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" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>3tAjL2X1SEMhxQ1Hp9X4HBUtsgQ=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>UYyELdlqq1InZSwSKozhIuATU52gdVFw0AqnZxOB0xQR6CS2hbW5tZIXc6fMPSYstyfMVULO1ZioRkHvyIY7LqeR/i4cYZvA1VpzTnx+0gZEcPFEuMORNgJ0v/W7NHi5xJb6uxkdZBcSMleFWitTHO+tPh8tha0cNdp4XO8Xx4Y=</SignatureValue>
  </Signature>
</MySignedXMLRoot>

如果您想将其包含在以下XML文档中:

<MyXML>
    <SomeData>Random Data</SomeData>
    <Container/>
</MyXML>

必须正确设置签名的Reference属性:

<MyXML>
    <SomeData>Random Data</SomeData>
    <Container>
        <MySignedXMLRoot id="ElementToSign" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <MySignedXMLNode>TextToBeSigned</MySignedXMLNode>
            <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="#ElementToSign">
                        <Transforms>
                            <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                        </Transforms>
                        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                        <DigestValue>3tAjL2X1SEMhxQ1Hp9X4HBUtsgQ=</DigestValue>
                    </Reference>
                </SignedInfo>
                <SignatureValue>UYyELdlqq1InZSwSKozhIuATU52gdVFw0AqnZxOB0xQR6CS2hbW5tZIXc6fMPSYstyfMVULO1ZioRkHvyIY7LqeR/i4cYZvA1VpzTnx+0gZEcPFEuMORNgJ0v/W7NHi5xJb6uxkdZBcSMleFWitTHO+tPh8tha0cNdp4XO8Xx4Y=</SignatureValue>
            </Signature>
        </MySignedXMLRoot>
    </Container>
</MyXML>

可以用Reference设置。Uri