好吧,我发现我的一个结构有问题(没有将字节数组成员声明为数组)并且我不再遇到崩溃问题。但是,我仍然没有使用 CryptDecodeObject 取得任何成功。下面的代码是我正在使用的。GetLastErr 只返回 0(帮助不大)。如果有人对我可能出错的地方有想法,请告诉我!
Dim iFile As Integer
Dim sPEM As String, sDER As String
Dim lenPEM As Long, lenDER As Long
Dim publicKeyInfo As CERT_PUBLIC_KEY_INFO
Dim publicKeyInfoLen As Long
iFile = FreeFile
Open app.Path & "\publickey.txt" For Binary As iFile
sPEM = Space(LOF(iFile))
Get #iFile, , sPEM
Close iFile
lenPEM = Len(sPEM)
' Determine buffer length required for the DER string
CryptStringToBinary sPEM, lenPEM, CRYPT_STRING_BASE64HEADER, 0, lenDER, 0, 0
sDER = Space(lenDER)
' Do conversion to binary
If Not CryptStringToBinary(sPEM, lenPEM, CRYPT_STRING_BASE64HEADER, sDER, lenDER, 0, 0) Then
Debug.Print sDER
Else
MsgBox "CryptStringToBinary Error " & GetLastError
Exit Sub
End If
' Do conversion to blob
If Not CryptDecodeObject(X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO, sDER, lenDER, 0, publicKeyInfo, publicKeyInfoLen) Then
MsgBox "CryptDecodeObject Error: " & GetLastError
Exit Sub
End If
如果有人认为这会有所帮助,我可以发布所有函数和类型声明,我相信它们是正确的。
这是 OpenSSL 生成的公钥:
-----开始公钥----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANWhFRxt/ZF56uGO7GsbvevmX42//thm JdseUwQNot/ihXCPRadf0SPYbtHS6/JA92pCX7NxfgYNoYlOFb0IYYcCAwEAAQ== -----结束公钥-----