キーのサイズを指定して、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 のセキュリティ
- SecurityPermission (アンマネージ コードを呼び出すために必要なアクセス許可)。SecurityPermissionFlag.UnmanagedCode (関連する列挙体)。
プラットフォーム
Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0
.NET Compact Framework
サポート対象 : 2.0
参照
関連項目
DSACryptoServiceProvider クラス
DSACryptoServiceProvider メンバ
System.Security.Cryptography 名前空間