DSACryptoServiceProvider Class
Defines a wrapper object to access the cryptographic service provider (CSP) implementation of the DSA algorithm.
For a list of all members of this type, see DSACryptoServiceProvider Members.
System.Object
System.Security.Cryptography.AsymmetricAlgorithm
System.Security.Cryptography.DSA
System.Security.Cryptography.DSACryptoServiceProvider
[Visual Basic] NotInheritable Public Class DSACryptoServiceProvider Inherits DSA [C#] public sealed class DSACryptoServiceProvider : DSA [C++] public __gc __sealed class DSACryptoServiceProvider : public DSA [JScript] public class DSACryptoServiceProvider extends DSA
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
Digital signatures help to authenticate the identity of another entity and help to protect the integrity of data. For example, to use a public-key system to digitally sign a message, the sender first applies a hash function to the message to create a message digest. The sender then encrypts the message digest with the sender's private key to create the sender's personal signature, because this private key uniquely identifies the sender. Upon receiving the message and signature, the receiver decrypts the signature using the sender's public key to recover the message digest and hashes the message using the same hash algorithm that the sender used. If the message digest that the receiver computes exactly matches the message digest received from the sender, the receiver can assume that the message came from the sender. Note that a signature can be verified by anyone, because the sender's public key is common knowledge.
Example
[Visual Basic] Imports System Imports System.Security.Cryptography _ Class DSACSPSample Shared Sub Main() Try 'Create a new instance of DSACryptoServiceProvider to generate 'a new key pair. Dim DSA As New DSACryptoServiceProvider() 'The hash value to sign. Dim HashValue As Byte() = {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. Dim SignedHashValue As Byte() = DSASignHash(HashValue, DSA.ExportParameters(True), "SHA1") 'Verify the hash and display the results. If DSAVerifyHash(HashValue, SignedHashValue, DSA.ExportParameters(False), "SHA1") Then Console.WriteLine("The hash value was verified.") Else Console.WriteLine("The hash value was not verified.") End If Catch e As ArgumentNullException Console.WriteLine(e.Message) End Try End Sub Public Shared Function DSASignHash(ByVal HashToSign() As Byte, ByVal DSAKeyInfo As DSAParameters, ByVal HashAlg As String) As Byte() Try 'Create a new instance of DSACryptoServiceProvider. Dim DSA As New DSACryptoServiceProvider() 'Import the key information. DSA.ImportParameters(DSAKeyInfo) 'Create an DSASignatureFormatter object and pass it the 'DSACryptoServiceProvider to transfer the private key. Dim DSAFormatter As New DSASignatureFormatter(DSA) 'Set the hash algorithm to the passed value. DSAFormatter.SetHashAlgorithm(HashAlg) 'Create a signature for HashValue and return it. Return DSAFormatter.CreateSignature(HashToSign) Catch e As CryptographicException Console.WriteLine(e.Message) Return Nothing End Try End Function Public Shared Function DSAVerifyHash(ByVal HashValue() As Byte, ByVal SignedHashValue() As Byte, ByVal DSAKeyInfo As DSAParameters, ByVal HashAlg As String) As Boolean Try 'Create a new instance of DSACryptoServiceProvider. Dim DSA As New DSACryptoServiceProvider() 'Import the key information. DSA.ImportParameters(DSAKeyInfo) 'Create an DSASignatureDeformatter object and pass it the 'DSACryptoServiceProvider to transfer the private key. Dim DSADeformatter As New DSASignatureDeformatter(DSA) 'Set the hash algorithm to the passed value. DSADeformatter.SetHashAlgorithm(HashAlg) 'Verify signature and return the result. Return DSADeformatter.VerifySignature(HashValue, SignedHashValue) Catch e As CryptographicException Console.WriteLine(e.Message) Return False End Try End Function End Class [C#] 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 hash value to sign. byte[] HashValue = {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[] SignedHashValue = DSASignHash(HashValue, DSA.ExportParameters(true), "SHA1"); //Verify the hash and display the results. if(DSAVerifyHash(HashValue, SignedHashValue, DSA.ExportParameters(false), "SHA1")) { 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[] DSASignHash(byte[] HashToSign, DSAParameters DSAKeyInfo, string HashAlg) { try { //Create a new instance of DSACryptoServiceProvider. DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information. DSA.ImportParameters(DSAKeyInfo); //Create an DSASignatureFormatter object and pass it the //DSACryptoServiceProvider to transfer the private key. DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSA); //Set the hash algorithm to the passed value. DSAFormatter.SetHashAlgorithm(HashAlg); //Create a signature for HashValue and return it. return DSAFormatter.CreateSignature(HashToSign); } catch(CryptographicException e) { Console.WriteLine(e.Message); return null; } } public static bool DSAVerifyHash(byte[] HashValue, byte[] SignedHashValue, DSAParameters DSAKeyInfo, string HashAlg) { try { //Create a new instance of DSACryptoServiceProvider. DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(); //Import the key information. DSA.ImportParameters(DSAKeyInfo); //Create an DSASignatureDeformatter object and pass it the //DSACryptoServiceProvider to transfer the private key. DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSA); //Set the hash algorithm to the passed value. DSADeformatter.SetHashAlgorithm(HashAlg); //Verify signature and return the result. return DSADeformatter.VerifySignature(HashValue, SignedHashValue); } catch(CryptographicException e) { Console.WriteLine(e.Message); return false; } } } [C++] #using <mscorlib.dll> #using <System.dll> using namespace System; using namespace System::Security::Cryptography; Byte DSASignHash(Byte HashToSign[], DSAParameters DSAKeyInfo, String* HashAlg) [] { try { //Create a new instance of DSACryptoServiceProvider. DSACryptoServiceProvider* DSA = new DSACryptoServiceProvider(); //Import the key information. DSA->ImportParameters(DSAKeyInfo); //Create an DSASignatureFormatter object and pass it the //DSACryptoServiceProvider to transfer the private key. DSASignatureFormatter* DSAFormatter = new DSASignatureFormatter(DSA); //Set the hash algorithm to the passed value. DSAFormatter->SetHashAlgorithm(HashAlg); //Create a signature for HashValue and return it. return DSAFormatter->CreateSignature(HashToSign); } catch (CryptographicException* e) { Console::WriteLine(e->Message); return 0; } } bool DSAVerifyHash(Byte HashValue[], Byte SignedHashValue[], DSAParameters DSAKeyInfo, String* HashAlg) { try { //Create a new instance of DSACryptoServiceProvider. DSACryptoServiceProvider* DSA = new DSACryptoServiceProvider(); //Import the key information. DSA->ImportParameters(DSAKeyInfo); //Create an DSASignatureDeformatter Object* and pass it the //DSACryptoServiceProvider to transfer the private key. DSASignatureDeformatter* DSADeformatter = new DSASignatureDeformatter(DSA); //Set the hash algorithm to the passed value. DSADeformatter->SetHashAlgorithm(HashAlg); //Verify signature and return the result. return DSADeformatter->VerifySignature(HashValue, SignedHashValue); } catch (CryptographicException* e) { Console::WriteLine(e->Message); return false; } } int main() { try { //Create a new instance of DSACryptoServiceProvider to generate //a new key pair. DSACryptoServiceProvider* DSA = new DSACryptoServiceProvider(); //The hash value to sign. Byte HashValue[] = {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 SignedHashValue[] = DSASignHash(HashValue, DSA->ExportParameters(true), S"SHA1"); //Verify the hash and display the results. if (DSAVerifyHash(HashValue, SignedHashValue, DSA->ExportParameters(false), S"SHA1")) { Console::WriteLine(S"The hash value was verified."); } else { Console::WriteLine(S"The hash value was not verified."); } } catch (ArgumentNullException* e) { Console::WriteLine(e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Security.Cryptography
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: Mscorlib (in Mscorlib.dll)
See Also
DSACryptoServiceProvider Members | System.Security.Cryptography Namespace | Cryptographic Services