Visual Basic 2010 HMAC SHA1

发布于 2024-12-06 03:13:50 字数 747 浏览 1 评论 0原文

我有一个将字符串转换为 hmac sha1 加密的代码。 但是,我无法让它工作。这是我的代码:

Public Shared Function HashString(ByVal StringToHash As String) As String
    Dim myEncoder As New System.Text.UTF32Encoding
    Dim Key() As Byte = myEncoder.GetBytes("thisismykey")
    Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
    Dim myHMACSHA1 As New System.Security.Cryptography.HMACSHA1(Key)
    Dim HashCode As Byte() = myHMACSHA1.ComputeHash(Text)
    Return Convert.ToBase64String(HashCode)
End Function

当我运行这样的函数时:

TextBox1.Text = HashString("thisismystring")

我在文本框中得到 04p075DKS2Suw9jGQKC5Q7mYjvI= 。 我应该得到的是 c2bc9dd26b76d5b61a40ac788220eef0b26cb2bb

有人知道如何解决这个问题吗?请帮忙:)

i got a code to convert a string to a hmac sha1 encyption.
However, i cant get it to work. Here is my code:

Public Shared Function HashString(ByVal StringToHash As String) As String
    Dim myEncoder As New System.Text.UTF32Encoding
    Dim Key() As Byte = myEncoder.GetBytes("thisismykey")
    Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
    Dim myHMACSHA1 As New System.Security.Cryptography.HMACSHA1(Key)
    Dim HashCode As Byte() = myHMACSHA1.ComputeHash(Text)
    Return Convert.ToBase64String(HashCode)
End Function

When i run the function like this:

TextBox1.Text = HashString("thisismystring")

I get 04p075DKS2Suw9jGQKC5Q7mYjvI= in the textbox.
What i should get is c2bc9dd26b76d5b61a40ac788220eef0b26cb2bb

Anyone has any idea on how to solve this? Please help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

dawn曙光 2024-12-13 03:13:50

我找到了解决方案。
我只是将字节转换为字符串,将其降低并替换 - 没有任何内容。
请参阅下面的代码:)

    Public Function HashString(ByVal StringToHash As String, ByVal HachKey As String) As String
    Dim myEncoder As New System.Text.UTF8Encoding
    Dim Key() As Byte = myEncoder.GetBytes(HachKey)
    Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
    Dim myHMACSHA1 As New System.Security.Cryptography.HMACSHA1(Key)
    Dim HashCode As Byte() = myHMACSHA1.ComputeHash(Text)
    Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "")
    Return hash.ToLower
End Function

示例用法:

TextBox1.Text = HashString("thisismystring", "thisismykey")

感谢您的帮助:)

I found the solution.
I just converted the byte to a string, made it to lower and replaced - with nothing.
See my code below :)

    Public Function HashString(ByVal StringToHash As String, ByVal HachKey As String) As String
    Dim myEncoder As New System.Text.UTF8Encoding
    Dim Key() As Byte = myEncoder.GetBytes(HachKey)
    Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
    Dim myHMACSHA1 As New System.Security.Cryptography.HMACSHA1(Key)
    Dim HashCode As Byte() = myHMACSHA1.ComputeHash(Text)
    Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "")
    Return hash.ToLower
End Function

Example Usage:

TextBox1.Text = HashString("thisismystring", "thisismykey")

Thanks for your help :)

放赐 2024-12-13 03:13:50

您的 04p075DKS2Suw9jGQKC5Q7mYjvI= 采用 Base64。您的 c2bc9dd26b76d5b61a40ac788220eef0b26cb2bb 为十六进制。您需要将一种格式转换为另一种格式,以便可以正确比较它们。

ETA:我检查过,两者不匹配,你的十六进制给我 Base64 的 wryd0mt21bYaQKx4giDu8LJssrs= 。我怀疑问题可能在于使用UTF32编码,这是非常不寻常的。 UTF8 或 UTF16 更为常见。首先尝试UTF8。

Your 04p075DKS2Suw9jGQKC5Q7mYjvI= is in Base64. Your c2bc9dd26b76d5b61a40ac788220eef0b26cb2bb is in hex. You need to convert one into the other format so you can compare them correctly.

ETA: I checked, the two don't match, your hex gives me wryd0mt21bYaQKx4giDu8LJssrs= in Base64. I suspect the problem may lie with using UTF32 encoding, this is very unusual. UTF8 or UTF16 are much more common. Try UTF8 first.

暖伴 2024-12-13 03:13:50

更正主要 EXCEL,

pour faire du mD5,il faut copier coller le code ci-dessous

Option Explicit
Private Const HP_HASHVAL = 2
Private Const HP_HASHSIZE = 4
Private Const PROV_RSA_FULL  As Long = 1
Private Const ALG_CLASS_HASH = 32768
Private Const ALG_TYPE_ANY = 0
Private Const ALG_SID_MD2 = 1
Private Const ALG_SID_MD4 = 2
Private Const ALG_SID_MD5 = 3
Private Const ALG_SID_SHA1 = 4
Private Const CRYPT_NEWKEYSET = &H8
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000

Enum HashAlgorithm
    MD2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
    MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
    MD5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
    SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
End Enum

Private Declare Function CryptAcquireContext Lib "Advapi32" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "Advapi32" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptCreateHash Lib "Advapi32" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long
Private Declare Function CryptDestroyHash Lib "Advapi32" (ByVal hHash As Long) As Long
Private Declare Function CryptHashData Lib "Advapi32" (ByVal hHash As Long, pbData As Any, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetHashParam Lib "Advapi32" (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, pdwDataLen As Long, ByVal dwFlags As Long) As Long

Public Function HashString(ByVal Str As String, Optional ByVal Algorithm As HashAlgorithm = MD5) 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

correction depuis maj EXCEL,

pour faire du mD5, il faut copier coller le code ci-dessous

Option Explicit
Private Const HP_HASHVAL = 2
Private Const HP_HASHSIZE = 4
Private Const PROV_RSA_FULL  As Long = 1
Private Const ALG_CLASS_HASH = 32768
Private Const ALG_TYPE_ANY = 0
Private Const ALG_SID_MD2 = 1
Private Const ALG_SID_MD4 = 2
Private Const ALG_SID_MD5 = 3
Private Const ALG_SID_SHA1 = 4
Private Const CRYPT_NEWKEYSET = &H8
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000

Enum HashAlgorithm
    MD2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
    MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
    MD5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
    SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
End Enum

Private Declare Function CryptAcquireContext Lib "Advapi32" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "Advapi32" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptCreateHash Lib "Advapi32" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long
Private Declare Function CryptDestroyHash Lib "Advapi32" (ByVal hHash As Long) As Long
Private Declare Function CryptHashData Lib "Advapi32" (ByVal hHash As Long, pbData As Any, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetHashParam Lib "Advapi32" (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, pdwDataLen As Long, ByVal dwFlags As Long) As Long

Public Function HashString(ByVal Str As String, Optional ByVal Algorithm As HashAlgorithm = MD5) 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文