对于相当长的代码块,我提前道歉,但这是我可以生成的最小的可编译示例。我已经从原始代码中省略了所有错误检查。我正在使用 Visual Studio 2012 和 .NET 4.5,虽然这对 4.5 来说并不是什么新鲜事,但它应该适用于任何版本。
我正在尝试签署 XML 文档的元素以保护它们不被篡改。我不想保护整个文档,而只想保护某些元素。甚至可能具有不同键的不同元素。
但是,当我签署三个示例元素并尝试验证它们时,第一个总是验证,另外两个失败。更糟糕的是,如果我在签名后修改它,第一个甚至会成功。我搜索了很多,阅读了很多教程,甚至在这里问了一个理论问题,但我不知道我做错了什么。有人能看出我的错误吗?
注意:我很乐意为解决此问题的任何人提供与周五问题相同的赏金。
证书是通过执行创建的:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin\makecert" -r -pe -n "CN=XMLDSIG_Test" -b 01/01/2013 -e 01/01/2014 -sky签名 -ss 我的
测试 xml 文件是:
<?xml version="1.0" encoding="utf-8" ?>
<PackageRoot>
<Package>
<Changes >
<Change/>
</Changes>
</Package>
<Package>
<Changes>
<Change/>
<Change/>
</Changes>
</Package>
<Package>
<Changes>
<Change/>
<Change/>
<Change/>
</Changes>
</Package>
</PackageRoot>
签名和验证的代码:
namespace SOExample
{
using System;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Xml;
public static class Program
{
public static void Sign(this XmlElement element, X509Certificate2 certificate)
{
var identifier = Guid.NewGuid().ToString();
element.SetAttribute("Id", identifier);
var signedXml = new SignedXml(element) { SigningKey = certificate.PrivateKey };
var reference = new Reference("#" + identifier);
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
signedXml.AddReference(reference);
signedXml.ComputeSignature();
var xmlDigitalSignature = signedXml.GetXml();
element.AppendChild(element.OwnerDocument.ImportNode(xmlDigitalSignature, true));
}
public static bool VerifySignature(this XmlElement element, X509Certificate2 certificate)
{
var signedXml = new SignedXml(element);
XmlNodeList nodeList = element.GetElementsByTagName("Signature");
if (nodeList.Count != 1) return false;
signedXml.LoadXml((XmlElement)nodeList[0]);
return signedXml.CheckSignature(certificate, true);
}
public static void Main()
{
var xmlDoc = new XmlDocument { PreserveWhitespace = true };
xmlDoc.Load("ExamplePackage.xml");
var certificate = GetCertificateBySubject("CN=XMLDSIG_Test");
foreach (XmlElement root in xmlDoc.GetElementsByTagName("PackageRoot"))
{
foreach (XmlElement package in root.GetElementsByTagName("Package"))
{
package.Sign(certificate);
}
}
xmlDoc.Save("test_signed.xml");
Console.WriteLine("XML file signed.");
Console.WriteLine("Press any key to verify");
Console.ReadLine();
var signedDoc = new XmlDocument();
signedDoc.Load("test_signed.xml");
foreach (XmlElement root in xmlDoc.GetElementsByTagName("PackageRoot"))
{
foreach (XmlElement package in root.GetElementsByTagName("Package"))
{
Console.Write("Verifying Package " + package.GetAttribute("Id"));
var success = package.VerifySignature(certificate);
Console.WriteLine(success ? " successful!" : " failed!");
}
}
Console.WriteLine("Done.");
Console.ReadLine();
}
private static X509Certificate2 GetCertificateBySubject(string certificateSubject)
{
var store = new X509Store("My", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 c in store.Certificates)
{
if (c.Subject == certificateSubject)
{
store.Close();
return c;
}
}
store.Close();
return null;
}
}
}