Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DSACryptoServiceProvider Constructor (CspParameters)

Updated: April 2009

Initializes a new instance of the DSACryptoServiceProvider class with the specified parameters for the cryptographic service provider (CSP).

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)
public DSACryptoServiceProvider(
	CspParameters parameters
)

Parameters

parameters
Type: System.Security.Cryptography.CspParameters

The parameters for the CSP.

Important noteImportant Note:

In the .NET Framework version 3.5 Service Pack 1 and later, when you use a constructor that supplies the default ProviderType field, the value that is supplied is 24 (PROV_RSA_AES). However, the ProviderName field is not supplied when ProviderType is greater than 20. To explicitly set the ProviderType to a value that supplies the ProviderName, use this constructor and set the CspParameters.ProviderType field to a value that is less than 20, for example, 13 (PROV_DSS_DH).

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
        {
            string KeyContainerName = "MyKeyContainer";

            //Create a new key and persist it in  
            //the key container.
            DSAPersistKeyInCSP(KeyContainerName);

            //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, KeyContainerName, "SHA1");

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

            //Delete 
            DSADeleteKeyInCSP(KeyContainerName);


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

    public static void DSAPersistKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass 
            // 13 to specify a DSA container or 1 to specify 
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider to generate 
            //a new key pair.  Pass the CspParameters class to persist the  
            //key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
    }

    public static void DSADeleteKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass 
            // 13 to specify a DSA container or 1 to specify 
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.  
            //Pass the CspParameters class to use the  
            //key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Delete the key entry in the container.
            DSAalg.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from the container.
            DSAalg.Clear();

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
    }

    public static byte[] DSASignHash(byte[] HashToSign, string ContainerName, string HashAlg)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass 
            // 13 to specify a DSA container or 1 to specify 
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the key  
            //from the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

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

            //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, string ContainerName, string HashAlg)
    {
        try
        {   
            // Create a new instance of CspParameters.  Pass 
            // 13 to specify a DSA container or 1 to specify 
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the key  
            //from the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Create an DSASignatureDeformatter object and pass it the  
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSAalg);
				
            //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

Date

History

Reason

April 2009

Added advisory note about constructor.

Content bug fix.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.