Freigeben über


DSACryptoServiceProvider.VerifyData-Methode

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

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

Syntax

'Declaration
Public Function VerifyData ( _
    rgbData As Byte(), _
    rgbSignature As Byte() _
) As Boolean
'Usage
Dim instance As DSACryptoServiceProvider
Dim rgbData As Byte()
Dim rgbSignature As Byte()
Dim returnValue As Boolean

returnValue = instance.VerifyData(rgbData, rgbSignature)
public bool VerifyData (
    byte[] rgbData,
    byte[] rgbSignature
)
public:
bool VerifyData (
    array<unsigned char>^ rgbData, 
    array<unsigned char>^ rgbSignature
)
public boolean VerifyData (
    byte[] rgbData, 
    byte[] rgbSignature
)
public function VerifyData (
    rgbData : byte[], 
    rgbSignature : byte[]
) : boolean

Parameter

  • rgbData
    Die Daten, die signiert wurden.
  • rgbSignature
    Die zu prüfenden Signaturdaten.

Rückgabewert

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

Hinweise

Diese Methode überprüft die digitale DSA-Signatur, die von SignData erzeugt 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 data to sign.
            Dim Data 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 SignedValue As Byte() = DSASignData(Data, DSA.ExportParameters(True))

            'Verify the data and display the results.
            If DSAVerifyData(Data, SignedValue, DSA.ExportParameters(False)) 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 DSASignData(ByVal DataToSign() As Byte, ByVal DSAKeyInfo As DSAParameters) As Byte()
        Try
            'Create a new instance of DSACryptoServiceProvider.
            Dim DSA As New DSACryptoServiceProvider()

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

            'Compute hash value, sign the hash, and return the signed hash.
            Return DSA.SignData(DataToSign)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)

            Return Nothing
        End Try
    End Function


    Public Shared Function DSAVerifyData(ByVal Data() As Byte, ByVal SignedData() As Byte, ByVal DSAKeyInfo As DSAParameters) 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.VerifyData(Data, SignedData)

        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 data to sign.
            byte[] Data = {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[] SignedValue = DSASignData(Data, DSA.ExportParameters(true));

            //Verify the data and display the results.
            if(DSAVerifyData(Data, SignedValue, DSA.ExportParameters(false)))
            {
                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[] DSASignData(byte[] DataToSign, DSAParameters DSAKeyInfo)
    {
        try
        {
            //Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

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

            //Compute hash value, sign the hash, and return the signed hash.
            return DSA.SignData(DataToSign);
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);

            return null;
        }

    }

    public static bool DSAVerifyData(byte[] Data, byte[] SignedData, DSAParameters DSAKeyInfo)
    {
        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.VerifyData(Data, SignedData);

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

            return false;
        }
            
    }

}
#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
array<Byte>^ DSASignData( array<Byte>^DataToSign, DSAParameters DSAKeyInfo )
{
   try
   {
      
      //Create a new instance of DSACryptoServiceProvider.
      DSACryptoServiceProvider^ DSA = gcnew DSACryptoServiceProvider;
      
      //Import the key information.   
      DSA->ImportParameters( DSAKeyInfo );
      
      //Compute hash value, sign the hash, and return the signed hash.
      return DSA->SignData( DataToSign );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
      return nullptr;
   }

}

bool DSAVerifyData( array<Byte>^Data, array<Byte>^SignedData, DSAParameters DSAKeyInfo )
{
   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->VerifyData( Data, SignedData );
   }
   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 data to sign.
      array<Byte>^Data = {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>^SignedValue = DSASignData( Data, DSA->ExportParameters( true ) );
      
      //Verify the data and display the results.
      if ( DSAVerifyData( Data, SignedValue, DSA->ExportParameters( false ) ) )
      {
         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 data to sign.
            ubyte data[] =  {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 signedValue[] = DSASignData(data, dsa.ExportParameters(true));
            
            //Verify the data and display the results.
            if (DSAVerifyData(data, signedValue, dsa.ExportParameters(false))) {
                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[] DSASignData(ubyte dataToSign[], DSAParameters dsaKeyInfo) 
    {
        try {
            // Create a new instance of DSACryptoServiceProvider.
            DSACryptoServiceProvider dsa =  new DSACryptoServiceProvider();
            
            // Import the key information.   
            dsa.ImportParameters(dsaKeyInfo);
            
            // Compute hash value, sign the hash, and return the signed hash.
            return dsa.SignData(dataToSign) ;
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());            
            return null ;
        }
    } //DSASignData

    public static boolean DSAVerifyData(ubyte data[], ubyte signedData[], 
        DSAParameters dsaKeyInfo) 
    {
        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.VerifyData(data, signedData) ;
        } 
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
            return false ;
        }
    } //DSAVerifyData
} //DSACSPSample

Im folgenden Codebeispiel wird ein DSACryptoServiceProvider erstellt, ein neues Schlüsselpaar wird generiert, und einige Daten werden signiert und getestet.

Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New ASCIIEncoding

            Dim dataString As String = "Data to Sign"

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToSign As Byte() = ByteConverter.GetBytes(dataString)
            Dim hashedData() As Byte
            Dim signedData() As Byte

            ' Create a SHA1CryptoServiceProvider object 
            ' to hash the data that will be signed.
            Dim SHA1alg As New SHA1CryptoServiceProvider

            ' Hash the data to sign.
            hashedData = SHA1alg.ComputeHash(dataToSign)

            'Create a new instance of the DSACryptoServiceProvider class 
            ' and automatically create a new key-pair.
            Dim DSAalg As New DSACryptoServiceProvider

            'Use the MapNameToOID method to get an object identifier  
            '(OID) from the string name of the SHA1 algorithm.
            Dim OID As String = CryptoConfig.MapNameToOID("SHA1")

            ' Sign the hash and pass the OID.
            signedData = DSAalg.SignHash(hashedData, OID)

            ' Verify the signature and display the results to the
            ' console.
            If DSAalg.VerifySignature(hashedData, signedData) Then
                Console.WriteLine("The data was verified.")
            Else
                Console.WriteLine("The data does not match the signature.")
            End If

        Catch e As CryptographicException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine(e.Message)
        End Try
    End Sub

End Module
using System;
using System.Security.Cryptography;
using System.Text;

class DSACSPSample
{
    static void Main()
    {
        try
        {
            //Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = "Data to Sign";

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToSign = ByteConverter.GetBytes(dataString);
            byte[] hashedData;
            byte[] signedData;

            // Create a SHA1CryptoServiceProvider object 
            // to hash the data that will be signed.
            SHA1CryptoServiceProvider SHA1alg = new SHA1CryptoServiceProvider();

            // Hash the data to sign.
            hashedData = SHA1alg.ComputeHash(dataToSign);

            //Create a new instance of the DSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider();

            //Use the MapNameToOID method to get an object identifier  
            //(OID) from the string name of the SHA1 algorithm.
            string OID = CryptoConfig.MapNameToOID("SHA1");
 
            // Sign the hash and pass the OID.
            signedData = DSAalg.SignHash(hashedData, OID);

            // Verify the signature and display the results to the
            // console.
            if(DSAalg.VerifySignature(hashedData, signedData))
            {
                Console.WriteLine("The data was verified.");
            }
            else
            {
                Console.WriteLine("The data does not match the signature.");
            }

        }
        catch(CryptographicException e)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine(e.Message);

        }
    }
}
using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
int main()
{
   try
   {
      
      //Create a UnicodeEncoder to convert between byte array and string.
      ASCIIEncoding^ ByteConverter = gcnew ASCIIEncoding;
      String^ dataString = "Data to Sign";
      
      //Create byte arrays to hold original, encrypted, and decrypted data.
      array<Byte>^dataToSign = ByteConverter->GetBytes( dataString );
      array<Byte>^hashedData;
      array<Byte>^signedData;
      
      // Create a SHA1CryptoServiceProvider object 
      // to hash the data that will be signed.
      SHA1CryptoServiceProvider^ SHA1alg = gcnew SHA1CryptoServiceProvider;
      
      // Hash the data to sign.
      hashedData = SHA1alg->ComputeHash( dataToSign );
      
      //Create a new instance of the DSACryptoServiceProvider class 
      // and automatically create a new key-pair.
      DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider;
      
      //Use the MapNameToOID method to get an object identifier  
      //(OID) from the string name of the SHA1 algorithm.
      String^ OID = CryptoConfig::MapNameToOID( "SHA1" );
      
      // Sign the hash and pass the OID.
      signedData = DSAalg->SignHash( hashedData, OID );
      
      // Verify the signature and display the results to the
      // console.
      if ( DSAalg->VerifySignature( hashedData, signedData ) )
      {
         Console::WriteLine( "The data was verified." );
      }
      else
      {
         Console::WriteLine( "The data does not match the signature." );
      }
   }
   catch ( CryptographicException^ e ) 
   {
      
      //Catch this exception in case the encryption did
      //not succeed.
      Console::WriteLine( e->Message );
   }

}

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