如何从私钥创建签名? -DSA
我正在尝试根据我提供的私钥和哈希值创建签名。我正在使用 DSA 和以下代码,但收到以下错误:
指定的类型无效。 source mscorlib
这条线上抛出错误: 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
谁能告诉我,我是否在正确的线路上,或者我出路了?
对此的任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用主要特定于 OpenSSL 的格式的 PEM 编码对象作为私钥。从根本上来说,它是一个基于 ASN.1 的结构,以 DER 进行编码,然后以 Base64 重新编码(以使其与 ASCII 兼容),最后配备一些标头(“BEGIN DSA PRIVATE KEY”)。另一方面,
ImportCsbBlob()
需要一堆与 CryptoAPI 需要:DSA 关键元素的自定义、Microsoft 特定编码。我在上面的段落中使用了大量的首字母缩写词,以强调从一种编码转换为另一种编码是一件复杂的事情这一事实。这些东西层次分明,彼此之间非常不同。
这篇 Blob 帖子 似乎告诉我们,OpenSSL 的一些最新版本可以进行以下转换:你。
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.