RSASSA-PSS无参数使用SHA-256 . net 4.5支持
本文关键字:net 5支持 SHA-256 参数 RSASSA-PSS | 更新日期: 2023-09-27 17:53:23
我试图使用System.Security.Cryptography(目标框架。net 4.5)来创建xml数字签名,到目前为止,我设法使用以下方案创建和验证签名:RSA pkcs# 1 v1.5和SHA-256: http://www.w3.org/2001/04/xmldsig-more#rsa-sha256
但是,我不能使用以下方案:'使用SHA-256的无参数RSASSA-PSS ' [RFC6931]: http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1
显示的错误是明确的"无法为提供的签名算法创建SignatureDescription "。
对于"RSA pkcs# 1 v1.5和SHA-256",我添加了以下公共类作为其签名:
public class RSAPKCS1SHA256SignatureDescription : SignatureDescription
{
public RSAPKCS1SHA256SignatureDescription()
{
base.KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";
base.DigestAlgorithm = "System.Security.Cryptography.SHA256Managed";
base.FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
base.DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
}
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = (AsymmetricSignatureDeformatter)
CryptoConfig.CreateFromName(base.DeformatterAlgorithm);
asymmetricSignatureDeformatter.SetKey(key);
asymmetricSignatureDeformatter.SetHashAlgorithm("SHA256");
return asymmetricSignatureDeformatter;
}
}
然而,我不知道。net 4.5是否支持"使用SHA-256的无参数RSASSA-PSS",如果支持,如何设置其签名定义。
如果有人有类似的经历并能提供一些帮助,我将非常感激。我终于弄明白了。诀窍是注册XML签名指定的算法,在我的例子中是"http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1"。将其注册到一个自定义类,该类使用自定义签名格式化程序和反格式化程序,它们应用RSASignaturePadding.Pss
.
这里是一个实现,您所要做的就是调用RsaSsaPss.RegisterSha256RsaMgf1()
一次,例如从您的客户端的静态构造函数。然后,SignedXml.CheckSignature()
和SignedXml.ComputeSignature()
自动适用于任何指定此算法的XML签名。
在Core 2.1和Framework 4.7.1上测试:
/// <summary>
/// Contains classes for using RSA-SSA-PSS, as well as methods for registering them.
/// Registering such types adds support for them to SignedXml.
/// </summary>
public class RsaSsaPss
{
/// <summary>
/// Registers an implementation for "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1".
/// </summary>
public static void RegisterSha256RsaMgf1()
{
CryptoConfig.AddAlgorithm(typeof(RsaPssSha256SignatureDescription), "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1");
}
// Can add further registrations here...
public class RsaPssSha256SignatureDescription : SignatureDescription
{
public RsaPssSha256SignatureDescription()
{
using (var rsa = RSA.Create())
{
this.KeyAlgorithm = rsa.GetType().AssemblyQualifiedName; // Does not like a simple algorithm name, but wants a type name (AssembyQualifiedName in Core)
}
this.DigestAlgorithm = "SHA256"; // Somehow wants a simple algorithm name
this.FormatterAlgorithm = typeof(RsaPssSignatureFormatter).FullName;
this.DeformatterAlgorithm = typeof(RsaPssSignatureDeformatter).FullName;
}
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
{
var signatureFormatter = new RsaPssSignatureFormatter();
signatureFormatter.SetKey(key);
signatureFormatter.SetHashAlgorithm(this.DigestAlgorithm);
return signatureFormatter;
}
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
var signatureDeformatter = new RsaPssSignatureDeformatter();
signatureDeformatter.SetKey(key);
signatureDeformatter.SetHashAlgorithm(this.DigestAlgorithm);
return signatureDeformatter;
}
public class RsaPssSignatureFormatter : AsymmetricSignatureFormatter
{
private RSA Key { get; set; }
private string HashAlgorithmName { get; set; }
public override void SetKey(AsymmetricAlgorithm key)
{
this.Key = (RSA)key;
}
public override void SetHashAlgorithm(string strName)
{
// Verify the name
Oid.FromFriendlyName(strName, OidGroup.HashAlgorithm);
this.HashAlgorithmName = strName;
}
public override byte[] CreateSignature(byte[] rgbHash)
{
return this.Key.SignHash(rgbHash, new HashAlgorithmName(this.HashAlgorithmName), RSASignaturePadding.Pss);
}
}
public class RsaPssSignatureDeformatter : AsymmetricSignatureDeformatter
{
private RSA Key { get; set; }
private string HashAlgorithmName { get; set; }
public override void SetKey(AsymmetricAlgorithm key)
{
this.Key = (RSA)key;
}
public override void SetHashAlgorithm(string strName)
{
// Verify the name
Oid.FromFriendlyName(strName, OidGroup.HashAlgorithm);
this.HashAlgorithmName = strName;
}
public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
{
return this.Key.VerifyHash(rgbHash, rgbSignature, new HashAlgorithmName(this.HashAlgorithmName), RSASignaturePadding.Pss);
}
}
}
}