다음을 통해 공유


DSACryptoServiceProvider 클래스

래퍼 개체를 정의하여 DSA 알고리즘의 CSP(암호화 서비스 공급자) 구현에 액세스합니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Security.Cryptography
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
Public NotInheritable Class DSACryptoServiceProvider
    Inherits DSA
    Implements ICspAsymmetricAlgorithm
‘사용 방법
Dim instance As DSACryptoServiceProvider
[ComVisibleAttribute(true)] 
public sealed class DSACryptoServiceProvider : DSA, ICspAsymmetricAlgorithm
[ComVisibleAttribute(true)] 
public ref class DSACryptoServiceProvider sealed : public DSA, ICspAsymmetricAlgorithm
/** @attribute ComVisibleAttribute(true) */ 
public final class DSACryptoServiceProvider extends DSA implements ICspAsymmetricAlgorithm
ComVisibleAttribute(true) 
public final class DSACryptoServiceProvider extends DSA implements ICspAsymmetricAlgorithm

설명

DSACryptoServiceProvider 클래스를 사용하여 디지털 서명을 만들고 데이터 무결성을 보호할 수 있습니다.

공개 키 시스템을 사용하여 메시지를 디지털 서명하려는 경우 송신자는 먼저 메시지에 해시 함수를 적용하여 메시지 다이제스트를 만듭니다. 그런 다음 송신자의 개인 키로 메시지 다이제스트를 암호화하여 송신자의 개인 서명을 만듭니다. 수신자는 메시지와 서명을 받으면 송신자의 공개 키로 서명을 해독하여 메시지 다이제스트를 복구하고 송신자가 사용한 것과 동일한 해시 알고리즘을 사용하여 메시지를 해시합니다. 수신자가 계산한 메시지 다이제스트가 송신자로부터 받은 메시지 다이제스트와 정확히 일치하면 수신자는 메시지가 전송되는 동안 변경되지 않은 것으로 확인할 수 있습니다. 송신자의 공개 키는 누구나 알 수 있으므로 누구든지 서명을 확인할 수 있습니다.

이 알고리즘에서는 한 번에 64비트씩 증가하는 512비트에서 1024비트까지의 키 길이를 지원합니다.

예제

다음 코드 예제에서는 DSACryptoServiceProvider 클래스를 사용하여 해시 값의 디지털 서명을 만든 다음 서명을 확인합니다.

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
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;
        }
            
    }

}
#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
array<Byte>^ DSASignHash( array<Byte>^HashToSign, DSAParameters DSAKeyInfo, String^ HashAlg )
{
   try
   {
      
      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider;
      
      //Import the key information.   
      DSA->ImportParameters( DSAKeyInfo );
      
      //Create an DSASignatureFormatter object and pass it the 
      //DSACryptoServiceProvider to transfer the private key.
      DSASignatureFormatter^ DSAFormatter = gcnew 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 nullptr;
   }

}

bool DSAVerifyHash( array<Byte>^HashValue, array<Byte>^SignedHashValue, DSAParameters DSAKeyInfo, String^ HashAlg )
{
   try
   {
      
      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider;
      
      //Import the key information. 
      DSA->ImportParameters( DSAKeyInfo );
      
      //Create an DSASignatureDeformatter Object* and pass it the 
      //DSACryptoServiceProvider to transfer the private key.
      DSASignatureDeformatter^ DSADeformatter = gcnew 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 = gcnew DSACryptoServiceProvider;
      
      //The hash value to sign.
      array<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.
      array<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 );
   }

}
import System.*;
import System.Security.Cryptography.*;

class DSACSPSample
{
    public static void main(String[] args)
    {
        try {
            // Create a new instance of DSACryptoServiceProvider to generate
            // a new key pair.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            // The hash value to sign.
            ubyte 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.
            ubyte 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.get_Message());
        }
    } //main

    public static ubyte[] DSASignHash(ubyte 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.get_Message());
            return null ;
        }
    } //DSASignHash

    public static boolean DSAVerifyHash(ubyte hashValue[], 
        ubyte 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.get_Message());
            return false ;
        }
    } //DSAVerifyHash
} //DSACSPSample

상속 계층 구조

System.Object
   System.Security.Cryptography.AsymmetricAlgorithm
     System.Security.Cryptography.DSA
      System.Security.Cryptography.DSACryptoServiceProvider

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

DSACryptoServiceProvider 멤버
System.Security.Cryptography 네임스페이스

기타 리소스

암호화 서비스