DSACryptoServiceProvider クラス

DSA アルゴリズムの暗号サービス プロバイダ (CSP: Cryptographic Service Provider) 実装にアクセスするためのラッパー オブジェクトを定義します。

この型のすべてのメンバの一覧については、DSACryptoServiceProvider メンバ を参照してください。

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

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

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

デジタル署名は、別のエンティティの ID を認証し、データの整合性を保護するために使用されます。たとえば、公開キー システムを使用してメッセージにデジタル署名を行うには、送信者は最初にメッセージにハッシュ関数を適用して、メッセージのダイジェストを作成します。次に、送信者は、自身の秘密キーでメッセージ ダイジェストを暗号化し、送信者の個人的な署名を作成します。秘密キーは送信者を一意に識別します。受信者は、メッセージと署名を受け取ると、送信者の公開キーを使用して署名を解読してメッセージ ダイジェストを復元し、送信者が使用したものと同じハッシュ アルゴリズムを使用してメッセージをハッシュします。受信者が計算したメッセージ ダイジェストが送信者から受信したメッセージ ダイジェストと完全に一致する場合、受信者はそのメッセージが送信者から送られたものであると見なすことができます。送信者の公開キーは公開されているため、その署名はだれもが検証できることに注意してください。

使用例

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Security.Cryptography

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

DSACryptoServiceProvider メンバ | System.Security.Cryptography 名前空間 | 暗号サービス