Export (0) Print
Expand All

RSACryptoServiceProvider.VerifyHash Method

Verifies the specified signature data by comparing it to the signature computed for the specified hash value.

[Visual Basic]
Public Function VerifyHash( _
   ByVal rgbHash() As Byte, _
   ByVal str As String, _
   ByVal rgbSignature() As Byte _
) As Boolean
[C#]
public bool VerifyHash(
 byte[] rgbHash,
 string str,
 byte[] rgbSignature
);
[C++]
public: bool VerifyHash(
 unsigned char rgbHash __gc[],
 String* str,
 unsigned char rgbSignature __gc[]
);
[JScript]
public function VerifyHash(
   rgbHash : Byte[],
 str : String,
 rgbSignature : Byte[]
) : Boolean;

Parameters

rgbHash
The hash value of the data to be signed.
str
The hash algorithm identifier (OID) used to create the hash value of the data.
rgbSignature
The signature data to be verified.

Return Value

true if the signature verifies as valid; otherwise, false.

Exceptions

Exception Type Condition
ArgumentNullException The rgbHash parameter is a null reference (Nothing in Visual Basic).

-or-

The rgbSignature parameter is a null reference (Nothing).

CryptographicException The cryptographic service provider (CSP) cannot be acquired.

-or-

The signature cannot be verified.

Remarks

This method verifies the RSA digital signature produced by SignHash.

The valid hash algorithms are SHA1 and MD5. The algorithm identifier can be derived from the hash name by using the MapNameToOID method.

Example

[Visual Basic] 
Imports System
Imports System.Text
Imports System.Security.Cryptography

Namespace RSACryptoServiceProvider_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim toEncrypt() As Byte
            Dim encrypted() As Byte
            Dim signature() As Byte
            'Choose a small amount of data to encrypt.
            Dim original As String = "Hello"
            Dim myAscii As New ASCIIEncoding()

            'Create a sender and receiver.
            Dim mySender As New Sender()
            Dim myReceiver As New Receiver()

            'Convert the data string to a byte array.
            toEncrypt = myAscii.GetBytes(original)

            'Encrypt data using receiver's public key.
            encrypted = mySender.EncryptData(myReceiver.PublicParameters, toEncrypt)

            'Hash the encrypted data and generate a signature on the hash
            ' using the sender's private key.
            signature = mySender.HashAndSign(encrypted)

            Console.WriteLine("Original: {0}", original)

            'Verify the signature is authentic using the sender's public key.
            If myReceiver.VerifyHash(mySender.PublicParameters, encrypted, signature) Then
                'Decrypt the data using the receiver's private key.
                myReceiver.DecryptData(encrypted)
            Else
                Console.WriteLine("Invalid signature")
            End If
        End Sub 'Main
    End Class 'MyMainClass

    Class Sender
        Private rsaPubParams As RSAParameters
        Private rsaPrivateParams As RSAParameters

        Public Sub New()
            Dim rsaCSP As New RSACryptoServiceProvider()

            'Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(True)
            rsaPubParams = rsaCSP.ExportParameters(False)
        End Sub 'New

        Public ReadOnly Property PublicParameters() As RSAParameters
            Get
                Return rsaPubParams
            End Get
        End Property

        'Manually performs hash and then signs hashed value.
        Public Function HashAndSign(ByVal encrypted() As Byte) As Byte()
            Dim rsaCSP As New RSACryptoServiceProvider()
            Dim hash As New SHA1Managed()
            Dim hashedData() As Byte

            rsaCSP.ImportParameters(rsaPrivateParams)

            hashedData = hash.ComputeHash(encrypted)
            Return rsaCSP.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"))
        End Function 'HashAndSign

        'Encrypts using only the public key data.
        Public Function EncryptData(ByVal rsaParams As RSAParameters, ByVal toEncrypt() As Byte) As Byte()
            Dim rsaCSP As New RSACryptoServiceProvider()

            rsaCSP.ImportParameters(rsaParams)
            Return rsaCSP.Encrypt(toEncrypt, False)
        End Function 'EncryptData
    End Class 'Sender

    Class Receiver
        Private rsaPubParams As RSAParameters
        Private rsaPrivateParams As RSAParameters

        Public Sub New()
            Dim rsaCSP As New RSACryptoServiceProvider()

            'Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(True)
            rsaPubParams = rsaCSP.ExportParameters(False)
        End Sub 'New

        Public ReadOnly Property PublicParameters() As RSAParameters
            Get
                Return rsaPubParams
            End Get
        End Property

        'Manually performs hash and then verifies hashed value.
        Public Function VerifyHash(ByVal rsaParams As RSAParameters, ByVal signedData() As Byte, ByVal signature() As Byte) As Boolean
            Dim rsaCSP As New RSACryptoServiceProvider()
            Dim hash As New SHA1Managed()
            Dim hashedData() As Byte

            rsaCSP.ImportParameters(rsaParams)

            hashedData = hash.ComputeHash(signedData)
            Return rsaCSP.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature)
        End Function 'VerifyHash

        'Decrypt using the private key data.
        Public Sub DecryptData(ByVal encrypted() As Byte)
            Dim fromEncrypt() As Byte
            Dim roundTrip As String
            Dim myAscii As New ASCIIEncoding()
            Dim rsaCSP As New RSACryptoServiceProvider()

            rsaCSP.ImportParameters(rsaPrivateParams)
            fromEncrypt = rsaCSP.Decrypt(encrypted, False)
            roundTrip = myAscii.GetString(fromEncrypt)

            Console.WriteLine("RoundTrip: {0}", roundTrip)
        End Sub 'DecryptData
    End Class 'Receiver
End Namespace 'RSACryptoServiceProvider_Examples

[C#] 
using System;
using System.Text;
using System.Security.Cryptography;

namespace RSACryptoServiceProvider_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            byte[] toEncrypt;
            byte[] encrypted;
            byte[] signature;
            //Choose a small amount of data to encrypt.
            string original = "Hello";
            ASCIIEncoding myAscii = new ASCIIEncoding();

            //Create a sender and receiver.
            Sender mySender = new Sender();
            Receiver myReceiver = new Receiver();

            //Convert the data string to a byte array.
            toEncrypt = myAscii.GetBytes(original);
            
            //Encrypt data using receiver's public key.
            encrypted = mySender.EncryptData(myReceiver.PublicParameters, toEncrypt);
            
            //Hash the encrypted data and generate a signature on the hash
            // using the sender's private key.
            signature = mySender.HashAndSign(encrypted);

            Console.WriteLine("Original: {0}", original);

            //Verify the signature is authentic using the sender's public key.
            if(myReceiver.VerifyHash(mySender.PublicParameters, encrypted, signature))
            {
                //Decrypt the data using the receiver's private key.
                myReceiver.DecryptData(encrypted);
            }
            else
            {
                Console.WriteLine("Invalid signature");
            }
        }
    }

    class Sender
    {
        RSAParameters rsaPubParams;
        RSAParameters rsaPrivateParams;

        public Sender()
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            
            //Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(true);
            rsaPubParams = rsaCSP.ExportParameters(false);
        }
        
        public RSAParameters PublicParameters
        {
            get
            {
                return rsaPubParams;
            }
        }

        //Manually performs hash and then signs hashed value.
        public byte[] HashAndSign(byte[] encrypted)
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            SHA1Managed hash = new SHA1Managed();
            byte[] hashedData;

            rsaCSP.ImportParameters(rsaPrivateParams);

            hashedData = hash.ComputeHash(encrypted);
            return rsaCSP.SignHash(hashedData, CryptoConfig.MapNameToOID("SHA1"));
        }

        //Encrypts using only the public key data.
        public byte[] EncryptData(RSAParameters rsaParams, byte[] toEncrypt)
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();

            rsaCSP.ImportParameters(rsaParams);
            return rsaCSP.Encrypt(toEncrypt, false);
        }
    }

    class Receiver
    {
        RSAParameters rsaPubParams;
        RSAParameters rsaPrivateParams;

        public Receiver()
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            
            //Generate public and private key data.
            rsaPrivateParams = rsaCSP.ExportParameters(true);
            rsaPubParams = rsaCSP.ExportParameters(false);
        }

        public RSAParameters PublicParameters
        {
            get
            {
                return rsaPubParams;
            }
        }

        //Manually performs hash and then verifies hashed value.
        public bool VerifyHash(RSAParameters rsaParams, byte[] signedData, byte[] signature)
        {
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();
            SHA1Managed hash = new SHA1Managed();
            byte[] hashedData;

            rsaCSP.ImportParameters(rsaParams);

            hashedData = hash.ComputeHash(signedData);
            return rsaCSP.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature);
        }

        //Decrypt using the private key data.
        public void DecryptData(byte[] encrypted)
        {
            byte[] fromEncrypt;
            string roundTrip;
            ASCIIEncoding myAscii = new ASCIIEncoding();
            RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider();

            rsaCSP.ImportParameters(rsaPrivateParams);
            fromEncrypt = rsaCSP.Decrypt(encrypted, false);
            roundTrip = myAscii.GetString(fromEncrypt);

            Console.WriteLine("RoundTrip: {0}", roundTrip);
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;

__gc class Sender {
private:
    RSAParameters rsaPubParams;
    RSAParameters rsaPrivateParams;

public:
    Sender() {
        RSACryptoServiceProvider* rsaCSP = new RSACryptoServiceProvider();

        //Generate public and private key data.
        rsaPrivateParams = rsaCSP->ExportParameters(true);
        rsaPubParams = rsaCSP->ExportParameters(false);
    }

    __property RSAParameters get_PublicParameters() {
        return rsaPubParams;
    }

    //Manually performs hash and then signs hashed value.
    Byte HashAndSign(Byte encrypted[]) [] {
        RSACryptoServiceProvider* rsaCSP = new RSACryptoServiceProvider();
        SHA1Managed* hash = new SHA1Managed();
        Byte hashedData[];

        rsaCSP->ImportParameters(rsaPrivateParams);

        hashedData = hash->ComputeHash(encrypted);
        return rsaCSP->SignHash(hashedData, CryptoConfig::MapNameToOID(S"SHA1"));
    }

    //Encrypts using only the public key data.
    Byte EncryptData(RSAParameters rsaParams, Byte toEncrypt[]) [] {
        RSACryptoServiceProvider* rsaCSP = new RSACryptoServiceProvider();

        rsaCSP->ImportParameters(rsaParams);
        return rsaCSP->Encrypt(toEncrypt, false);
    }
};

__gc class Receiver {
private:
    RSAParameters rsaPubParams;
    RSAParameters rsaPrivateParams;

public:
    Receiver() {
        RSACryptoServiceProvider* rsaCSP = new RSACryptoServiceProvider();

        //Generate public and private key data.
        rsaPrivateParams = rsaCSP->ExportParameters(true);
        rsaPubParams = rsaCSP->ExportParameters(false);
    }

    __property RSAParameters get_PublicParameters() {
        return rsaPubParams;
    }

    //Manually performs hash and then verifies hashed value.
    bool VerifyHash(RSAParameters rsaParams, Byte signedData[], Byte signature[]) {
        RSACryptoServiceProvider* rsaCSP = new RSACryptoServiceProvider();
        SHA1Managed* hash = new SHA1Managed();
        Byte hashedData[];

        rsaCSP->ImportParameters(rsaParams);

        hashedData = hash->ComputeHash(signedData);
        return rsaCSP->VerifyHash(hashedData, CryptoConfig::MapNameToOID(S"SHA1"), signature);
    }

    //Decrypt using the private key data.
    void DecryptData(Byte encrypted[]) {
        Byte fromEncrypt[];
        String* roundTrip;
        ASCIIEncoding* myAscii = new ASCIIEncoding();
        RSACryptoServiceProvider* rsaCSP = new RSACryptoServiceProvider();

        rsaCSP->ImportParameters(rsaPrivateParams);
        fromEncrypt = rsaCSP->Decrypt(encrypted, false);
        roundTrip = myAscii->GetString(fromEncrypt);

        Console::WriteLine(S"RoundTrip: {0}", roundTrip);
    }
};

int main() {
    Byte toEncrypt[];
    Byte encrypted[];
    Byte signature[];
    //Choose a small amount of data to encrypt.
    String* original = S"Hello";
    ASCIIEncoding* myAscii = new ASCIIEncoding();

    //Create a sender and receiver.
    Sender* mySender = new Sender();
    Receiver* myReceiver = new Receiver();

    //Convert the data string to a byte array.
    toEncrypt = myAscii->GetBytes(original);

    //Encrypt data using receiver's public key.
    encrypted = mySender->EncryptData(myReceiver->PublicParameters, toEncrypt);

    //Hash the encrypted data and generate a signature on the hash
    // using the sender's private key.
    signature = mySender->HashAndSign(encrypted);

    Console::WriteLine(S"Original: {0}", original);

    //Verify the signature is authentic using the sender's public key.
    if (myReceiver->VerifyHash(mySender->PublicParameters, encrypted, signature)) {
        //Decrypt the data using the receiver's private key.
        myReceiver->DecryptData(encrypted);
    } else {
        Console::WriteLine(S"Invalid signature");
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RSACryptoServiceProvider Class | RSACryptoServiceProvider Members | System.Security.Cryptography Namespace | Cryptographic Services

Show:
© 2014 Microsoft