Compartir a través de


Verifying Signatures 

In order to verify that data was signed by a particular party, you must have the following information:

  • The public key of the party that signed the data.

  • The digital signature.

  • The data that was signed.

  • The hash algorithm used by the signer.

To verify a signature signed by the RSAPKCS1SignatureFormatter class, use the RSAPKCS1SignatureDeformatter class. The RSAPKCS1SignatureDeformatter class must be supplied the public key of the signer. You will need the values of the modulus and the exponent to specify the public key. (The party that generated the public/private key pair should provide these values.) First create an RSACryptoServiceProvider object to hold the public key that will verify the signature, and then initialize an RSAParameters structure to the modulus and exponent values that specify the public key.

The following code shows the creation of an RSAParameters structure. The Modulus property is set to the value of a byte array called ModulusData and the Exponent property is set to the value of a byte array called ExponentData.

Dim RSAKeyInfo As RSAParameters
RSAKeyInfo.Modulus = ModulusData
RSAKeyInfo.Exponent = ExponentData
RSAParameters RSAKeyInfo;
RSAKeyInfo.Modulus = ModulusData;
RSAKeyInfo.Exponent = ExponentData;

After you have created the RSAParameters object, you can initialize a new instance of the RSACryptoServiceProvider class to the values specified in RSAParameters. The RSACryptoServiceProvider is, in turn, passed to the constructor of an RSAPKCS1SignatureDeformatter to transfer the key.

The following example illustrates this process. In this example, HashValue and SignedHashValue are arrays of bytes provided by a remote party. The remote party has signed the HashValue using the SHA1 algorithm, producing the digital signature SignedHashValue. The RSAPKCS1SignatureDeformatter.VerifySignature method verifies that the digital signature is valid and was used to sign the HashValue.

Dim RSA As New RSACryptoServiceProvider()
RSA.ImportParameters(RSAKeyInfo)
Dim RSADeformatter As New RSAPKCS1SignatureDeformatter(RSA)
RSADeformatter.SetHashAlgorithm("SHA1")
If RSADeformatter.VerifySignature(HashValue, SignedHashValue) Then
   Console.WriteLine("The signature is valid.")
Else
   Console.WriteLine("The signture is not valid.")
End If
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(RSAKeyInfo);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("SHA1");
if(RSADeformatter.VerifySignature(HashValue, SignedHashValue))
{
   Console.WriteLine("The signature is valid.");
}
else
{
   Console.WriteLine("The signature is not valid.");
}

This code fragment will display "The signature is valid" if the signature is valid and "The signature is not valid" if it is not.

See Also

Concepts

Generating Signatures
Cryptographic Signatures

Other Resources

Cryptographic Tasks
Cryptographic Services