Skip to main content
Skip table of contents

AsymmetricAlgorithmSignature

AsymmetricAlgorithmSignature Public class

Description

This class allows you to sign with either an RSACryptoServiceProvider/DSACryptoServiceProvider from a X509Certificate2, or from manually created RSACryptoServiceProvider/DSACryptoServiceProvider. Depending on the certificate's CSP, sometimes you will not be able to sign with SHA-256/SHA-512 hash algorithm with RSACryptoServiceProvider taken directly from the certificate. This class allows you to use a workaround in this case and sign with certificate's private key and SHA-256/SHA-512 anyway.

         An example of a workaround for CSP that does not support SHA-256/SHA-512: ...

Diagram

  flowchart LR
  classDef interfaceStyle stroke-dasharray: 5 5;
  classDef abstractStyle stroke-width:4px
  subgraph iTextSharp.text.pdf.security
  iTextSharp.text.pdf.security.AsymmetricAlgorithmSignature[[AsymmetricAlgorithmSignature]]
  iTextSharp.text.pdf.security.IExternalSignature[[IExternalSignature]]
  class iTextSharp.text.pdf.security.IExternalSignature interfaceStyle;
  end
iTextSharp.text.pdf.security.IExternalSignature --> iTextSharp.text.pdf.security.AsymmetricAlgorithmSignature

Members

Methods

Public methods
ReturnsName
stringGetEncryptionAlgorithm()
stringGetHashAlgorithm()
byte``[]Sign(byte``[] message)

Details

Summary

This class allows you to sign with either an RSACryptoServiceProvider/DSACryptoServiceProvider from a X509Certificate2, or from manually created RSACryptoServiceProvider/DSACryptoServiceProvider. Depending on the certificate's CSP, sometimes you will not be able to sign with SHA-256/SHA-512 hash algorithm with RSACryptoServiceProvider taken directly from the certificate. This class allows you to use a workaround in this case and sign with certificate's private key and SHA-256/SHA-512 anyway.

         An example of a workaround for CSP that does not support SHA-256/SHA-512:

                        if (certificate.PrivateKey is RSACryptoServiceProvider)
                        {                
                            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
            
                            // Modified by J. Arturo
                            // Workaround for SHA-256 and SHA-512
            
                            if (rsa.CspKeyContainerInfo.ProviderName == "Microsoft Strong Cryptographic Provider" ||
                                            rsa.CspKeyContainerInfo.ProviderName == "Microsoft Enhanced Cryptographic Provider v1.0" ||
                                            rsa.CspKeyContainerInfo.ProviderName == "Microsoft Base Cryptographic Provider v1.0")
                            {
                                string providerName = "Microsoft Enhanced RSA and AES Cryptographic Provider";
                                int providerType = 24;
            
                                Type CspKeyContainerInfo_Type = typeof(CspKeyContainerInfo);
            
                                FieldInfo CspKeyContainerInfo_m_parameters = CspKeyContainerInfo_Type.GetField("m_parameters", BindingFlags.NonPublic | BindingFlags.Instance);
                                CspParameters parameters = (CspParameters)CspKeyContainerInfo_m_parameters.GetValue(rsa.CspKeyContainerInfo);
            
                                var cspparams = new CspParameters(providerType, providerName, rsa.CspKeyContainerInfo.KeyContainerName);
                                cspparams.Flags = parameters.Flags;
            
                                using (var rsaKey = new RSACryptoServiceProvider(cspparams))
                                {
                                    // use rsaKey now
                                }
                            }
                            else
                            {
                                // Use rsa directly
                            }
                        }
             

Inheritance

Constructors

AsymmetricAlgorithmSignature [1/2]

Source code

public AsymmetricAlgorithmSignature(RSACryptoServiceProvider algorithm, string hashAlgorithm)
Arguments
TypeNameDescription
RSACryptoServiceProvideralgorithm
stringhashAlgorithm
AsymmetricAlgorithmSignature [2/2]

Source code

public AsymmetricAlgorithmSignature(DSACryptoServiceProvider algorithm)
Arguments
TypeNameDescription
DSACryptoServiceProvideralgorithm

Methods

Sign

Source code

public virtual byte Sign(byte[] message)
Arguments
TypeNameDescription
byte``[]message
GetHashAlgorithm

Source code

public virtual string GetHashAlgorithm()
GetEncryptionAlgorithm

Source code

public virtual string GetEncryptionAlgorithm()

Generated with ModularDoc

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.