This documentation is archived and is not being maintained.

DSACryptoServiceProvider Constructor (Int32)

Initializes a new instance of the DSACryptoServiceProvider class with the specified key size.

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

public DSACryptoServiceProvider(
	int dwKeySize
)

Parameters

dwKeySize
Type: System.Int32

The size of the key for the asymmetric algorithm in bits.

The following code example creates a DSACryptoServiceProvider, generates a new key pair, and persists it to a key container.

using System;
using System.Security.Cryptography;

class DSACSPExample
{
    static void Main()
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider to generate 
            //a new key pair.  This constructor specifies that the key-length 
            //will be 1024.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(1024);

            //The hash value to sign.
            byte[] HashValue = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
				
            //The value to hold the signed value.
            byte[] SignedHashValue = DSASignHash(HashValue, DSAalg.ExportParameters(true), "SHA1");

            //Verify the hash and display the results. 
            if(DSAVerifyHash(HashValue, SignedHashValue, DSAalg.ExportParameters(false), "SHA1"))
            {
                Console.WriteLine("The hash value was verified.");
            }
            else
            {
                Console.WriteLine("The hash value was not verified.");
            }


        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static byte[] DSASignHash(byte[] HashToSign, DSAParameters DSAKeyInfo, string HashAlg)
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

            //Import the key information.   
            DSA.ImportParameters(DSAKeyInfo);

            //Create an DSASignatureFormatter object and pass it the  
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSA);

            //Set the hash algorithm to the passed value.
            DSAFormatter.SetHashAlgorithm(HashAlg);

            //Create a signature for HashValue and return it. 
            return DSAFormatter.CreateSignature(HashToSign);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    public static bool DSAVerifyHash(byte[] HashValue, byte[] SignedHashValue, DSAParameters DSAKeyInfo, string HashAlg)
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

            //Import the key information. 
            DSA.ImportParameters(DSAKeyInfo);

            //Create an DSASignatureDeformatter object and pass it the  
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSA);
				
            //Set the hash algorithm to the passed value.
            DSADeformatter.SetHashAlgorithm(HashAlg);

            //Verify signature and return the result.  
            return DSADeformatter.VerifySignature(HashValue, SignedHashValue);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return false;
        }		
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Show: