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)

'Declaration
Public Function SignData ( _
	buffer As Byte(), _
	halg As Object _
) As Byte()
'Usage
Dim instance As RSACryptoServiceProvider
Dim buffer As Byte()
Dim halg As Object
Dim returnValue As Byte()

returnValue = instance.SignData(buffer, halg)
public byte[] SignData (
	byte[] buffer, 
	Object halg
)
public function SignData (
	buffer : byte[], 
	halg : Object
) : byte[]

Parameters

buffer

The input data for which to compute the hash.

halg

The hash algorithm to use to create the hash value.

Return Value

The RSA signature for the specified data.

Exception typeCondition

ArgumentNullException

The halg parameter is a null reference (Nothing in Visual Basic).

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.

Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            ' Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New ASCIIEncoding

            Dim dataString As String = "Data to Sign"

            ' Create byte arrays to hold original, encrypted, and decrypted data.
            Dim originalData As Byte() = ByteConverter.GetBytes(dataString)
            Dim signedData() As Byte

            ' Create a new instance of the RSACryptoServiceProvider class 
            ' and automatically create a new key-pair.
            Dim RSAalg As 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.
            Dim Key As RSAParameters = 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) Then
                Console.WriteLine("The data was verified.")
            Else
                Console.WriteLine("The data does not match the signature.")
            End If

        Catch e As ArgumentNullException
            Console.WriteLine("The data was not signed or verified.")
        End Try
    End Sub

    Function HashAndSignBytes(ByVal DataToSign() As Byte, ByVal Key As RSAParameters) As Byte()
        Try
            ' Create a new instance of RSACryptoServiceProvider using the 
            ' key from RSAParameters.  
            Dim RSAalg As 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 e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Function VerifySignedHash(ByVal DataToVerify() As Byte, ByVal SignedData() As Byte, ByVal Key As RSAParameters) As Boolean
        Try
            ' Create a new instance of RSACryptoServiceProvider using the 
            ' key from RSAParameters.
            Dim RSAalg As 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 e As CryptographicException
            Console.WriteLine(e.Message)

            Return False
        End Try
    End Function
End Module

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

class RSACSPSample
{
    public static void main(String[] args)
    {
        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.
            ubyte originalData[] = byteConverter.GetBytes(dataString);
            ubyte 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 exp) {
            Console.WriteLine("The data was not signed or verified");
        }
    } //main

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

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

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Show: