Export (0) Print
Expand All

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
)

Parameters

buffer
Type: System.Byte[]
The input data for which to compute the hash.

Return Value

Type: System.Byte[]
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;
		}
			
	}

}


.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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), 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.

Community Additions

ADD
Show:
© 2014 Microsoft