Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

RSACryptoServiceProvider.SignData Method (Byte(), Int32, Int32, Object)

Computes the hash value of a subset 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(), _
	offset As Integer, _
	count As Integer, _
	halg As Object _
) As Byte()

Parameters

buffer
Type: System.Byte()

The input data for which to compute the hash.

offset
Type: System.Int32

The offset into the array from which to begin using data.

count
Type: System.Int32

The number of bytes in the array to use as data.

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 Nothing.

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 string value can be one of the following:

  • The object identifier (OID) friendly name of the hash algorithm to use, either a name registered in the crypto config file or one in the Crypto API OID table.

  • The OID value. The OID must be one recognized by the Crypto API.

For example, you could use SignData(new byte[5], "1.3.14.3.2.26") or SignData(new byte[5], "sha1"), or SignData(new byte[5], "SHA1").

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 
            Dim smallArray() 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.  Start at the fifth offset 
            ' only use data from the next 7 bytes.
            signedData = HashAndSignBytes(originalData, Key, 5, 7)

            ' The previous function only signed one segment 
            ' of the array.  Create a new array for verification 
            ' that only holds the data that was actually signed. 
            
            ' Initialize the array.
            smallArray = New Byte(6) {}
            ' Copy 7 bytes starting at the 5th index to  
            ' the new array.
            Array.Copy(originalData, 5, smallArray, 0, 7)

            ' Verify the data and display the result to the  
            ' console.   
            If VerifySignedHash(smallArray, 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, ByVal Index As Integer, ByVal Length As Integer) 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, Index, Length, 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Show:
© 2015 Microsoft