Export (0) Print
Expand All

RSACryptoServiceProvider.SignData Method (Byte[], Object)

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

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

public byte[] SignData(
	byte[] buffer,
	Object halg
)

Parameters

buffer
Type: System.Byte[]

The input data for which to compute the hash.

halg
Type: System.Object

The hash algorithm to use to create the hash value.

Return Value

Type: System.Byte[]
The RSA signature for the specified data.

ExceptionCondition
ArgumentNullException

The halg parameter is null.

ArgumentException

The halg parameter is not a valid type.

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

The halg parameter can accept a String, a HashAlgorithm, or a Type.

The following code example signs and verifies data.

using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{
    static void Main()
    {
        try
        {
            // Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = "Data to Sign";

            // Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] originalData = ByteConverter.GetBytes(dataString);
            byte[] signedData;

            // Create a new instance of the RSACryptoServiceProvider class  
            // and automatically create a new key-pair.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            // Export the key information to an RSAParameters object. 
            // You must pass true to export the private key for signing. 
            // However, you do not need to export the private key 
            // for verification.
            RSAParameters Key = RSAalg.ExportParameters(true);

            // Hash and sign the data.
            signedData = HashAndSignBytes(originalData, Key);

            // Verify the data and display the result to the  
            // console. 
            if(VerifySignedHash(originalData, signedData, Key))
            {
                Console.WriteLine("The data was verified.");
            }
            else
            {
                Console.WriteLine("The data does not match the signature.");
            }

        }
        catch(ArgumentNullException)
        {
            Console.WriteLine("The data was not signed or verified");

        }
    }
    public static byte[] HashAndSignBytes(byte[] DataToSign, RSAParameters Key)
    {
        try
        {   
            // Create a new instance of RSACryptoServiceProvider using the  
            // key from RSAParameters.  
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            RSAalg.ImportParameters(Key);

            // Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider 
            // to specify the use of SHA1 for hashing. 
            return RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }
    }

    public static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, RSAParameters Key)
    {
        try
        {
            // Create a new instance of RSACryptoServiceProvider using the  
            // key from RSAParameters.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            RSAalg.ImportParameters(Key);

            // Verify the data using the signature.  Pass a new instance of SHA1CryptoServiceProvider 
            // to specify the use of SHA1 for hashing. 
            return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), 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:
© 2014 Microsoft