DSACryptoServiceProviderとは? わかりやすく解説

DSACryptoServiceProvider クラス

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

名前空間: 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 クラスを使用してハッシュ値のデジタル署名を作成し、署名を検証するコード例を次に示します。

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) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

DSACryptoServiceProvider コンストラクタ ()

DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

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

Dim instance As New DSACryptoServiceProvider
public DSACryptoServiceProvider ()
public:
DSACryptoServiceProvider ()
public DSACryptoServiceProvider ()
public function DSACryptoServiceProvider ()
使用例使用例

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
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DSACryptoServiceProvider クラス
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

DSACryptoServiceProvider コンストラクタ (Int32, CspParameters)

キー サイズおよび暗号サービス プロバイダ (CSP) のパラメータを指定して、DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

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

Public Sub New ( _
    dwKeySize As Integer, _
    parameters As CspParameters _
)
Dim dwKeySize As Integer
Dim parameters As CspParameters

Dim instance As New DSACryptoServiceProvider(dwKeySize,
 parameters)
public DSACryptoServiceProvider (
    int dwKeySize,
    CspParameters parameters
)
public:
DSACryptoServiceProvider (
    int dwKeySize, 
    CspParameters^ parameters
)
public DSACryptoServiceProvider (
    int dwKeySize, 
    CspParameters parameters
)
public function DSACryptoServiceProvider (
    dwKeySize : int, 
    parameters : CspParameters
)

パラメータ

dwKeySize

暗号アルゴリズムで使用されるキーのサイズ (ビット単位)。

parameters

CSP のパラメータ。

例外例外
例外の種類条件

CryptographicException

CSP を取得できません。

または

キーを作成できません。

使用例使用例

DSACryptoServiceProvider を作成し、新しいキー ペアを生成して、このキー ペアをキー コンテナ内で永続化するコード例を次に示します。

Imports System.Security.Cryptography

Module DSACSPExample

    Sub Main()
        Try
            Dim KeyContainerName As String
 = "MyKeyContainer"

            'Create a new key and persist it in 
            'the key container.
            DSAPersistKeyInCSP(KeyContainerName)

            '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, KeyContainerName, "SHA1")

            'Verify the hash and display the results.
            If DSAVerifyHash(HashValue, SignedHashValue, KeyContainerName,
 "SHA1") Then
                Console.WriteLine("The hash value was verified.")
            Else
                Console.WriteLine("The hash value was not verified.")
            End If

            'Delete 
            DSADeleteKeyInCSP(KeyContainerName)


        Catch e As ArgumentNullException
            Console.WriteLine(e.Message)
        End Try

    End Sub

    Sub DSAPersistKeyInCSP(ByVal ContainerName
 As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider to generate
            'a new key pair.  Pass a key size of 1024. Pass the CspParameters
 
            'class to persist the key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(1024, cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the
 container, ""{0}"".",
 ContainerName)

            'Indicate the key size.
            Console.WriteLine("The key size is: {0}.",
 DSAalg.KeySize)

        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub DSADeleteKeyInCSP(ByVal ContainerName
 As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider. 
            'Pass the CspParameters class to use the 
            'key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Delete the key entry in the container.
            DSAalg.PersistKeyInCsp = False

            'Call Clear to release resources and delete the key from
 the container.
            DSAalg.Clear()

            'Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the
 container, ""{0}"".",
 ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Function DSASignHash(ByVal HashToSign()
 As Byte, ByVal ContainerName
 As String, ByVal HashAlg As String) As Byte()
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Create an DSASignatureFormatter object and pass it the
 
            'DSACryptoServiceProvider to transfer the private key.
            Dim DSAFormatter As New
 DSASignatureFormatter(DSAalg)

            '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


    Function DSAVerifyHash(ByVal HashValue()
 As Byte, ByVal SignedHashValue()
 As Byte, ByVal ContainerName As String, ByVal HashAlg
 As String) As Boolean
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Create an DSASignatureDeformatter object and pass it the
 
            'DSACryptoServiceProvider to transfer the private key.
            Dim DSADeformatter As New
 DSASignatureDeformatter(DSAalg)

            '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 Module
using System;
using System.Security.Cryptography;

class DSACSPExample
{
    static void Main()
    {
        try
        {
            string KeyContainerName = "MyKeyContainer";

            //Create a new key and persist it in 
            //the key container.
            DSAPersistKeyInCSP(KeyContainerName);

            //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, KeyContainerName, "SHA1");

            //Verify the hash and display the results.
            if(DSAVerifyHash(HashValue, SignedHashValue, KeyContainerName,
 "SHA1"))
            {
                Console.WriteLine("The hash value was verified.");
            }
            else
            {
                Console.WriteLine("The hash value was not verified.");
            }

            //Delete 
            DSADeleteKeyInCSP(KeyContainerName);


        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void DSAPersistKeyInCSP(string
 ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.  Pass a key size of 1024. Pass the CspParameters
 
            //class to persist the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(1024,
 cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in
 the container, \"{0}\".", ContainerName);
      
            //Indicate the key size.
            Console.WriteLine("The key size is: {0}.", DSAalg.KeySize);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
    }

    public static void DSADeleteKeyInCSP(string
 ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Delete the key entry in the container.
            DSAalg.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from
 the container.
            DSAalg.Clear();

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the container, \"{0}\".",
 ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
    }

    public static byte[] DSASignHash(byte[]
 HashToSign, string ContainerName, string
 HashAlg)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Create an DSASignatureFormatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSAalg);

            //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, string ContainerName, string
 HashAlg)
    {
        try
        {   
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Create an DSASignatureDeformatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSAalg);
                
            //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 namespace System;
using namespace System::Security::Cryptography;
void DSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider to generate
      //a new key pair.  Pass a key size of 1024. Pass the CspParameters
 
      //class to persist the key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( 1024,cspParams
 );
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The DSA key was persisted in the
 container, \"{0}\".", ContainerName );
      
      //Indicate the key size.
      Console::WriteLine( "The key size is: {0}.", DSAalg->KeySize );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void DSADeleteKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider. 
      //Pass the CspParameters class to use the 
      //key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Delete the key entry in the container.
      DSAalg->PersistKeyInCsp = false;
      
      //Call Clear to release resources and delete the key from the
 container.
      DSAalg->Clear();
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The DSA key was deleted from the container, \"{0}\".",
 ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

array<Byte>^ DSASignHash( array<Byte>^HashToSign, String^ ContainerName,
 String^ HashAlg )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Create an DSASignatureFormatter object and pass it the 
      //DSACryptoServiceProvider to transfer the private key.
      DSASignatureFormatter^ DSAFormatter = gcnew DSASignatureFormatter( DSAalg );
      
      //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,
 String^ ContainerName, String^ HashAlg )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Create an DSASignatureDeformatter object and pass it the 
      //DSACryptoServiceProvider to transfer the private key.
      DSASignatureDeformatter^ DSADeformatter = gcnew DSASignatureDeformatter( DSAalg
 );
      
      //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
   {
      String^ KeyContainerName = "MyKeyContainer";
      
      //Create a new key and persist it in 
      //the key container.
      DSAPersistKeyInCSP( KeyContainerName );
      
      //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, KeyContainerName,
 "SHA1" );
      
      //Verify the hash and display the results.
      if ( DSAVerifyHash( HashValue, SignedHashValue, KeyContainerName,
 "SHA1" ) )
      {
         Console::WriteLine( "The hash value was verified." );
      }
      else
      {
         Console::WriteLine( "The hash value was not verified." );
      }
      
      //Delete 
      DSADeleteKeyInCSP( KeyContainerName );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

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

class DSACSPExample
{
    public static void main(String
 args[])
    {
        try {
            String keyContainerName = "MyKeyContainer";
            //Create a new key and persist it in 
            //the key container.
            DSAPersistKeyInCSP(keyContainerName);
            //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, keyContainerName,
                "SHA1");
            //Verify the hash and display the results.
            if (DSAVerifyHash(hashValue, signedHashValue, keyContainerName,
 
                "SHA1")) {
                Console.WriteLine("The hash value was verified.");
            }
            else {
                Console.WriteLine("The hash value was not verified.");
            }
            //Delete 
            DSADeleteKeyInCSP(keyContainerName);
        }
        catch (ArgumentNullException e) {
            Console.WriteLine(e.get_Message());
        }
    } //main

    public static void DSAPersistKeyInCSP(String
 containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.  Pass a key size of 1024. Pass the CspParameters
 
            //class to persist the key in the container.
            DSACryptoServiceProvider dsaAlg = new DSACryptoServiceProvider(1024
,
                cspParams);
            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in
 the container," 
                + " \"{0}\".", containerName);
            //Indicate the key size.
            Console.WriteLine("The key size is: {0}.", System.Convert.ToString(
                dsaAlg.get_KeySize()));
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //DSAPersistKeyInCSP

    public static void DSADeleteKeyInCSP(String
 containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Delete the key entry in the container.
            dsaAlg.set_PersistKeyInCsp(false);
            //Call Clear to release resources and delete the key from
 the 
            //container.
            dsaAlg.Clear();
            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the container, "
 
                + "\"{0}\".", containerName);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //DSADeleteKeyInCSP

    public static ubyte[] DSASignHash(ubyte
 hashToSign[], String containerName,
        String hashAlg)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Create an DSASignatureFormatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureFormatter dsaFormatter = 
                new DSASignatureFormatter(dsaAlg);
            //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[], String containerName, String hashAlg)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Create an DSASignatureDeformatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter dsaDeformatter = 
                new DSASignatureDeformatter(dsaAlg);
            //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
} //DSACSPExample
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DSACryptoServiceProvider クラス
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

DSACryptoServiceProvider コンストラクタ (CspParameters)

暗号サービス プロバイダ (CSP) のパラメータを指定して、DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

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

Public Sub New ( _
    parameters As CspParameters _
)
Dim parameters As CspParameters

Dim instance As New DSACryptoServiceProvider(parameters)
public DSACryptoServiceProvider (
    CspParameters parameters
)
public:
DSACryptoServiceProvider (
    CspParameters^ parameters
)
public DSACryptoServiceProvider (
    CspParameters parameters
)
public function DSACryptoServiceProvider (
    parameters : CspParameters
)

パラメータ

parameters

CSP のパラメータ。

使用例使用例

DSACryptoServiceProvider を作成し、新しいキー ペアを生成して、このキー ペアをキー コンテナ内で永続化するコード例を次に示します。

Imports System.Security.Cryptography

Module DSACSPExample

    Sub Main()
        Try
            Dim KeyContainerName As String
 = "MyKeyContainer"

            'Create a new key and persist it in 
            'the key container.
            DSAPersistKeyInCSP(KeyContainerName)

            '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, KeyContainerName, "SHA1")

            'Verify the hash and display the results.
            If DSAVerifyHash(HashValue, SignedHashValue, KeyContainerName,
 "SHA1") Then
                Console.WriteLine("The hash value was verified.")
            Else
                Console.WriteLine("The hash value was not verified.")
            End If

            'Delete 
            DSADeleteKeyInCSP(KeyContainerName)


        Catch e As ArgumentNullException
            Console.WriteLine(e.Message)
        End Try

    End Sub

    Sub DSAPersistKeyInCSP(ByVal ContainerName
 As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider to generate
            'a new key pair.  Pass the CspParameters class to persist
 the 
            'key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the
 container, ""{0}"".",
 ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub DSADeleteKeyInCSP(ByVal ContainerName
 As String)
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider. 
            'Pass the CspParameters class to use the 
            'key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Delete the key entry in the container.
            DSAalg.PersistKeyInCsp = False

            'Call Clear to release resources and delete the key from
 the container.
            DSAalg.Clear()

            'Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the
 container, ""{0}"".",
 ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Function DSASignHash(ByVal HashToSign()
 As Byte, ByVal ContainerName
 As String, ByVal HashAlg As String) As Byte()
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Create an DSASignatureFormatter object and pass it the
 
            'DSACryptoServiceProvider to transfer the private key.
            Dim DSAFormatter As New
 DSASignatureFormatter(DSAalg)

            '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


    Function DSAVerifyHash(ByVal HashValue()
 As Byte, ByVal SignedHashValue()
 As Byte, ByVal ContainerName As String, ByVal HashAlg
 As String) As Boolean
        Try
            ' Create a new instance of CspParameters.  Pass
            ' 13 to specify a DSA container or 1 to specify
            ' an RSA container.  The default is 1.
            Dim cspParams As New
 CspParameters(13)

            ' Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName

            'Create a new instance of DSACryptoServiceProvider.
            'Pass the CspParameters class to use the key 
            'from the key in the container.
            Dim DSAalg As New
 DSACryptoServiceProvider(cspParams)

            'Create an DSASignatureDeformatter object and pass it the
 
            'DSACryptoServiceProvider to transfer the private key.
            Dim DSADeformatter As New
 DSASignatureDeformatter(DSAalg)

            '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 Module
using System;
using System.Security.Cryptography;

class DSACSPExample
{
    static void Main()
    {
        try
        {
            string KeyContainerName = "MyKeyContainer";

            //Create a new key and persist it in 
            //the key container.
            DSAPersistKeyInCSP(KeyContainerName);

            //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, KeyContainerName, "SHA1");

            //Verify the hash and display the results.
            if(DSAVerifyHash(HashValue, SignedHashValue, KeyContainerName,
 "SHA1"))
            {
                Console.WriteLine("The hash value was verified.");
            }
            else
            {
                Console.WriteLine("The hash value was not verified.");
            }

            //Delete 
            DSADeleteKeyInCSP(KeyContainerName);


        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public static void DSAPersistKeyInCSP(string
 ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.  Pass the CspParameters class to persist
 the 
            //key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in
 the container, \"{0}\".", ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
    }

    public static void DSADeleteKeyInCSP(string
 ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Delete the key entry in the container.
            DSAalg.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from
 the container.
            DSAalg.Clear();

            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the container, \"{0}\".",
 ContainerName);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

        }
    }

    public static byte[] DSASignHash(byte[]
 HashToSign, string ContainerName, string
 HashAlg)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Create an DSASignatureFormatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSAalg);

            //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, string ContainerName, string
 HashAlg)
    {
        try
        {   
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

            //Create an DSASignatureDeformatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSAalg);
                
            //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 namespace System;
using namespace System::Security::Cryptography;
void DSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider to generate
      //a new key pair.  Pass the CspParameters class to persist the
 
      //key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The DSA key was persisted in the
 container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void DSADeleteKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider. 
      //Pass the CspParameters class to use the 
      //key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Delete the key entry in the container.
      DSAalg->PersistKeyInCsp = false;
      
      //Call Clear to release resources and delete the key from the
 container.
      DSAalg->Clear();
      
      //Indicate that the key was persisted.
      Console::WriteLine( "The DSA key was deleted from the container, \"{0}\".",
 ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

array<Byte>^ DSASignHash( array<Byte>^HashToSign, String^ ContainerName,
 String^ HashAlg )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Create an DSASignatureFormatter object and pass it the 
      //DSACryptoServiceProvider to transfer the private key.
      DSASignatureFormatter^ DSAFormatter = gcnew DSASignatureFormatter( DSAalg );
      
      //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,
 String^ ContainerName, String^ HashAlg )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters( 13 );
      
      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;
      
      //Create a new instance of DSACryptoServiceProvider.
      //Pass the CspParameters class to use the key 
      //from the key in the container.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams
 );
      
      //Create an DSASignatureDeformatter object and pass it the 
      //DSACryptoServiceProvider to transfer the private key.
      DSASignatureDeformatter^ DSADeformatter = gcnew DSASignatureDeformatter( DSAalg
 );
      
      //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
   {
      String^ KeyContainerName = "MyKeyContainer";
      
      //Create a new key and persist it in 
      //the key container.
      DSAPersistKeyInCSP( KeyContainerName );
      
      //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, KeyContainerName,
 "SHA1" );
      
      //Verify the hash and display the results.
      if ( DSAVerifyHash( HashValue, SignedHashValue, KeyContainerName,
 "SHA1" ) )
      {
         Console::WriteLine( "The hash value was verified." );
      }
      else
      {
         Console::WriteLine( "The hash value was not verified." );
      }
      
      //Delete 
      DSADeleteKeyInCSP( KeyContainerName );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

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

class DSACSPExample
{
    public static void main(String
 args[])
    {
        try {
            String keyContainerName = "MyKeyContainer";
            //Create a new key and persist it in 
            //the key container.
            DSAPersistKeyInCSP(keyContainerName);
            //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, keyContainerName, 
                "SHA1");
            //Verify the hash and display the results.
            if (DSAVerifyHash(hashValue, signedHashValue, keyContainerName,
 
                "SHA1")) {
                Console.WriteLine("The hash value was verified.");
            }
            else {
                Console.WriteLine("The hash value was not verified.");
            }
            //Delete 
            DSADeleteKeyInCSP(keyContainerName);
        }
        catch (ArgumentNullException e) {
            Console.WriteLine(e.get_Message());
        }
    } //main

    public static void DSAPersistKeyInCSP(String
 containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.  Pass the CspParameters class to persist
 the 
            //key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in
 the container, " 
                + "\"{0}\".", containerName);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //DSAPersistKeyInCSP

    public static void DSADeleteKeyInCSP(String
 containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Delete the key entry in the container.
            dsaAlg.set_PersistKeyInCsp(false);
            //Call Clear to release resources and delete the key from
 
            // the container.
            dsaAlg.Clear();
            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the container, "
 
                + "\"{0}\".", containerName);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //DSADeleteKeyInCSP

    public static ubyte[] DSASignHash(ubyte
 hashToSign[], String containerName, 
        String hashAlg)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Create an DSASignatureFormatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureFormatter dsaFormatter = 
                new DSASignatureFormatter(dsaAlg);
            //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[], String containerName, String hashAlg)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters(13);
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = containerName;
            //Create a new instance of DSACryptoServiceProvider.
            //Pass the CspParameters class to use the key 
            //from the key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Create an DSASignatureDeformatter object and pass it the
 
            //DSACryptoServiceProvider to transfer the private key.
            DSASignatureDeformatter dsaDeformatter = 
                new DSASignatureDeformatter(dsaAlg);
            //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
} //DSACSPExample
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DSACryptoServiceProvider クラス
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

DSACryptoServiceProvider コンストラクタ

DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
DSACryptoServiceProvider () DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

.NET Compact Framework によってサポートされています。

DSACryptoServiceProvider (CspParameters) 暗号サービス プロバイダ (CSP) のパラメータを指定して、DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

.NET Compact Framework によってサポートされています。

DSACryptoServiceProvider (Int32) キーのサイズを指定して、DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

.NET Compact Framework によってサポートされています。

DSACryptoServiceProvider (Int32, CspParameters) キー サイズおよび暗号サービス プロバイダ (CSP) のパラメータを指定して、DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

.NET Compact Framework によってサポートされています。

参照参照

関連項目

DSACryptoServiceProvider クラス
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス

DSACryptoServiceProvider コンストラクタ (Int32)

キーのサイズを指定して、DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。

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

Public Sub New ( _
    dwKeySize As Integer _
)
Dim dwKeySize As Integer

Dim instance As New DSACryptoServiceProvider(dwKeySize)
public DSACryptoServiceProvider (
    int dwKeySize
)
public:
DSACryptoServiceProvider (
    int dwKeySize
)
public DSACryptoServiceProvider (
    int dwKeySize
)
public function DSACryptoServiceProvider (
    dwKeySize : int
)

パラメータ

dwKeySize

非対称アルゴリズムで使用されるキーのサイズ (ビット単位)。

使用例使用例

DSACryptoServiceProvider を作成し、新しいキー ペアを生成して、このキー ペアをキー コンテナ内で永続化するコード例を次に示します。

Imports System.Security.Cryptography

Module DSACSPExample

    Sub Main()
        Try
            'Create a new instance of DSACryptoServiceProvider to generate
            'a new key pair.  This constructor specifies that the key-length
            'will be 1024.
            Dim DSAalg As New
 DSACryptoServiceProvider(1024)

            '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, DSAalg.ExportParameters(True), "SHA1")

            'Verify the hash and display the results.
            If DSAVerifyHash(HashValue, SignedHashValue, DSAalg.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

    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


    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 Module
using System;
using System.Security.Cryptography;

class DSACSPExample
{
    static void Main()
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.  This constructor specifies that the key-length
            //will be 1024.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(1024);

            //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, DSAalg.ExportParameters(true),
 "SHA1");

            //Verify the hash and display the results.
            if(DSAVerifyHash(HashValue, SignedHashValue, DSAalg.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 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.  This constructor specifies that the key-length
      //will be 1024.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( 1024 );
      
      //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, DSAalg->ExportParameters(
 true ), "SHA1" );
      
      //Verify the hash and display the results.
      if ( DSAVerifyHash( HashValue, SignedHashValue, DSAalg->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 DSACSPExample
{
    public static void main(String
 args[])
    {
        try {
            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.  This constructor specifies that the key-length
            //will be 1024.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(1024);
            //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, 
                    dsaAlg.ExportParameters(true), "SHA1");
            //Verify the hash and display the results.
            if (DSAVerifyHash(hashValue, signedHashValue, 
                dsaAlg.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
} //DSACSPExample
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DSACryptoServiceProvider クラス
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

DSACryptoServiceProvider フィールド


プロテクト フィールドプロテクト フィールド

参照参照

関連項目

DSACryptoServiceProvider クラス
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス

DSACryptoServiceProvider プロパティ


DSACryptoServiceProvider メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Clear  AsymmetricAlgorithm クラスによって使用されているすべてのリソースを解放します。 ( AsymmetricAlgorithm から継承されます。)
パブリック メソッド Create  オーバーロードされます。 非対称アルゴリズムを実行するために使用する暗号オブジェクトを作成します。 ( DSA から継承されます。)
パブリック メソッド CreateSignature オーバーライドされます。 指定したデータに対する DSA 署名を作成します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ExportCspBlob DSACryptoServiceProvider オブジェクトに関連付けられたキー情報を含む BLOB をエクスポートします。
パブリック メソッド ExportParameters オーバーライドされます。 DSAParameters をエクスポートします。
パブリック メソッド FromXmlString  XML 文字列から DSA オブジェクトを再構築します。 ( DSA から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド ImportCspBlob DSA キー情報を表す BLOB をインポートします。
パブリック メソッド ImportParameters オーバーライドされます。 指定した DSAParameters をインポートします。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド SignData オーバーロードされます。 指定したデータのハッシュ値を計算し、算出された値に署名します。
パブリック メソッド SignHash 指定したハッシュ値を秘密キーで暗号化することにより、そのハッシュ値の署名を計算します。
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
パブリック メソッド ToXmlString  現在の DSA オブジェクトの XML 文字列形式を作成して返します。 ( DSA から継承されます。)
パブリック メソッド VerifyData 指定した署名データを、指定したデータに対して計算された署名と比較することによって検証します。
パブリック メソッド VerifyHash 指定した署名データを、指定したハッシュ値に対して計算された署名と比較することによって検証します。
パブリック メソッド VerifySignature オーバーライドされます。 指定したデータに対する DSA 署名を検証します。
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DSACryptoServiceProvider クラス
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス

DSACryptoServiceProvider メンバ

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

DSACryptoServiceProvider データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド DSACryptoServiceProvider オーバーロードされます。 DSACryptoServiceProvider クラスの新しいインスタンスを初期化します。
プロテクト フィールドプロテクト フィールド
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Clear  AsymmetricAlgorithm クラスによって使用されているすべてのリソースを解放します。 (AsymmetricAlgorithm から継承されます。)
パブリック メソッド Create  オーバーロードされます。 非対称アルゴリズムを実行するために使用する暗号オブジェクトを作成します。 (DSA から継承されます。)
パブリック メソッド CreateSignature オーバーライドされます。 指定したデータに対する DSA 署名を作成します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド ExportCspBlob DSACryptoServiceProvider オブジェクトに関連付けられたキー情報を含む BLOB をエクスポートします。
パブリック メソッド ExportParameters オーバーライドされます。 DSAParameters をエクスポートします。
パブリック メソッド FromXmlString  XML 文字列から DSA オブジェクトを再構築します。 (DSA から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド ImportCspBlob DSA キー情報を表す BLOB をインポートします。
パブリック メソッド ImportParameters オーバーライドされます。 指定した DSAParameters をインポートします。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド SignData オーバーロードされます。 指定したデータのハッシュ値を計算し、算出された値に署名します。
パブリック メソッド SignHash 指定したハッシュ値を秘密キーで暗号化することにより、そのハッシュ値の署名を計算します。
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
パブリック メソッド ToXmlString  現在の DSA オブジェクトの XML 文字列形式を作成して返します。 (DSA から継承されます。)
パブリック メソッド VerifyData 指定した署名データを、指定したデータに対して計算された署名と比較することによって検証します。
パブリック メソッド VerifyHash 指定した署名データを、指定したハッシュ値に対して計算された署名と比較することによって検証します。
パブリック メソッド VerifySignature オーバーライドされます。 指定したデータに対する DSA 署名を検証します。
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

DSACryptoServiceProvider クラス
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「DSACryptoServiceProvider」の関連用語

DSACryptoServiceProviderのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DSACryptoServiceProviderのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS