使用.cer签名XML

时间:2014-02-21 00:09:26

标签: c# xml

我正在尝试签署XML文档以通过Web服务发送给供应商,但供应商告诉我数据DigestValue和SignatureValue是错误的,他们发送我作为离开这些数据并且不同于我放置我,比较我的xml和他们的是唯一不同的数据。我把我和供应商返回的数据

供应商

<DigestValue>E3wGWNsdBY7ovcY0MRbJu6qPFwo=</DigestValue>SignatureValue>YtBox2b4o+/9nwYr7ULpFJ70gXDP+jSfIjFTAkz85N3ETP1EE+SVCxtlphQl6LfgikG7PsrRo2sW77vL0QSWefNAkDQGuHEd70gyjTMdjVj7URK5u7BnWX4aHoqGYbyQEg2OxhPXXQB6ERJIYckKviuZXitY8Wrb/yQ1/CuL/Ck=</SignatureValue>

我的数据

<DigestValue>iTDhLAQ6s9cdckT206Mtgm0lXgM=</DigestValue>SignatureValue>dkXswnM7hQKwUN+h5WmwReTpFmoWnqViNDMYEu15um6CNeBow3eBasWSEYBbabhZ1nvBAaUmDd5z3Jn931dkdLKzWjX6N75I0sbIF4Fe2cYr/XJi9tojGoCjQL0waI67p006VwPjvq3kiy34cUqVdJmbBdRPXYCD+VHGjTT3hoc=</SignatureValue>

尝试签名的xml:

<Cancelacion Fecha="2014-02-18T11:44:17" RfcEmisor="AAA010101AAA"><Folios><UUID>50bfe0b5-461e-4356-b447-3b4ce02c1377</UUID></Folios></Cancelacion>

这是我用来签署XML的代码

public static void SignXmlFile(string FileName, string SignedFileName, RSA Key, string Certificate)
        {
            // Create a new XML document.
            XmlDocument doc = new XmlDocument();

            // Format the document to ignore white spaces.
            //doc.PreserveWhitespace = false;//false;

            // Load the passed XML file using it's name.
            doc.Load(new XmlTextReader(FileName));

            // Create a SignedXml object.
            SignedXml signedXml = new SignedXml(doc);

            // Create a reference to be signed.
            Reference reference = new Reference();
            reference.Uri = "";

            // Add the key to the SignedXml document. 
            signedXml.SigningKey = Key;
            KeyInfo keyInfo = new KeyInfo();

            // Create a reference to be signed.
            //Reference reference = new Reference();
            //reference.Uri = "";

            // Add an enveloped transformation to the reference.
            XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
            doc.PreserveWhitespace = false;
            //Transform transform = new XmlDsigC14NTransform(false);
            reference.AddTransform(env);
            //reference.AddTransform(transform);

            // Add the reference to the SignedXml object.
            signedXml.AddReference(reference);

            // Create a new KeyInfo object.
           // KeyInfo keyInfo = new KeyInfo();

            // Load the X509 certificate.
            X509Certificate MSCert = X509Certificate.CreateFromCertFile(Certificate);
            //X509Certificate2
            string resultsTrue = string.Empty;
            resultsTrue = MSCert.ToString(true);

            //X509Certificate2 cert = GetCertificateBySubject("C:\\CFDV20\\CERTS\\CICE\\CIC910530TC8_1201051226S.p12");
            X509IssuerSerial xserial;

            xserial.IssuerName = MSCert.Issuer.ToString();
            xserial.SerialNumber = MSCert.GetSerialNumberString();

            // Load the certificate into a KeyInfoX509Data object
            // and add it to the KeyInfo object.
           // keyInfo.AddClause(new KeyInfoX509Data(MSCert));

            KeyInfoX509Data keyData = new KeyInfoX509Data(MSCert);
            X509IssuerSerial x509Serial;
            x509Serial.IssuerName = MSCert.Issuer.ToString();
            x509Serial.SerialNumber = MSCert.GetSerialNumberString();

            //keyData.AddCertificate(MSCert);

            keyData.AddIssuerSerial(x509Serial.IssuerName.ToString(), x509Serial.SerialNumber.ToString());
           // keyData.AddSubjectName(x509Serial.IssuerName);
            //keyData.AddSubjectName(MSCert.Subject.ToString());
            keyInfo.AddClause(keyData);

            //keyData.AddIssuerSerial(x509Serial.IssuerName, x509Serial.SerialNumber);
            //keyInfo.AddClause(keyData);

            // Add the KeyInfo object to the SignedXml object.
            signedXml.KeyInfo = keyInfo;

            // Compute the signature.
            signedXml.ComputeSignature();

            // Get the XML representation of the signature and save
            // it to an XmlElement object.
            XmlElement xmlDigitalSignature = signedXml.GetXml();

            // Append the element to the XML document.
            doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));

            if (doc.FirstChild is XmlDeclaration)
            {
                doc.RemoveChild(doc.FirstChild);
            }
            // Save the signed XML document to a file specified
            // using the passed string.
            XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
            doc.WriteTo(xmltw);
            xmltw.Close();
        }

0 个答案:

没有答案