gpt4 book ai didi

c# - 如何使用 RsaCng 作为 SignedXml 的 SigningKey?

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:39 25 4
gpt4 key购买 nike

我正在尝试遵循以下帖子中的解决方案,以便能够使用 CNG 私钥作为我的 SignedXml 的签名 key 。

"Invalid provider type specified" CryptographicException when trying to load private key of certificate

enter image description here

CustomSignedXml 派生自 SignedXml。但是,当我这样做并调用 ComputeSignature 方法时,出现异常

方法不受支持。在 System.Security.Cryptography.RSA.DecryptValue(Byte[] rgb) 在 System.Security.Cryptography.Xml.SignedXml.ComputeSignature()

由于堆栈跟踪表明 Method is not supported 是从 DecryptValue 中抛出的,我认为这是 RSA 基类中已弃用的方法。我无法覆盖它以使用 RSACng.Decrypt,因为 RSACng 是一个密封类。

https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa.decryptvalue(v=vs.110).aspx

我在这里错过了什么?

最佳答案

在 CLR 安全问题页面中,SignedXml 仍然缺少对 RSACng 的支持。但是,评论已将我引导至 4.6.2 预览版。您可以使用 GetRSAPrivateKey() 获取 key (我认为它在 4.6.1 中已经可用)但是这次我没有收到“不支持的方法”异常。所以最好的解决办法是将你的框架更新到 4.6.2,下面的代码就可以工作了。

https://clrsecurity.codeplex.com/workitem/11073

private CustomSignedXml CreateSignedXml()
{
var signedXml = new CustomSignedXml(_documentToSign)
{
SigningKey = _signingCertificate.GetRSAPrivateKey(),
KeyInfo = CreateKeyInfo()
};

Reference reference = new Reference("");
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
reference.DigestMethod = SecurityAlgorithms.Sha256Digest; // SignedXml.XmlDsigSHA256Url;


signedXml.SignedInfo.CanonicalizationMethod = SecurityAlgorithms.ExclusiveC14n;
signedXml.SignedInfo.SignatureMethod = SecurityAlgorithms.RsaSha256Signature; // SignedXml.XmlDsigRSASHA256Url
signedXml.AddReference(reference);

return signedXml;
}

关于c# - 如何使用 RsaCng 作为 SignedXml 的 SigningKey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686950/

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