1

我现在整整一个星期都在为此苦苦挣扎,希望有人可以帮助我。

我需要使用 SHA256 和 xmldsig 对 xml 进行签名。为此,我使用 SignedXML 类。在查看此类时,我看到它使用加载键的 SignatureAlgorithm 值来确定要使用的散列类型。

无论我如何加载密钥(通过证书存储或通过加载证书文件),它都会将 SHA1 显示为 SignatureAlgorithm。当我在 MMC 证书存储中查找我的证书的详细信息时,它显示 SHA256 作为 SignatureAlgorithm。

我尝试了 openssl 和 makecert 来生成一个 SHA256 证书密钥,但是两者都将在 .Net 中加载为 SHA1 所以 signedXml.ComputeSignature(); 将使用 SHA1 作为 SignatureMethod

.Net 4.0 应该支持 SHA256 吧?

4

2 回答 2

1

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. The latter doesn't use the SignatureAlgorithm of the used key to determine what Algorithm to use. Now I can set the Algorithm myself with 'SignedXml.SignedInfo.SignatureMethod' and use a SHA1 key.

于 2013-06-21T12:03:21.617 回答
0

您必须声明一个KeyedHashAlgorithm对象并传递一个对应于 SHA256 的字符串(此处的文档)。

SHA256 对应的字符串是HMACSHA256

Then pass this object to the ComputeSignaturemethod。

代码应如下所示:
KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create("HMACSHA256"); signedXml.ComputeSignature(kha);

于 2015-06-03T12:30:46.843 回答