Freigeben über


DSACryptoServiceProvider.SignHash-Methode

Berechnet die Signatur für den angegebenen Hashwert durch Verschlüsselung mit dem privaten Schlüssel.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function SignHash ( _
    rgbHash As Byte(), _
    str As String _
) As Byte()
'Usage
Dim instance As DSACryptoServiceProvider
Dim rgbHash As Byte()
Dim str As String
Dim returnValue As Byte()

returnValue = instance.SignHash(rgbHash, str)
public byte[] SignHash (
    byte[] rgbHash,
    string str
)
public:
array<unsigned char>^ SignHash (
    array<unsigned char>^ rgbHash, 
    String^ str
)
public byte[] SignHash (
    byte[] rgbHash, 
    String str
)
public function SignHash (
    rgbHash : byte[], 
    str : String
) : byte[]

Parameter

  • rgbHash
    Der Hashwert der zu signierenden Daten.
  • str
    Der Name des Hashalgorithmus, mit dem der Hashwert der Daten erstellt wurde.

Rückgabewert

Die DSA-Signatur für den angegebenen Hashwert.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

Der rgbHash-Parameter ist NULL (Nothing in Visual Basic).

CryptographicException

Der Kryptografiedienstanbieter (Cryptographic Service Provider, CSP) kann nicht ermittelt werden.

– oder –

Es ist kein privater Schlüssel vorhanden.

Hinweise

Diese Methode erstellt eine digitale Signatur, die mithilfe der VerifyHash-Methode überprüft wird.

DSA verwendet den SHA1-Hashalgorithmus.

Beispiel

Im folgenden Codebeispiel werden Daten unter Verwendung der DSACryptoServiceProvider-Klasse signiert und überprüft.

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

}
#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
array<Byte>^ DSASignHash( array<Byte>^HashToSign, DSAParameters DSAKeyInfo, String^ HashOID )
{
   try
   {
      
      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew 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 nullptr;
   }

}

bool DSAVerifyHash( array<Byte>^Hash, array<Byte>^SignedHash, DSAParameters DSAKeyInfo, String^ HashOID )
{
   try
   {
      
      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew 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 = gcnew DSACryptoServiceProvider;
      
      //The hash to sign.
      array<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.
      array<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 );
   }

}
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 to sign.
            ubyte 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.
            ubyte 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.get_Message());
        }
    } //main

    public static ubyte[] DSASignHash(ubyte 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;
        }
    } //DSASignHash  
        
    public static boolean DSAVerifyHash(ubyte hash[], ubyte 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.get_Message());
            return false ;
        }
    } //DSAVerifyHash
} //DSACSPSample

.NET Framework-Sicherheit

  • KeyContainerPermission  für die Berechtigung, eine Datei mithilfe des Schlüssels zu signieren. Zugeordnete Enumeration: Sign. Sicherheitsaktion: Demand.

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

DSACryptoServiceProvider-Klasse
DSACryptoServiceProvider-Member
System.Security.Cryptography-Namespace

Weitere Ressourcen

Kryptografische Dienste