Table of Contents

Class AsymmetricAlgorithmSignature

Namespace
iText.Signatures
Assembly
itext.sign.dll

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
    }
}
public class AsymmetricAlgorithmSignature : IExternalSignature
Inheritance
AsymmetricAlgorithmSignature
Implements
Inherited Members

Constructors

AsymmetricAlgorithmSignature(RSACryptoServiceProvider, string)

public AsymmetricAlgorithmSignature(RSACryptoServiceProvider algorithm, string digestAlgorithm)

Parameters

algorithm RSACryptoServiceProvider
digestAlgorithm string

Methods

GetDigestAlgorithmName()

public string GetDigestAlgorithmName()

Returns

string

GetSignatureAlgorithmName()

public string GetSignatureAlgorithmName()

Returns

string

GetSignatureMechanismParameters()

public ISignatureMechanismParams GetSignatureMechanismParameters()

Returns

ISignatureMechanismParams

Sign(byte[])

public byte[] Sign(byte[] message)

Parameters

message byte[]

Returns

byte[]