Freigeben über


DSACryptoServiceProvider.VerifyHash-Methode

Überprüft die angegebenen Signaturdaten durch Vergleich mit der Signatur, die für den angegebenen Hashwert berechnet wurde.

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

Syntax

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

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

Parameter

  • rgbHash
    Der Hashwert der zu signierenden Daten.
  • str
    Der Name des Hashalgorithmus, mit dem der Hashwert der Daten erstellt wurde.
  • rgbSignature
    Die zu prüfenden Signaturdaten.

Rückgabewert

true, wenn die Signatur gültig ist, andernfalls false.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

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

– oder –

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

CryptographicException

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

– oder –

Die Signatur kann nicht überprüft werden.

Hinweise

Diese Methode überprüft die digitale DSA-Signatur, die von SignHash erzeugt wird.

Wenn der str-Parameter NULL (Nothing in Visual Basic) ist, wird der Standardhashalgorithmus (SHA1) verwendet. Die gültigen Hashalgorithmen sind SHA1 und MD5.

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

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