クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
DSACryptoServiceProvider クラス

  低帯域幅での表示をオンにする
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
DSACryptoServiceProvider クラス

更新 : 2007 年 11 月

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

名前空間 :  System.Security.Cryptography
アセンブリ :  mscorlib (mscorlib.dll 内)

Visual Basic (宣言)
<ComVisibleAttribute(True)> _
Public NotInheritable Class DSACryptoServiceProvider _
    Inherits DSA _
    Implements ICspAsymmetricAlgorithm
Visual Basic (使用法)
Dim instance As DSACryptoServiceProvider
C#
[ComVisibleAttribute(true)]
public sealed class DSACryptoServiceProvider : DSA, 
    ICspAsymmetricAlgorithm
Visual C++
[ComVisibleAttribute(true)]
public ref class DSACryptoServiceProvider sealed : public DSA, 
    ICspAsymmetricAlgorithm
J#
/** @attribute ComVisibleAttribute(true) */
public final class DSACryptoServiceProvider extends DSA implements ICspAsymmetricAlgorithm
JScript
public final class DSACryptoServiceProvider extends DSA implements ICspAsymmetricAlgorithm

DSACryptoServiceProvider クラスを使用すると、デジタル署名を作成し、データの整合性を保護できます。

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

このアルゴリズムは、512 ビットから 1024 ビットのキー長を 64 ビット単位でサポートしています。

DSACryptoServiceProvider クラスを使用してハッシュ値のデジタル署名を作成し、署名を検証するコード例を次に示します。

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

}

Visual C++
#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 );
   }

}


J#
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
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

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

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。

.NET Framework

サポート対象 : 3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 3.5、2.0
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件  |  商標  |  プライバシー
Page view tracker