开发者

How to create a signature from Private Key? - DSA

开发者 https://www.devze.com 2023-01-27 15:28 出处:网络
I am trying to create a signature from a private key that i have been provided and a hashed value. I am using DSA and the following code but receive the following error:

I am trying to create a signature from a private key that i have been provided and a hashed value. I am using DSA and the following code but receive the following error:

Invalid type specified. source mscorlib

The error is thrown on this line: ImportCspBlob(pk)

 Private Function key() As String

        Dim privatekey As String = "-----BEGIN DSA PRIVATE KEY-----" _
        & "Key Data"
        & "-----END DSA PRIVATE KEY-----"

        Dim dsa As DSACryptoServiceProvider = New DSACryptoServiceProvider()
        Dim pk As Byte()开发者_如何转开发 = Encoding.ASCII.GetBytes(privatekey)

        dsa.ImportCspBlob(pk)
        Dim st As Byte() = Encoding.ASCII.GetBytes("THIS IS THE HASH STRING")))
        Dim signedValue As Byte() = dsa.SignHash(st, "SHA1")

        Return Encoding.ASCII.GetString(signedValue)

    End Function

Can anyone tell me if I am on the right lines herE or am I way out?

Any help on this would be much appreciated.


You are using, as private key, a PEM-encoded object in a format which is mostly specific to OpenSSL. At its root, it is an ASN.1-based structure, encoded in DER, then re-encoded in Base64 (to make it ASCII-compatible), and finally equipped with some headers (the "BEGIN DSA PRIVATE KEY"). On the other hand, ImportCsbBlob() expects a bunch of bytes compatible with what CryptoAPI expects: a custom, Microsoft-specific encoding of the DSA key elements.

I have used an awful lot of acronyms in the paragraph above in order to underline the fact that converting from one encoding to another is a complex matter. Those things are heavily layered and very different from each other.

This blob post seems to tell that some very recent versions of OpenSSL can do the conversion for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号