Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SignData Method (Byte[])
Collapse the table of content
Expand the table of content

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;
		}
			
	}

}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft