DSA signature problems with VB.NET 2002

From: jwb (jwbiagio_at_sbcglobal.net)
Date: 06/26/03


Date: Thu, 26 Jun 2003 17:59:48 GMT


Hello all,

I am having difficulty getting DSA signatures to work with VB.NET 2002. I'm
attaching code that retrieves my keys from the machine key store, prints
them out (xml string to the console), signs a hash value (20 bytes for
SHA-1) and verifies the signature.

The program retrieves my keys fine (it prints to the screen), but it cannot
sign and verify. In fact, it gives me a "Bad Key" CryptographicException,
which I do not understand, because I just retrieved and printed out the
keys! Please take a look at the code and let me know.

Thanks,

jwb <jwbiagio at sbcglobal dot net>

------ code follows ------
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 cp As CspParameters = GenKey_SaveInContainer("CU5000")

            ' Create a new instance of DSACryptoServiceProvider that
accesses
            Dim DSA As New DSACryptoServiceProvider(cp)

            '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,
DSA.ExportParameters(True), "SHA1")

            'Verify the hash and display the results.
            If DSAVerifyHash(HashValue, SignedHashValue,
DSA.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

    Public Shared Function GenKey_SaveInContainer(ByVal ContainerName As
String) As CspParameters
        ' Create the CspParameters object and set the key container
        ' name used to store the DSA key pair.
        Dim cp As New CspParameters()
        cp.KeyContainerName = ContainerName
        cp.Flags = CspProviderFlags.UseMachineKeyStore

        ' Create a new instance of DSACryptoServiceProvider that accesses
        ' the key container MyKeyContainerName.
        Dim DSA As New DSACryptoServiceProvider(cp)

        ' Display the key information to the console.
        Console.WriteLine("Key added to container: {0}",
DSA.ToXmlString(True))
        return cp
    End Function

    Public Shared 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)
            System.Diagnostics.Debugger.Break()
            Return Nothing
        End Try
    End Function

    Public Shared 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)
            System.Diagnostics.Debugger.Break()
            Return False
        End Try
    End Function
End Class



Relevant Pages

  • Re: Registry Cleaning?
    ... without knowing beforehand the number of entries it will retrieve back. ... if it retrieves and processes 20. ... windows program comes anywhere near 500 keys. ... ignore cleaning out any registry entries installed. ...
    (microsoft.public.windows.vista.general)
  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)
  • Re: Merging hashes using both symbols and strings as keys
    ... default values for my class) and the other using strings as keys ... (taken from the params hash). ... the string, ... # the returned hash will also be extended by SymbolizeKeys ...
    (comp.lang.ruby)
  • Re: Send a fax from VBA
    ... Public Sub FaxeFeuille(FeuilleAFaxer As String, destinataire As String, ... Dim keys As String ... (This would not be needed if the assistant is already ... > Then you would use the Sendkeys statement to TAB to the required textbox ...
    (microsoft.public.excel.programming)
  • Re: Merging hashes using both symbols and strings as keys
    ... default values for my class) and the other using strings as keys ... (taken from the params hash). ... I made a small module called SymbolizeKeys that will allow you to extend ... # converts any current string keys to symbol keys ...
    (comp.lang.ruby)