DSACryptoServiceProvider.VerifyData Method
.NET Framework 4
Verifies the specified signature data by comparing it to the signature computed for the specified data.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- rgbData
- Type: System.Byte[]
The data that was signed.
- rgbSignature
- Type: System.Byte[]
The signature data to be verified.
The following code example signs and verifies data using the DSACryptoServiceProvider class.
using System; using System.Security.Cryptography; class DSACSPSample { static void Main() { try { //Create a new instance of DSACryptoServiceProvider to generate //a new key pair. DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //The data to sign. byte[] Data = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135}; //The value to hold the signed value. byte[] SignedValue = DSASignData(Data, DSA.ExportParameters(true)); //Verify the data and display the results. if(DSAVerifyData(Data, SignedValue, DSA.ExportParameters(false))) { Console.WriteLine("The hash value was verified."); } else { Console.WriteLine("The hash value was not verified."); } } catch(ArgumentNullException e) { Console.WriteLine(e.Message); } } public static byte[] DSASignData(byte[] DataToSign, DSAParameters DSAKeyInfo) { try { //Create a new instance of DSACryptoServiceProvider. DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information. DSA.ImportParameters(DSAKeyInfo); //Compute hash value, sign the hash, and return the signed hash. return DSA.SignData(DataToSign); } catch(CryptographicException e) { Console.WriteLine(e.Message); return null; } } public static bool DSAVerifyData(byte[] Data, byte[] SignedData, DSAParameters DSAKeyInfo) { try { //Create a new instance of DSACryptoServiceProvider. DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information. DSA.ImportParameters(DSAKeyInfo); //Verify the signature and return the result. return DSA.VerifyData(Data, SignedData); } catch(CryptographicException e) { Console.WriteLine(e.Message); return false; } } }
The following code example creates a DSACryptoServiceProvider, generates a new key pair, and signs and tests some data.
using System; using System.Security.Cryptography; using System.Text; class DSACSPSample { 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[] dataToSign = ByteConverter.GetBytes(dataString); byte[] hashedData; byte[] signedData; // Create a SHA1CryptoServiceProvider object // to hash the data that will be signed. SHA1CryptoServiceProvider SHA1alg = new SHA1CryptoServiceProvider(); // Hash the data to sign. hashedData = SHA1alg.ComputeHash(dataToSign); //Create a new instance of the DSACryptoServiceProvider class // and automatically create a new key-pair. DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(); //Use the MapNameToOID method to get an object identifier //(OID) from the string name of the SHA1 algorithm. string OID = CryptoConfig.MapNameToOID("SHA1"); // Sign the hash and pass the OID. signedData = DSAalg.SignHash(hashedData, OID); // Verify the signature and display the results to the // console. if(DSAalg.VerifySignature(hashedData, signedData)) { Console.WriteLine("The data was verified."); } else { Console.WriteLine("The data does not match the signature."); } } catch(CryptographicException e) { //Catch this exception in case the encryption did //not succeed. Console.WriteLine(e.Message); } } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.