C# XAdES SignedXml.CheckSignature()始终返回false
我使用了MSDN中的以下方法对XML文件进行签名和验证 问题是我无法验证签名的XML文件。SignedXML.CheckSignature()方法始终返回false,甚至不会对出错的内容引发任何异常 用于对XML文件签名的方法C# XAdES SignedXml.CheckSignature()始终返回false,c#,xml,x509certificate,signing,C#,Xml,X509certificate,Signing,我使用了MSDN中的以下方法对XML文件进行签名和验证 问题是我无法验证签名的XML文件。SignedXML.CheckSignature()方法始终返回false,甚至不会对出错的内容引发任何异常 用于对XML文件签名的方法 public static void SignXMLFile(XmlDocument xmlDoc, string XmlSigFileName) { // Create a SignedXml object. SignedXml signedXml =
public static void SignXMLFile(XmlDocument xmlDoc, string XmlSigFileName)
{
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
string certPath = @"path to PFX file.pfx";
X509Certificate2 cert = new X509Certificate2(certPath, "password", X509KeyStorageFlags.Exportable);
var exportedKeyMaterial = cert.PrivateKey.ToXmlString(true);
var Key = new RSACryptoServiceProvider(new CspParameters(24));
Key.PersistKeyInCsp = false;
Key.FromXmlString(exportedKeyMaterial);
// Assign the key to the SignedXml object.
signedXml.SigningKey = Key;
//// Create a reference to be signed.
//Reference reference = new Reference(System.IO.File.Open(@"D:\test.docx",System.IO.FileMode.Open));
//// Add the passed URI to the reference object.
//reference.Uri = URIString;
//// Add the reference to the SignedXml object.
//signedXml.AddReference(reference);
// Create a reference to be signed.
Reference reference = new Reference();
// Add the passed URI to the reference object.
reference.Uri = "";
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
//Save the public key into the KeyValue node of the Signature
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue(Key));
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();
// Save the signed XML document to a file specified
//using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(XmlSigFileName, new UTF8Encoding(false));
xmlDigitalSignature.WriteTo(xmltw);
xmltw.Close();
}
用于验证XML文件签名的方法
// Verify the signature of an XML file and return the result.
public static Boolean VerifyXmlFile(String Name)
{
// Check the arguments.
if (Name == null)
throw new ArgumentNullException("Name");
// Create a new XML document.
XmlDocument xmlDocument = new XmlDocument();
// Format using white spaces.
xmlDocument.PreserveWhitespace = true;
// Load the passed XML file into the document.
xmlDocument.Load(Name);
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml signedXml = new SignedXml(xmlDocument);
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
// Load the signature node.
signedXml.LoadXml((XmlElement)nodeList[0]);
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigBase64TransformUrl;
X509Certificate2 cert = new X509Certificate2(@"path to PFX file.pfx", "password");
// Check the signature and return the result.
return signedXml.CheckSignature(cert, true);
}
我尝试了stackoverflow的所有建议,但没有成功。非常感谢您的帮助。谢谢。根据评论中的数据,您的问题是您试图使用外部(分离的)签名,尽管您显示的代码使用了表示“整个文档”的Uri
“
”
在游戏中,我们会得到一些宝石:
还有第四种签名称为外部分离签名,即数据和签名位于单独的XML文档中。SignedXml类不支持外部分离签名
元素的URI属性
- 将URI属性设置为空字符串表示文档的根元素正在签名,这是一种封装签名形式
- 其他任何内容都被视为外部资源分离签名,SignedXml类不支持
所以,如果你需要这样做。。。小心。您可以尝试启用SignedXml日志()。或者,如果您可以给出一个您签署的示例文档,但该文档没有报告为有效文档,这将非常有用。谢谢@bartonjs,我会尝试一下,并让您知道。另外,我想知道SignedXml类是否支持外部分离签名。我发现一篇文章[提到SignedXml类中不支持外部分离签名。我是否可以在C#中使用外部分离签名?我已启用SignedXml日志,并在日志中捕获事件。但是,日志中没有抛出错误。在验证签名时,我遇到了“无法解析URI”错误。我可以从日志文件中获得的最后一行是“System.Security.Cryptography.Xml.SignedXml Verbose:13:[SignedXml#01f93f3e,VerifyReference]处理引用引用#00eb2115,Uri“test.Xml”,Id“Type”一周以来我一直在寻找解决方案。非常感谢您的帮助。除非您编辑注册表,否则SignedXml不支持分离签名。没有文档、稳定的复制或日志,任何人都无法提供帮助。@bartonjs根据您的建议,我编辑了注册表,并出现错误“无法解析Uri”未抛出,签名已成功验证。非常感谢您的回复。非常感谢。