简体   繁体   English

.Net 4.0 C#加载SHA256密钥时SignatureAlgorithm更改为SHA1

[英].Net 4.0 C# When loading SHA256 key SignatureAlgorithm changes to SHA1

I'm struggling with this now a whole week and hope someone could help me out. 我整整一个星期都在为此苦苦挣扎,希望有人能帮助我。

I need to sign xml using SHA256 and xmldsig. 我需要使用SHA256和xmldsig签署xml。 For this I use the SignedXML Class. 为此,我使用了SignedXML类。 When looking into this class I see it uses the SignatureAlgorithm value of the loaded key to determine what Hashing type to use. 查看此类时,我看到它使用已加载密钥的SignatureAlgorithm值来确定要使用的哈希类型。

No matter how I load the key(via cert store of via loading cert file) it will show SHA1 as SignatureAlgorithm. 无论我如何加载密钥(通过加载证书文件的证书存储),SHA1都将显示为SignatureAlgorithm。 When I look up the details of my Certificate in the MMC cert store, it shows SHA256 as SignatureAlgorithm. 当我在MMC证书存储区中查找证书的详细信息时,它将SHA256显示为SignatureAlgorithm。

I tried both openssl and makecert to generate a SHA256 cert key, but both will load as SHA1 in .Net And so signedXml.ComputeSignature(); 我尝试了openssl和makecert来生成SHA256证书密钥,但是两者都将在.Net中作为SHA1加载,因此,signedXml.ComputeSignature(); will use SHA1 as a SignatureMethod 将使用SHA1作为SignatureMethod

.Net 4.0 should support SHA256 right? .Net 4.0应该支持SHA256对吗?

Found out that I was probably using the wrong class. 发现我可能使用了错误的类。

Instead of Microsoft.Web.Services.Security.SignedXml of should use System.Security.Cryptography.Xml.SignedXml. 而不是Microsoft.Web.Services.Security.SignedXml应该使用System.Security.Cryptography.Xml.SignedXml。 The latter doesn't use the SignatureAlgorithm of the used key to determine what Algorithm to use. 后者不使用所用密钥的SignatureAlgorithm来确定要使用的算法。 Now I can set the Algorithm myself with 'SignedXml.SignedInfo.SignatureMethod' and use a SHA1 key. 现在,我可以使用'SignedXml.SignedInfo.SignatureMethod'自己设置算法,并使用SHA1键。

You have to declare a KeyedHashAlgorithm object and pass a string corresponding to SHA256 (documentation here ). 您必须声明一个KeyedHashAlgorithm对象,并传递一个与SHA256相对应的字符串( 此处的文档)。

The corresponding string for SHA256 is HMACSHA256 SHA256的对应字符串是HMACSHA256

Then pass this object to the ComputeSignature method. 然后将此对象传递给ComputeSignature方法。

The code should be as follows: 代码应如下所示:
KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create("HMACSHA256"); signedXml.ComputeSignature(kha);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM