DSACryptoServiceProvider Class
.NET Framework Class Library
DSACryptoServiceProvider Class

Defines a wrapper object to access the cryptographic service provider (CSP) implementation of the DSA algorithm. This class cannot be inherited.

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
<ComVisibleAttribute(True)> _
Public NotInheritable Class DSACryptoServiceProvider _
    Inherits DSA _
    Implements ICspAsymmetricAlgorithm
C#
[ComVisibleAttribute(true)]
public sealed class DSACryptoServiceProvider : DSA, 
    ICspAsymmetricAlgorithm
Visual C++
[ComVisibleAttribute(true)]
public ref class DSACryptoServiceProvider sealed : public DSA, 
    ICspAsymmetricAlgorithm
F#
[<SealedAttribute>]
[<ComVisibleAttribute(true)>]
type DSACryptoServiceProvider =  
    class
        inherit DSA
        interface ICspAsymmetricAlgorithm
    end

You can use the DSACryptoServiceProvider class to create digital signatures and protect the integrity of your data.

To use a public-key system to digitally sign a message, the sender first applies a hash function to the message to create a message digest. The sender then encrypts the message digest with the sender's private key to create the sender's personal signature. Upon receiving the message and signature, the receiver decrypts the signature using the sender's public key to recover the message digest and hashes the message using the same hash algorithm that the sender used. If the message digest that the receiver computes exactly matches the message digest received from the sender, the receiver can be sure that the message was not altered while in transit. Note that a signature can be verified by anyone, because the sender's public key is common knowledge.

This algorithm supports key lengths from 512 bits to 1024 bits in increments of 64 bits.

The following code example creates a digital signature of a hash value using the DSACryptoServiceProvider class and then verifies the signature.

Visual Basic
Imports System
Imports System.Security.Cryptography

Public Class DSACSPSample
    Public Shared Sub Main()
        Try
            Dim privateKeyInfo As DSAParameters
            Dim publicKeyInfo As DSAParameters

            ' Create a new instance of DSACryptoServiceProvider to generate
            ' a new key pair.
            Using DSA As New DSACryptoServiceProvider()
                privateKeyInfo = DSA.ExportParameters(True)
                publicKeyInfo = DSA.ExportParameters(False)
            End Using

            ' 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, privateKeyInfo, "SHA1")

            ' Verify the hash and display the results.
            Dim verified As Boolean = DSAVerifyHash(HashValue, SignedHashValue, publicKeyInfo, "SHA1")

            If verified Then
                Console.WriteLine("The hash value was verified.")
            Else
                Console.WriteLine("The hash value was not verified.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Public Shared Function DSASignHash(HashToSign As Byte(), DSAKeyInfo As DSAParameters, _
        HashAlg As String) As Byte()
        Dim sig As Byte() = Nothing

        Try
            ' Create a new instance of DSACryptoServiceProvider.
            Using DSA As New DSACryptoServiceProvider()
                'Import the key information.
                DSA.ImportParameters(DSAKeyInfo)

                ' Create an DSASignatureFormatter object and pass it the
                ' DSACryptoServiceProvider to transfer the private key.
                Dim DSAFormatter As New DSASignatureFormatter(DSA)

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

                ' Create a signature for HashValue and return it.
                sig = DSAFormatter.CreateSignature(HashToSign)
            End Using
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try

        Return sig
    End Function

    Public Shared Function DSAVerifyHash(HashValue As Byte(), SignedHashValue As Byte(), _
        DSAKeyInfo As DSAParameters, HashAlg As String) As Boolean
        Dim verified As Boolean = False

        Try
            ' Create a new instance of DSACryptoServiceProvider.
            Using DSA As New DSACryptoServiceProvider()
                ' Import the key information.
                DSA.ImportParameters(DSAKeyInfo)

                ' Create an DSASignatureDeformatter object and pass it the
                ' DSACryptoServiceProvider to transfer the private key.
                Dim DSADeformatter As New DSASignatureDeformatter(DSA)

                ' Set the hash algorithm to the passed value.
                DSADeformatter.SetHashAlgorithm(HashAlg)

                ' Verify signature and return the result.
                verified = DSADeformatter.VerifySignature(HashValue, SignedHashValue)
            End Using
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try

        Return verified
    End Function
End Class
C#
using System;
using System.Security.Cryptography;

public class DSACSPSample
{
    public static void Main()
    {
        try
        {
            DSAParameters privateKeyInfo;
            DSAParameters publicKeyInfo;

            // Create a new instance of DSACryptoServiceProvider to generate
            // a new key pair.
            using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider())
            {
                privateKeyInfo = DSA.ExportParameters(true);
                publicKeyInfo = DSA.ExportParameters(false);
            }

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

            // Verify the hash and display the results.
            bool verified = DSAVerifyHash(HashValue, SignedHashValue, publicKeyInfo, "SHA1");

            if (verified)
            {
                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)
    {
        byte[] sig = null;

        try
        {
            // Create a new instance of DSACryptoServiceProvider.
            using (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.
                sig = DSAFormatter.CreateSignature(HashToSign);
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }

        return sig;
    }

    public static bool DSAVerifyHash(byte[] HashValue, byte[] SignedHashValue,
        DSAParameters DSAKeyInfo, string HashAlg)
    {
        bool verified = false;

        try
        {
            // Create a new instance of DSACryptoServiceProvider.
            using (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.
                verified = DSADeformatter.VerifySignature(HashValue, SignedHashValue);
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }

        return verified;
    }
}
Visual C++
using namespace System;
using namespace System::Security::Cryptography;

public ref class DSACSPSample
{
public:
    static void Main()
    {
        try
        {
            DSAParameters privateKeyInfo;
            DSAParameters publicKeyInfo;

            // Create a new instance of DSACryptoServiceProvider to generate
            // a new key pair.
            DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider();
            privateKeyInfo = DSA->ExportParameters(true);
            publicKeyInfo = DSA->ExportParameters(false);
            delete DSA;

            // The hash value to sign.
            array<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.
            array<Byte>^ SignedHashValue = DSASignHash(HashValue, privateKeyInfo, "SHA1");

            // Verify the hash and display the results.
            bool verified = DSAVerifyHash(HashValue, SignedHashValue, publicKeyInfo, "SHA1");

            if (verified)
            {
                Console::WriteLine("The hash value was verified.");
            }
            else
            {
                Console::WriteLine("The hash value was not verified.");
            }
        }
        catch (ArgumentNullException^ e)
        {
            Console::WriteLine(e->Message);
        }
    }

    static array<Byte>^ DSASignHash(array<Byte>^ HashToSign, DSAParameters DSAKeyInfo,
        String^ HashAlg)
    {
        array<Byte>^ sig = nullptr;

        try
        {
            // Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider();

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

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

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

            // Create a signature for HashValue and return it.
            sig = DSAFormatter->CreateSignature(HashToSign);
            delete DSA;
        }
        catch (CryptographicException^ e)
        {
            Console::WriteLine(e->Message);
        }

        return sig;
    }

    static bool DSAVerifyHash(array<Byte>^ HashValue, array<Byte>^ SignedHashValue,
        DSAParameters DSAKeyInfo, String^ HashAlg)
    {
        bool verified = false;

        try
        {
            // Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider();

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

            // Create an DSASignatureDeformatter object and pass it the
            // DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter^ DSADeformatter = gcnew DSASignatureDeformatter(DSA);

            // Set the hash algorithm to the passed value.
            DSADeformatter->SetHashAlgorithm(HashAlg);

            // Verify signature and return the result.
            verified = DSADeformatter->VerifySignature(HashValue, SignedHashValue);
            delete DSA;
        }
        catch (CryptographicException^ e)
        {
            Console::WriteLine(e->Message);
        }

        return verified;
    }
};

int main()
{
    DSACSPSample::Main();
}
System..::.Object
  System.Security.Cryptography..::.AsymmetricAlgorithm
    System.Security.Cryptography..::.DSA
      System.Security.Cryptography..::.DSACryptoServiceProvider
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker