X509Certificate2 私钥

问题描述

我正在尝试使用本地机器证书对信封进行签名,当我发送请求时,“签名无效”被忽略。

我有一个旧代码,它在使用 X509Certificate (Microsoft.Web.Services.Security.X509) 的 .Net Framework 上运行良好

X509Certificate cert = this.GetX509Certificate;
SignedXml signedXml = new SignedXml(envelope);
signedXml.SigningKey = cert.Key; // This is working

enter image description here

现在我需要在 .Net Core 中重写整个内容,并且我使用的是 X509Certificate2 (System.Security.Cryptography.X509Certificates)

cert.Key 不存在。所以我使用以下内容:

X509Certificate2 cert = this.GetX509Certificate;

SignedXml signedXml = new SignedXml(envelope);

Reference bodyReference = new Reference();
bodyReference.Uri = "#Body";
bodyReference.DigestMethod = SignedXml.XmlDsigSHA1Url;

XmlDsigExcC14NTransform trans = new XmlDsigExcC14NTransform();
bodyReference.AddTransform(trans);
signedXml.AddReference(bodyReference);

signedXml.SignedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;
signedXml.SignedInfo.CanonicalizationMethod = "http://www.w3.org/2001/10/xml-exc-c14n#";

signedXml.SigningKey = cert.GetRSAPrivateKey() // This is not working

enter image description here

CspKeyContainerInfo 的差异可能是这里的问题吗?如果是这样,我该如何改变?

我还重新设置了 DigestMethod、SignatureMethod 和 CanonicalizationMethod 的值,以匹配 .Net Framework 中工作代码下的方法。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)