使用advapi32.dll cryptverysignature的VB代码在Windows 10 64位上的Vista



我一直在尝试解决为什么在VISTA SP2机上编写和编译的应用程序的元素在该机器上完美工作,而在64位Windows 10机器上不工作。为了提高调试功能,我在Excel VBA中复制了问题编码。在Vista机器上工作,但在Windows 10机器上不使用。

cryptoapi呼叫都因此而声明:

Private Declare Function CryptVerifySignature _
Lib "advapi32.dll" _
    Alias "CryptVerifySignatureA" ( _
        ByVal hHash As Long, _
        pbSignature As Byte, _
        ByVal dwSigLen As Long, _
        ByVal hPubKey As Long, _
        ByVal sDescription As String, _
        ByVal dwFlags As Long _
            ) As Long

Windows 10机器上失败的部分由下方和下方的火车轨道突出显示:

Private Function SignValidate(ByRef abData() As Byte, _
                        ByRef abSigned() As Byte, _
                        Optional bSigned As Boolean = True) As Long
Dim hHash As Long
Dim lngReturnValue As Long
Dim lngSigLen As Long
Dim abText() As Byte
Dim strTxt As String
Dim lngW As Long
Dim lngX As Long
Dim lngY As Long
Dim abHashVal() As Byte
SignValidate = -1
ReDim abText(UBound(abData))
abText = abData
'Create a hash object to sign/validate
lngReturnValue = CryptCreateHash(hCryptProv, CALG_SHA, 0, 0, hHash)
If lngReturnValue = 0 Then
    'Set_locale regionalSymbol
    Err.Raise Err.LastDllError, , "DLL error code shown above. Could not create a Hash Object (CryptCreateHash API)"
End If
'Hash the data
lngW = UBound(abText) + 1
lngReturnValue = CryptHashData(hHash, abText(0), lngW, 0)
If lngReturnValue = 0 Then
    'Set_locale regionalSymbol
    Err.Raise Err.LastDllError, , "DLL error code shown above. Could not calculate a Hash Value (CryptHashData API)"
End If
If bSigned Then
    'release old key pair handle
    If hKeyPair <> 0 Then CryptDestroyKey hKeyPair
    'get a handle to the signature key pair
    lngReturnValue = CryptGetUserKey(hCryptProv, AT_SIGNATURE, hKeyPair)
    If lngReturnValue = 0 Then
        'Set_locale regionalSymbol
        Err.Raise Err.LastDllError, , "DLL error code shown above. Could not obtain key pair"
    End If
    'Determine the size of the signature
    lngReturnValue = CryptSignHash(hHash, AT_SIGNATURE, 0, 0, vbNull, lngSigLength)
    If lngSigLength > 0 Then ReDim abSig(lngSigLength - 1)
    'Sign the hash object
    lngReturnValue = CryptSignHash(hHash, AT_SIGNATURE, 0, 0, abSig(0), lngSigLength)
    If lngReturnValue = 0 Then
        'Set_locale regionalSymbol
        Err.Raise Err.LastDllError, , "DLL error code shown above. Could not sign the hash"
    End If
    ' the signature is now available
    ' size returned array to signature length
    ReDim abSigned(UBound(abSig))
    ' return the signature to the calling procedure
    abSigned = abSig
    SignValidate = 0
Else
    lngSigLength = UBound(abSigned) + 1
    ReDim abSig(UBound(abSigned))
    abSig = abSigned ' load the Signature array
'========================================================
    'this is the line where the actual validation is done
    lngReturnValue = CryptVerifySignature(hHash, abSig(0), lngSigLength, hKeyPair, 0, 0)
'========================================================
    If lngReturnValue = 0 Then 'some error occurred
        SignValidate = Err.LastDllError
    Else
        SignValidate = 0
    End If
End If
End Function

Windows 10机器在突出显示的cryptverysignature呼叫上失败,并返回一个err.lastdllerror等于nte_bad_signature。Vista机器验证了签名罚款。

我花了几天的时间研究这里可能发生的事情。一切都无济于事。任何指针都感激地收到

经过许多挫败感和毫无结果的研究,我最终发现了问题所在。一路上,我发现问题完全源于代码的另一部分。我还发现该问题也表现在Windows 10 32位 - 所以不是64位问题。

,即使在Vista 32位的Cryptverifysignature呼吁,即使调查的Cryptimportkey的呼吁失败了,

向较早的CryptiMportkey进行了一个较早的呼叫的论点似乎并没有阻止Cryptverifysignature的呼吁。一旦纠正了Cryptimportkey的DWFlags参数,以crypt_export_able或crypt_no_salt纠正,它就成功了,随后的cryptverysignature呼叫在所有替代操作系统/位数字组合下都成功了。

我能够测试。。

表示歉意,并感谢所有试图在此问题上提供帮助的人。直到下一次。

编辑 - 您的声明或致电

我看不到任何错误

我看到的唯一替代方案是更改字节指针以称为ByVal pSignature Long,然后使用varPtr(abSig(0)

调用它

但是您的代码看起来不错 - 我很困惑


您的声明与Microsoft API声明不符。不确定这是否是设计,因为签名是字节指针

根据文档

LPBYTE, BYTE  will be : *   ByRef Byte

更改此(默认情况下是通过BYVAL传递)

pbSignature As Byte

到这个

ByRef pbSignature As Byte

相关内容

最新更新