次の方法で共有


DSACryptoServiceProvider.SignHash メソッド

指定したハッシュ値を秘密キーで暗号化することにより、そのハッシュ値の署名を計算します。

Public Function SignHash( _
   ByVal rgbHash() As Byte, _   ByVal str As String _) As Byte()
[C#]
public byte[] SignHash(byte[] rgbHash,stringstr);
[C++]
public: unsigned char SignHash(unsigned charrgbHash __gc[],String* str)  __gc[];
[JScript]
public function SignHash(
   rgbHash : Byte[],str : String) : Byte[];

パラメータ

  • rgbHash
    署名されるデータのハッシュ値。
  • str
    データのハッシュ値を作成するために使用するハッシュ アルゴリズムの名前。

戻り値

指定したハッシュ値に対する DSA 署名。

例外

例外の種類 条件
ArgumentNullException rgbHash パラメータが null 参照 (Visual Basic では Nothing) です。
CryptographicException 暗号サービス プロバイダ (CSP) を取得できません。

または

秘密キーがありません。

解説

このメソッドは、デジタル署名を作成します。作成した署名は VerifyHash メソッドを使用して検証されます。

DSA は、 SHA1 ハッシュ アルゴリズムを使用します。

使用例

 
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 to sign.
            Dim Hash As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}

            'Use the MapNameToOID method to get an OID 
            'for the SHA1 algorithm.
            Dim OID As String = CryptoConfig.MapNameToOID("SHA1")

            'The value to hold the signed hash.
            Dim SignedHashValue As Byte() = DSASignHash(Hash, DSA.ExportParameters(True), OID)

            'Verify the hash and display the results.
            If DSAVerifyHash(Hash, SignedHashValue, DSA.ExportParameters(False), OID) 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 HashOID As String) As Byte()
        Try
            'Create a new instance of DSACryptoServiceProvider.
            Dim DSA As New DSACryptoServiceProvider()

            'Import the key information.   
            DSA.ImportParameters(DSAKeyInfo)

            'Sign the hash and return it.
            Return DSA.SignHash(HashToSign, HashOID)
        Catch e As CryptographicException
            Console.WriteLine(e)

            Return Nothing
        End Try
    End Function


    Public Shared Function DSAVerifyHash(ByVal Hash() As Byte, ByVal SignedHash() As Byte, ByVal DSAKeyInfo As DSAParameters, ByVal HashOID As String) As Boolean
        Try
            'Create a new instance of DSACryptoServiceProvider.
            Dim DSA As New DSACryptoServiceProvider()

            'Import the key information. 
            DSA.ImportParameters(DSAKeyInfo)

            'Verify the signature and return the result.    
            Return DSA.VerifyHash(Hash, HashOID, SignedHash)

        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return False
        End Try
    End Function
End Class

[C#] 
using System;
using System.Security.Cryptography;

class DSACSPSample
{
        
    static void Main()
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider to generate
            //a new key pair.
            DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

            //The hash to sign.
            byte[] Hash = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
            
            //Use the MapNameToOID method to get an OID 
            //for the SHA1 algorithm.
            string OID = CryptoConfig.MapNameToOID("SHA1");
            
            //The value to hold the signed hash.
            byte[] SignedHashValue = DSASignHash(Hash, DSA.ExportParameters(true), OID);

            //Verify the hash and display the results.
            if(DSAVerifyHash(Hash, SignedHashValue, DSA.ExportParameters(false), OID))
            {
                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 HashOID)
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

            //Import the key information.   
            DSA.ImportParameters(DSAKeyInfo);

            //Sign the hash and return it.
            return DSA.SignHash(HashToSign, HashOID);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e);

            return null;
        }

    }

    public static bool DSAVerifyHash(byte[] Hash, byte[] SignedHash, DSAParameters DSAKeyInfo, string HashOID)
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

            //Import the key information. 
            DSA.ImportParameters(DSAKeyInfo);

            //Verify the signature and return the result.    
            return DSA.VerifyHash(Hash, HashOID, SignedHash);

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

            return false;
        }
            
    }

}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;

Byte DSASignHash(Byte HashToSign[], DSAParameters DSAKeyInfo, String* HashOID) [] {
    try {
        //Create a new instance of DSACryptoServiceProvider.
        DSACryptoServiceProvider* DSA = new DSACryptoServiceProvider();

        //Import the key information.   
        DSA->ImportParameters(DSAKeyInfo);

        //Sign the hash and return it.
        return DSA->SignHash(HashToSign, HashOID);
    } catch (CryptographicException* e) {
        Console::WriteLine(e);
        return 0;
    }
}

bool DSAVerifyHash(Byte Hash[], Byte SignedHash[], DSAParameters DSAKeyInfo, String* HashOID) {
    try {
        //Create a new instance of DSACryptoServiceProvider.
        DSACryptoServiceProvider* DSA = new DSACryptoServiceProvider();

        //Import the key information. 
        DSA->ImportParameters(DSAKeyInfo);

        //Verify the signature and return the result.    
        return DSA->VerifyHash(Hash, HashOID, SignedHash);
    } catch (CryptographicException* e) {
        Console::WriteLine(e->Message);
        return false;
    }
}

int main() {
    try {
        //Create a new instance of DSACryptoServiceProvider to generate
        //a new key pair.
        DSACryptoServiceProvider* DSA = new DSACryptoServiceProvider();

        //The hash to sign.
        Byte Hash[] = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};

        //Use the MapNameToOID method to get an OID 
        //for the SHA1 algorithm.
        String* OID = CryptoConfig::MapNameToOID(S"SHA1");

        //The value to hold the signed hash.
        Byte SignedHashValue[] = DSASignHash(Hash, DSA->ExportParameters(true), OID);

        //Verify the hash and display the results.
        if (DSAVerifyHash(Hash, SignedHashValue, DSA->ExportParameters(false), OID)) {
            Console::WriteLine(S"The hash value was verified.");
        } else {
            Console::WriteLine(S"The hash value was not verified.");
        }
    } catch (ArgumentNullException* e) {
        Console::WriteLine(e->Message);
    }
}

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

必要条件

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

参照

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