This documentation is archived and is not being maintained.

DSACryptoServiceProvider.SignData Method (Byte[])

Computes the hash value of the specified byte array and signs the resulting hash value.

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

public byte[] SignData (
	byte[] buffer
)
public byte[] SignData (
	byte[] buffer
)
public function SignData (
	buffer : byte[]
) : byte[]
Not applicable.

Parameters

buffer

The input data for which to compute the hash.

Return Value

The DSA signature for the specified data.

This method creates a digital signature that is verified using the VerifyData method.

DSA uses the SHA1 hash algorithm.

The following code example signs and verifies data using the DSACryptoServiceProvider class.

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 data to sign.
			byte[] Data = {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[] SignedValue = DSASignData(Data, DSA.ExportParameters(true));

			//Verify the data and display the results.
			if(DSAVerifyData(Data, SignedValue, DSA.ExportParameters(false)))
			{
				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[] DSASignData(byte[] DataToSign, DSAParameters DSAKeyInfo)
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

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

			//Compute hash value, sign the hash, and return the signed hash.
			return DSA.SignData(DataToSign);
		}
		catch(CryptographicException e)
		{
			Console.WriteLine(e.Message);

			return null;
		}

	}

	public static bool DSAVerifyData(byte[] Data, byte[] SignedData, DSAParameters DSAKeyInfo)
	{
		try
		{
			//Create a new instance of DSACryptoServiceProvider.
			DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

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

			//Verify the signature and return the result.	
			return DSA.VerifyData(Data, SignedData);

		}
		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 data to sign.
            ubyte data[] =  {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 signedValue[] = DSASignData(data, dsa.ExportParameters(true));
            
            //Verify the data and display the results.
            if (DSAVerifyData(data, signedValue, dsa.ExportParameters(false))) {
                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[] DSASignData(ubyte dataToSign[], DSAParameters dsaKeyInfo) 
    {
        try {
            // Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            // Import the key information.   
            dsa.ImportParameters(dsaKeyInfo);
            
            // Compute hash value, sign the hash, and return the signed hash.
            return dsa.SignData(dataToSign) ;
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());            
            return null ;
        }
    } //DSASignData

    public static boolean DSAVerifyData(ubyte data[], ubyte signedData[], 
        DSAParameters dsaKeyInfo) 
    {
        try {
            // Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            // Import the key information. 
            dsa.ImportParameters(dsaKeyInfo);
            
            // Verify the signature and return the result.    
            return dsa.VerifyData(data, signedData) ;
        } 
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
            return false ;
        }
    } //DSAVerifyData
} //DSACSPSample

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
Show: