我正在尝试签署一个XML文件,但不是整个文件,只是所有内部标记" soapenv:Body"。 他们给了我一个例子,我试图给出相同的结果,但是开始我不能得到相同的DigestValue。 我得到的那个不会改变,无论输入是什么,我想如果我改变了标签内的值,我每次都会得到一个不同的DigestValue,但我不是。 我需要知道我做错了什么。 代码如下:
doc.Load(xmlfileInput);
XmlNodeList nodo = doc.DocumentElement.GetElementsByTagName("soapenv:Body");
string nodoxml = nodo.Item(0).InnerXml;
XmlDocument doc1 = new XmlDocument();
//doc.PreserveWhitespace = true;
doc1.LoadXml(nodoxml);
//Print content to sign
System.Console.WriteLine(doc1.OuterXml);
System.Console.WriteLine("---------------------------------------------------------------------------------------------");
SignedXml signer = new SignedXml(doc1);
byte[] pkBytes = File.ReadAllBytes(privateKey); //testkey.key
RsaPrivateCrtKeyParameters pkFactory = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.DecryptKey(passwordKey.ToCharArray(), pkBytes);
RSA key = DotNetUtilities.ToRSA(pkFactory);
signer.KeyInfo = new KeyInfo();
signer.KeyInfo.AddClause(new RSAKeyValue(key));
signer.SigningKey = key;
signer.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationWithCommentsUrl;
Reference r = new Reference("");
XmlDsigXPathTransform XPathTransform = CreateXPathTransform(@"/dodas");
r.AddTransform(XPathTransform);
r.AddTransform(new XmlDsigEnvelopedSignatureTransform());
r.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
signer.AddReference(r);
signer.ComputeSignature();
System.Console.WriteLine(signer.GetXml().OuterXml);
XmlNode nodeimported = doc.ImportNode(signer.GetXml().FirstChild.ParentNode, true);
doc.DocumentElement.GetElementsByTagName("dodas").Item(0).AppendChild(nodeimported);
string xmlFirmado = doc.OuterXml;
File.WriteAllText(xmlfileOutput, xmlFirmado);
//Éxito
System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.WriteLine("Documento XML firmado con éxito!");
两个不同的跑步在控制台中显示以下内容:
第一个输出:
<mat:altaDoda xmlns:mat="http://impl.service.qrws.ce.siat.sat.gob.mx/siatbus/matce"><dodas><credenciales><!--Optional:--><ciec>
<!--Optional:--><Usuario>TEST1TEST1</Usuario>
<!--Optional:--><Contrasena>VMH94111</Contrasena></ciec>
<!--Optional:--><sellado><!--Optional:--><cadenaOriginalAA>kldffIL</cadenaOriginalAA><!--Optional:--><certificado64>kk9rtreljd</certificado64><!--Optional:--><firmado>||jkjkkkk|trtvl76767|jnkj3444||</firmado><!--Optional:--><serie>1234567890</serie></sellado></credenciales><!--1 or more repetitions:--><doda><!--Optional:--><datosGenerales><!--Optional:--><aduana>170</aduana><!--Optional:--><seccion>170</seccion><!--Optional:--><caat>123</caat><!--Optional:--><contenedores><!--Zero or more repetitions:--><contenedor><!--Zero or more repetitions:--><candados><!--Optional:--><valorCandado>valCandado01</valorCandado></candados><!--Optional:--><valorContenedor>valContenedor01</valorContenedor></contenedor></contenedores><!--Optional:--><idTransporte>890zxc</idTransporte><!--Optional:--><fastId /><!--Zero or more repetitions:--><tipoOperacion>1</tipoOperacion></datosGenerales><!--Optional:--><pedimentos><!--1 or more repetitions:--><pedimento><!--Optional:--><patenteAutorizacion>3904</patenteAutorizacion><!--Optional:--><documento>7100164</documento><!--Optional:--><numeroRemesa>2</numeroRemesa><!--Optional:--><dtaNiu>abc</dtaNiu><!--Optional:--><importeDifDolares>abc</importeDifDolares><!--Optional:--><importeEfectivoDolares>abc123</importeEfectivoDolares><!--Optional:--><umc>abc</umc><!--Optional:--><articulo7>0</articulo7><!--Optional:--><cove>12345</cove></pedimento></pedimentos></doda></dodas></mat:altaDoda>
---------------------------------------------------------------------------------------------
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://ww
w.w3.org/TR/1999/REC-xpath-19991116"><XPath>/dodas</XPath></Transform><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><DigestValue>47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=</DigestValue
></Reference></SignedInfo><SignatureValue>PLeNmY5ryu348507jd1P8DXMAEXQWg5ProRS9SU7tPDTVGjn33u4qwtU0HW1GJVwd3NpiGvjJzOonVR7gmoV8FbZtWG0IpLL8jy7PRAppZEupAAmGlFnLR10MPHKlvnsZIsiVstGn+TBKzx/EqNxztrfNJUUH8Huc65YPlq3TEA=</SignatureValue><KeyInfo><KeyValue><RSAKeyValue><Modulus>yapCI4cdFFHP+pJ8LtCH8TxWpaPm
NwhFMy2/K6HmOEbyGYD+J2F3YcMaDCUnaG22t3V90p4bjEDQvhu+QLNL7JOQFs8pEOfRWL0OP0YglwZ0UnuK9umGV4hGxlpuj9q/JT1hJMhYJgIXwjNd1vw0mDeXmrJzBQQNX0ShZd8T4AM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo></Signature>
Documento XML firmado con éxito!
第二个输出是:
<mat:altaDoda xmlns:mat="http://impl.service.qrws.ce.siat.sat.gob.mx/siatbus/matce"><dodas><credenciales><!--Optional:--><ciec>
<!--Optional:--><Usuario>ANOTHERTEST2TEST2</Usuario>
<!--Optional:--><Contrasena>VMH94111</Contrasena></ciec>
<!--Optional:--><sellado><!--Optional:--><cadenaOriginalAA>kldffI
L</cadenaOriginalAA><!--Optional:--><certificado64>kk9rtreljd</certificado64><!--Optional:--><firmado>||jkjkkkk|trtvl76767|jnkj3444||</firmado><!--Optional:--><serie>1234567890</serie></sellado></credenciales><!--1 or more repetitions:--><doda><!--Optional:--><datosGenerales><!--Optional:--><aduana>
170</aduana><!--Optional:--><seccion>170</seccion><!--Optional:--><caat>123</caat><!--Optional:--><contenedores><!--Zero or more repetitions:--><contenedor><!--Zero or more repetitions:--><candados><!--Optional:--><valorCandado>valCandado01</valorCandado></candados><!--Optional:--><valorContenedor>v
alContenedor01</valorContenedor></contenedor></contenedores><!--Optional:--><idTransporte>890zxc</idTransporte><!--Optional:--><fastId /><!--Zero or more repetitions:--><tipoOperacion>1</tipoOperacion></datosGenerales><!--Optional:--><pedimentos><!--1 or more repetitions:--><pedimento><!--Optional:-
-><patenteAutorizacion>3904</patenteAutorizacion><!--Optional:--><documento>7100164</documento><!--Optional:--><numeroRemesa>2</numeroRemesa><!--Optional:--><dtaNiu>abc</dtaNiu><!--Optional:--><importeDifDolares>abc</importeDifDolares><!--Optional:--><importeEfectivoDolares>abc123</importeEfectivoDo
lares><!--Optional:--><umc>abc</umc><!--Optional:--><articulo7>0</articulo7><!--Optional:--><cove>12345</cove></pedimento></pedimentos></doda></dodas></mat:altaDoda>
---------------------------------------------------------------------------------------------
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /><Reference URI=""><Transforms><Transform Algorithm="http://ww
w.w3.org/TR/1999/REC-xpath-19991116"><XPath>/dodas</XPath></Transform><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><DigestValue>47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=</DigestValue
></Reference></SignedInfo><SignatureValue>PLeNmY5ryu348507jd1P8DXMAEXQWg5ProRS9SU7tPDTVGjn33u4qwtU0HW1GJVwd3NpiGvjJzOonVR7gmoV8FbZtWG0IpLL8jy7PRAppZEupAAmGlFnLR10MPHKlvnsZIsiVstGn+TBKzx/EqNxztrfNJUUH8Huc65YPlq3TEA=</SignatureValue><KeyInfo><KeyValue><RSAKeyValue><Modulus>yapCI4cdFFHP+pJ8LtCH8TxWpaPm
NwhFMy2/K6HmOEbyGYD+J2F3YcMaDCUnaG22t3V90p4bjEDQvhu+QLNL7JOQFs8pEOfRWL0OP0YglwZ0UnuK9umGV4hGxlpuj9q/JT1hJMhYJgIXwjNd1vw0mDeXmrJzBQQNX0ShZd8T4AM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo></Signature>
Documento XML firmado con éxito!
第一个输入具有标记值
<Usuario>TEST1TEST1</Usuario>
第二个输入具有标记值
<Usuario>ANOTHERTEST2TEST2</Usuario>
但具有相同的DigestValue
<DigestValue>47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=</DigestValue>