SHA256签名在.NET 4.5中停止运行

Thu*_*uan 17 .net signing certificate sha256 wif

我们有一段代码创建了一个SigningCredentials对象,用于使用SHA256算法对xml文档进行签名.它完美适用于.NET 3.5.但是,当我们将代码库升级到.NET 4.5时,它会停止工作.相同的代码,相同的证书!我花了好几个小时在互联网上调试和搜索,没有任何运气.

谁能告诉我这里的问题是什么?先感谢您.

创建SigningCredentials的代码:

public SigningCredentials CreateSigningCredentials(X509Certificate2 cert)
{
    var ski = new SecurityKeyIdentifier(new X509RawDataKeyIdentifierClause(cert));
    return new SigningCredentials(new X509AsymmetricSecurityKey(cert), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "http://www.w3.org/2001/04/xmlenc#sha256", ski);
}
Run Code Online (Sandbox Code Playgroud)

例外:

[CryptographicException: Invalid algorithm specified.
]
   System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +41
   System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 cbHash, ObjectHandleOnStack retSignature) +0
   System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash) +118
   System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash) +334
   System.Security.Cryptography.RSAPKCS1SignatureFormatter.CreateSignature(Byte[] rgbHash) +321
   System.IdentityModel.SignedXml.ComputeSignature(HashAlgorithm hash, AsymmetricSignatureFormatter formatter, String signatureMethod) +323
   System.IdentityModel.SignedXml.ComputeSignature(SecurityKey signingKey) +690
   System.IdentityModel.EnvelopedSignatureWriter.ComputeSignature() +338
   System.IdentityModel.EnvelopedSignatureWriter.OnEndRootElement() +278
   System.IdentityModel.Metadata.MetadataSerializer.WriteEntityDescriptor(XmlWriter inputWriter, EntityDescriptor entityDescriptor) +1109
Run Code Online (Sandbox Code Playgroud)

tyg*_*ger 15

与XmlDsig有同样的问题(试图用RSA-SHA256算法制作包含xml文档的签名).首先我得到了例外

System.Security.Cryptography.CryptographicException:无法为提供的签名算法创建SignatureDescription.

然后我发现提到RSAPKCS1SHA256SignatureDescription了RSA-SHA256签名的签名描述实现.
在此全面实施:
http://clrsecurity.codeplex.com/SourceControl/changeset/view/47833#269110
或此处:https://gist.github.com/sneal/f35de432115b840c4c1f

您必须为每个appdomain手动调用一次:

CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
Run Code Online (Sandbox Code Playgroud)

之后我得到了一个新例外:

System.Security.Cryptography.CryptographicException:指定的算法无效.

这让我想到了你的问题.阅读建议文章后,我使用了一个新密钥和证书(使用OpenSSL)Microsoft Enhanced RSA and AES Cryptographic Provider.
令我惊讶的是,这个新证书让我成功签名.

经过一些调查后,我在安德鲁这里找到了有趣的答案/sf/answers/1210004211/,他曾经在那里RSACryptoServiceProviderSignedXml课堂准备SecretKey .特别是这部分(我的解释):

var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var certificates =  store.Certificates.Find(X509FindType.FindBySerialNumber,  "54dba096", true);
var certificate = certificates[0];

// next three lines
var cspParams = new CspParameters(24) { KeyContainerName = "XML_DSIG_RSA_KEY" };
var key = new RSACryptoServiceProvider(cspParams);
key.FromXmlString(certificate.PrivateKey.ToXmlString(true));

SignedXml sxml = new SignedXml(doc);
sxml.SigningKey = key;
Run Code Online (Sandbox Code Playgroud)

这个解决方案即使使用旧密钥也能正常工作!


Thu*_*uan 3

虽然这个问题是在大约一年前提出的,但最近收到了一些赞成票,这可能表明其他一些人也遇到了同样的问题。希望这个答案可以有所帮助:) 简而言之,该错误不会发生在所有机器上,而只会发生在其中一些机器上。我想这取决于特定机器上注册的 CSP。无论如何,在我的具体情况下,证书是使用“Microsoft RSA SChannel...”或“Microsoft 强大的加密提供程序”作为 CSP 生成的。我生成了一个新证书,但使用“Microsoft 增强型 RSA 和 AES 加密提供程序”作为 CSP,并且它的 SHA256 签名开始为我工作。

一些参考:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/e391ba75-ce6e-431c-bfc9-26a71ae1b033/sha256-signing-stops-working-in-net-45?forum=Geneva (正如你可以看到,万分感谢保罗帮助我解决了这个问题)

http://hintdesk.com/c-how-to-fix-invalid-algorithm-specified-when-signing-with-sha256/