【问题标题】:VB6 Hashing SHA1 output not matchedVB6 Hashing SHA1 输出不匹配
【发布时间】:2015-12-16 11:27:49
【问题描述】:

需要帮助解决我的问题。我为这个问题进行了搜索和谷歌搜索,但仍然没有找到为什么我的输出与预期输出不匹配的解决方案。

要散列的数据: 0800210142216688003333311100000554478000000

预期输出: DAAC526D4806C88CEDB8B7C6EA42A7442DE6E7DC

我的输出: 805C790E6BF39E3482067C44909EE126F9CBB878

我正在使用这个函数来生成哈希

Public Function HashString(ByVal Str As String, Optional ByVal Algorithm As HashAlgorithm = SHA1) As String
On Error Resume Next
Dim hCtx As Long
Dim hHash As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim AbData() As Byte
lRes = CryptAcquireContext(hCtx, vbNullString, vbNullString, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)
If lRes <> 0 Then
    lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash)
    If lRes <> 0 Then
        lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0)
        If lRes <> 0 Then
            lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)
            If lRes <> 0 Then
                ReDim AbData(0 To lLen - 1)
                lRes = CryptGetHashParam(hHash, HP_HASHVAL, AbData(0), lLen, 0)
                If lRes <> 0 Then
                    For lIdx = 0 To UBound(AbData)
                        HashString = HashString & Right$("0" & Hex$(AbData(lIdx)), 2)
                    Next
                End If
            End If
        End If
        CryptDestroyHash hHash
    End If

End If
CryptReleaseContext hCtx, 0
If lRes = 0 Then
    MsgBox Err.LastDllError
End If
End Function

这是调用函数的命令

Dim received As String
Dim HASH As String

HASH = "0800210142216688003333311100000554478000000"

received = HashString(HASH)

Debug.Print ("HASH VALUE : " & received)

谢谢

更新:

最后我设法得到了预期的输出。我使用本网站中的 sha1 函数更改了生成 sha1 的函数: http://vb.wikia.com/wiki/SHA-1.bas

我确实使用此函数将我的十六进制字符串转换为字节数组

Public Function HexStringToByteArray(ByRef HexString As String) As Byte()
    Dim bytOut() As Byte, bytHigh As Byte, bytLow As Byte, lngA As Long
    If LenB(HexString) Then
        ' preserve memory for output buffer
        ReDim bytOut(Len(HexString) \ 2 - 1)
        ' jump by every two characters (in this case we happen to use byte positions for greater speed)
        For lngA = 1 To LenB(HexString) Step 4
            ' get the character value and decrease by 48
            bytHigh = AscW(MidB$(HexString, lngA, 2)) - 48
            bytLow = AscW(MidB$(HexString, lngA + 2, 2)) - 48
            ' move old A - F values down even more
            If bytHigh > 9 Then bytHigh = bytHigh - 7
            If bytLow > 9 Then bytLow = bytLow - 7
            ' I guess the C equivalent of this could be like: *bytOut[++i] = (bytHigh << 8) || bytLow
            bytOut(lngA \ 4) = (bytHigh * &H10) Or bytLow
        Next lngA
        ' return the output
        HexStringToByteArray = bytOut
    End If
End Function

我使用这个命令得到预期的输出

Dim received As String
Dim HASH As String
Dim intVal As Integer
Dim temp() As Byte

HASH = "08002101422166880033333111000005544780000000"

temp = HexStringToByteArray(HASH)

received = Replace(HexDefaultSHA1(temp), " ", "")

Debug.Print ("HASH VALUE : " & received)

最后我得到了与预期相同的输出。是啊!!..

【问题讨论】:

    标签: hash vb6 sha1


    【解决方案1】:
    • 805c... 是您输入字符串中 characters 的 SHA1 哈希,即 '0', '8', '0', '0', ...
    • daac... 是将每对十六进制数字转换为 byte 后输入字符串中字符的 SHA1 哈希,即 0x08, 0x00, ...

    Convert the input string 散列之前的字节数组。

    【讨论】:

    • 这是正确的,除了……他的输入值根本不是十六进制的。它有奇数位数。虽然如果我最后加上另一个0,我会得到他似乎期望的结果,只要我进行您建议的转换并将这些 bytes 输入哈希而不是 Unicode文本他正在使用。
    • 如果我需要将输入转换为字节数组,这意味着我不能使用相同的函数对吗?..因为我当前的函数需要输入作为字符串..我应该修改当前函数将输入读取为字节数组以及如何修改它?
    • 为什么不在函数中将十六进制字符串解码为字节? CryptStringToBinary() 可以用来做到这一点,这是它存在的主要原因。
    • 最后,我设法得到了预期的输出。感谢大家对我的问题的指导^_^
    【解决方案2】:

    你的输出是正确的。这是使用 python 的 SHA1:

    >>> import hashlib
    >>> s = hashlib.sha1('0800210142216688003333311100000554478000000')
    >>> s.hexdigest()
    '805c790e6bf39e3482067c44909ee126f9cbb878'
    

    您从哪里获得其他 SHA1 计算?

    【讨论】:

    • 我使用来自 eftlab 的 Bp-tools 获得了输出,我需要获得与预期相同的输出。
    • 有一个在线工具可以像我预期的输出一样生成 cnp-wireless.com/Tools/sha1-luhn.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多