How to Verify a RSA-SHA512 XML Signature in .NET?

前端 未结 2 1955
一向
一向 2021-01-18 14:26

With the help of the MSDN site about SignedXml, I can easily verify if an XML DSig is correct. It works perfectly if the signature method sha1 was used.

However, whe

2条回答
  •  我在风中等你
    2021-01-18 15:14

    You can verfify RSA SHA512 signatures but you'll have to implement and register the signature description by yourself.

    Signature description:

    public sealed class RSAPKCS1SHA512SignatureDescription : SignatureDescription
    {
        public RSAPKCS1SHA512SignatureDescription()
        {
            KeyAlgorithm = typeof( RSACryptoServiceProvider ).FullName;
            DigestAlgorithm = typeof( SHA512Managed ).FullName;
            FormatterAlgorithm = typeof( RSAPKCS1SignatureFormatter ).FullName;
            DeformatterAlgorithm = typeof( RSAPKCS1SignatureDeformatter ).FullName;
        }
    
        public override AsymmetricSignatureDeformatter CreateDeformatter( AsymmetricAlgorithm key )
        {
            if( key == null )
            {
                throw new ArgumentNullException( "key" );
            }
    
            var deformatter = new RSAPKCS1SignatureDeformatter( key );
            deformatter.SetHashAlgorithm( "SHA512" );
            return deformatter;
        }
    
        public override AsymmetricSignatureFormatter CreateFormatter( AsymmetricAlgorithm key )
        {
            if( key == null )
            {
                throw new ArgumentNullException( "key" );
            }
    
            var formatter = new RSAPKCS1SignatureFormatter( key );
            formatter.SetHashAlgorithm( "SHA512" );
            return formatter;
        }
    }
    

    In your code you'll have to register this description with CryptoConfig:

    const string XmlDsigRsaSha512 = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
    CryptoConfig.AddAlgorithm( typeof( RSAPKCS1SHA512SignatureDescription ), XmlDsigRsaSha512 );
    

    I tested it with .Net 4.0 on Windows 7 64 Bit.

提交回复
热议问题