Export (0) Print
Expand All

DSACryptoServiceProvider Constructor

.NET Framework 1.1

Initializes a new instance of the DSACryptoServiceProvider class.

Overload List

Initializes a new instance of the DSACryptoServiceProvider class.

[Visual Basic] Public Sub New()
[C#] public DSACryptoServiceProvider();
[C++] public: DSACryptoServiceProvider();
[JScript] public function DSACryptoServiceProvider();

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

[Visual Basic] Public Sub New(CspParameters)
[C#] public DSACryptoServiceProvider(CspParameters);
[C++] public: DSACryptoServiceProvider(CspParameters*);
[JScript] public function DSACryptoServiceProvider(CspParameters);

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

[Visual Basic] Public Sub New(Integer)
[C#] public DSACryptoServiceProvider(int);
[C++] public: DSACryptoServiceProvider(int);
[JScript] public function DSACryptoServiceProvider(int);

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

[Visual Basic] Public Sub New(Integer, CspParameters)
[C#] public DSACryptoServiceProvider(int, CspParameters);
[C++] public: DSACryptoServiceProvider(int, CspParameters*);
[JScript] public function DSACryptoServiceProvider(int, CspParameters);

Example

[Visual Basic, C#] The following example shows how to create a DSACryptoServiceProvider object and to use that object for encryption.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of the DSACryptoServiceProvider constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System.Security.Cryptography

Module DSACSPExample

    Sub Main()
        Try
            Dim KeyContainerName As String = "MyKeyContainer"

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

            'The hash value to sign.
            Dim HashValue As Byte() = {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.
            Dim SignedHashValue As Byte() = DSASignHash(HashValue, KeyContainerName, "SHA1")

            'Verify the hash and display the results.
            If DSAVerifyHash(HashValue, SignedHashValue, KeyContainerName, "SHA1") Then
                Console.WriteLine("The hash value was verified.")
            Else
                Console.WriteLine("The hash value was not verified.")
            End If

            'Delete 
            DSADeleteKeyInCSP(KeyContainerName)


        Catch e As ArgumentNullException
            Console.WriteLine(e.Message)
        End Try

    End Sub

    Sub DSAPersistKeyInCSP(ByVal ContainerName As String)
        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.
            Dim cspParams As 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 a key size of 1024. Pass the CspParameters 
            'class to persist the key in the container.
            Dim DSAalg As New DSACryptoServiceProvider(1024, cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the container, ""{0}"".", ContainerName)

            'Indicate the key size.
            Console.WriteLine("The key size is: {0}.", DSAalg.KeySize)

        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub DSADeleteKeyInCSP(ByVal ContainerName As String)
        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.
            Dim cspParams As 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.
            Dim DSAalg As 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 e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Function DSASignHash(ByVal HashToSign() As Byte, ByVal ContainerName As String, ByVal HashAlg As String) As Byte()
        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.
            Dim cspParams As 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.
            Dim DSAalg As New DSACryptoServiceProvider(cspParams)

            'Create an DSASignatureFormatter object and pass it the 
            'DSACryptoServiceProvider to transfer the private key.
            Dim DSAFormatter As 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 e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Function DSAVerifyHash(ByVal HashValue() As Byte, ByVal SignedHashValue() As Byte, ByVal ContainerName As String, ByVal HashAlg As String) As Boolean
        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.
            Dim cspParams As 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.
            Dim DSAalg As New DSACryptoServiceProvider(cspParams)

            'Create an DSASignatureDeformatter object and pass it the 
            'DSACryptoServiceProvider to transfer the private key.
            Dim DSADeformatter As 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 e As CryptographicException
            Console.WriteLine(e.Message)

            Return False
        End Try
    End Function

End Module

[C#] 
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 a key size of 1024. Pass the CspParameters 
            //class to persist the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(1024, cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the container, \"{0}\".", ContainerName);
      
            //Indicate the key size.
            Console.WriteLine("The key size is: {0}.", DSAalg.KeySize);
        }
        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;
        }        
    }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

DSACryptoServiceProvider Class | DSACryptoServiceProvider Members | System.Security.Cryptography Namespace

Show:
© 2014 Microsoft