您能告诉我如何在C#中签署一个xml元素。
使用System.Security.Cryptography;
使用System.Security.Cryptography.Xml;
使用System.Security.Cryptography.X509Certificates;
示例:
我有这个xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Signature Id="SignatureIdValue" 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="#idPackageObject" Type="http://www.w3.org/2000/09/xmldsig#Object">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>3H+EGzfJMnudlkWAtFYTfJkaeZM=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>h7ApS9H4NagiJIvt9xUy9FijPVpSQQQtUtvn/hU/WuSPPqap4r3NK98K+qTKptCPTgXcY3P3o+l+vrEXnl71gttfvK3nQabNtPlaXd5KR7fLAJq+6xJNzznLFu7d4JmXDYN3xfq7Scr+vlWcaU5zIGBBbIg90w3AXe1GsYRCpME=</SignatureValue>
<Object Id="idPackageObject">
<Manifest>
<Reference URI="/finder.xml?ContentType=vnd-sizr-datacollection/finder">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>pQAvJzZlmBqHmPU46dj4rYQqjPM=</DigestValue>
</Reference>
<Reference URI="/_rels/finder.xml.rels?ContentType=application/vnd.openxmlformats-package.relationships+xml">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>Qcp4TAsGEpSIhnVDCYCKih3t+tg=</DigestValue>
</Reference>
<Reference URI="/content.xml?ContentType=vnd-sizr-datacollection/content">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>i8TcHWdSKqLEpMevvhRztwrFCO4=</DigestValue>
</Reference>
<Reference URI="/systemcheck.xml?ContentType=vnd-sizr-datacollection/systemcheck">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>NB1XkMlRU83JUjZqdZLJ0925T54=</DigestValue>
</Reference>
<Reference URI="tree/service.xml?ContentType=vnd-sizr-datacollection/service">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>4FgBGSm/TosmN5bngmTKapOHMSc=</DigestValue>
</Reference>
</Manifest>
<SignatureProperties>
<SignatureProperty Id="idSignatureTime" Target="#SignatureIdValue">
<SignatureTime xmlns="http://schemas.openxmlformats.org/package/2006/digital-signature">
<Format>YYYY-MM-DDThh:mm:ss.sTZD</Format>
<Value>2018-03-25T01:07:44.0+00:00</Value>
</SignatureTime>
</SignatureProperty>
</SignatureProperties>
</Object>
</Signature>
我知道如何在<DigestValue>
中生成<Manifest>
,使用以下代码:
private static void SignObject(ref XmlDocument xmlDoc)
{
// Generate a signing key.
RSACryptoServiceProvider Key = new RSACryptoServiceProvider();
// Create a SignedXml object.
SignedXml signedXml = new SignedXml();
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
try
{
// Create a new KeyInfo object.
KeyInfo keyInfo = new KeyInfo();
// Load the X509 certificate.
X509Certificate MSCert =
X509Certificate.CreateFromCertFile(Certificate);
// Load the certificate into a KeyInfoX509Data object
// and add it to the KeyInfo object.
keyInfo.AddClause(new KeyInfoX509Data(MSCert));
// Add the KeyInfo object to the SignedXml object.
signedXml.KeyInfo = keyInfo;
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Unable to locate the following file: " +
Certificate);
}
// Compute the signature.
signedXml.ComputeSignature();
// Add the signature branch to the original tree so it is enveloped.
xmlDoc.DocumentElement.AppendChild(signedXml.GetXml());
}
但我不知道如何使用<DigestValue>
生成<Reference URI="#idPackageObject"...>
请帮帮我。