Export (0) Print
Expand All

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)

[ComVisibleAttribute(true)] 
public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm
/** @attribute ComVisibleAttribute(true) */ 
public final class DSACryptoServiceProvider extends DSA implements ICspAsymmetricAlgorithm
ComVisibleAttribute(true) 
public final class DSACryptoServiceProvider extends DSA implements ICspAsymmetricAlgorithm
Not applicable.

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.

using System;
using System.Security.Cryptography;

class DSACSPSample
{
		
	static void Main()
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider to generate
			//a new key pair.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

			//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, DSA.ExportParameters(true), "SHA1");

			//Verify the hash and display the results.
			if(DSAVerifyHash(HashValue, SignedHashValue, DSA.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;
		}
			
	}

}

import System.*;
import System.Security.Cryptography.*;

class DSACSPSample
{
    public static void main(String[] args)
    {
        try {
            // Create a new instance of DSACryptoServiceProvider to generate
            // a new key pair.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            // The hash value to sign.
            ubyte 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.
            ubyte signedHashValue[] = DSASignHash(hashValue,
                dsa.ExportParameters(true), "SHA1");
            
            // Verify the hash and display the results.
            if (DSAVerifyHash(hashValue, signedHashValue, 
                dsa.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.get_Message());
        }
    } //main

    public static ubyte[] DSASignHash(ubyte 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.get_Message());
            return null ;
        }
    } //DSASignHash

    public static boolean DSAVerifyHash(ubyte hashValue[], 
        ubyte 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.get_Message());
            return false ;
        }
    } //DSAVerifyHash
} //DSACSPSample

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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft