DSA signature problems with VB.NET 2002
From: jwb (jwbiagio_at_sbcglobal.net)
Date: 06/26/03
- Next message: Matjaz Ladava: "Re: SQL Integrated Security in .NET1.1"
- Previous message: basin: "Re: SQL Integrated Security in .NET1.1"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
- Next message: Matjaz Ladava: "Re: SQL Integrated Security in .NET1.1"
- Previous message: basin: "Re: SQL Integrated Security in .NET1.1"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|