gpt4 book ai didi

c# - SignedXml 生成无效签名

转载 作者:太空狗 更新时间:2023-10-29 21:07:33 35 4
gpt4 key购买 nike

我一直在努力使 .NET 中的 XMLDSIG 支持正常运行,更具体地说是 SignedXml 类。我正在实现第三方服务,他们最近才开始要求所有消息都必须进行数字签名...

我的问题是,我似乎无法生成有效的签名。第三方服务和我发现的在线签名验证程序都报告签名无效。验证服务 ( http://www.aleksey.com/xmlsec/xmldsig-verifier.html ) 报告摘要和数据不匹配,到目前为止我一直无法弄清楚我做错了什么。

这是相关代码 - 希望有人能够发现我的错误;

public static XDocument SignDocument(XDocument originalDocument, X509Certificate2 certificate)
{
var document = new XmlDocument();
document.LoadXml(originalDocument.ToString(SaveOptions.DisableFormatting));
if (document.DocumentElement == null)
throw new InvalidOperationException("Invalid XML document; no root element found.");

var signedDocument = new SignedXml(document);
Reference signatureReference = GetSignatureReference();
KeyInfo certificateKeyInfo = GetCertificateKeyInfo(certificate);
var dataObject = new DataObject("", "text/xml", "utf-8", document.DocumentElement);

signedDocument.AddReference(signatureReference);
signedDocument.AddObject(dataObject);
signedDocument.SigningKey = certificate.PrivateKey;
signedDocument.KeyInfo = certificateKeyInfo;
signedDocument.ComputeSignature();

return XDocument.Parse(signedDocument.GetXml().OuterXml, LoadOptions.PreserveWhitespace);
}


private static Reference GetSignatureReference()
{
var signatureReference = new Reference("");
signatureReference.AddTransform(new XmlDsigEnvelopedSignatureTransform());

return signatureReference;
}


private static KeyInfo GetCertificateKeyInfo(X509Certificate certificate)
{
var certificateKeyInfo = new KeyInfo();
certificateKeyInfo.AddClause(new KeyInfoX509Data(certificate));

return certificateKeyInfo;
}

最佳答案

如果有人感兴趣,我解决了这个问题并在我的博客上写下了它: http://thomasjo.com/blog/2009/08/04/xmldsig-in-the-net-framework.html

关于c# - SignedXml 生成无效签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1205121/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com