【问题标题】:Cant use .GetBytes and .ComputeHash methods on VBA不能在 VBA 上使用 .GetBytes 和 .ComputeHash 方法
【发布时间】:2016-07-22 22:29:47
【问题描述】:

我想将一个 VB 函数翻译成 VBA。该函数使用"System.Text.UTF8Encoding""System.Security.Cryptography.HMACSHA256"

对象及其".GetBytes"".ComputeHash" 方法。

我已经在 VBA 代码中添加了“系统”和“mscorlib.dll”引用,但是我收到了“无效的过程调用或参数”错误。

这是我的 VB 函数:

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 myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
    Dim HashCode As Byte() = myHMACSHA256.ComputeHash(Text)
    Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "")
    Return hash.ToLower
End Function

这是我已经翻译成 VBA 的内容:

Function HashString(ByRef StringToHash As String, ByRef HachKey As String) As String
    Dim myEncoder As Object
    Dim myHMACSHA256 As Object
    Dim Key As Byte
    Dim Text As Byte
    Dim HashCode As Byte
    Dim hash As String
    Set myEncoder = CreateObject("System.Text.UTF8Encoding")
    Set myHMACSHA256 = CreateObject("System.Security.Cryptography.HMACSHA256")
    Key = myEncoder.GetBytes(HachKey)
    Text = myEncoder.GetBytes(StringToHash)
    HashCode = myHMACSHA256.ComputeHash(Text)
    hash = Replace(BitConverter.ToString(HashCode), "-", "")
    HashString = hash.ToLower
End Function

有人可以帮忙吗?我的第一个猜测是我错误地使用了“.GetBytes”和“.ComputeHash”方法

提前致谢

【问题讨论】:

    标签: vb.net vba hash encoding


    【解决方案1】:

    使用 VBA 计算 HMACSHA256 的工作示例:

    Function ComputeHMACSHA256(key As String, text As String) As String
      Dim encoder As Object, crypto As Object
      Dim hash() As Byte, hmacsha As String, i As Long
    
      ' compute HMACSHA256
      Set encoder = CreateObject("System.Text.UTF8Encoding")
      Set crypto = CreateObject("System.Security.Cryptography.HMACSHA256")
      crypto.key = encoder.GetBytes_4(key)
      hash = crypto.ComputeHash_2(encoder.GetBytes_4(text))
    
      ' convert to an hexa string
      hmacsha = String(64, "0")
      For i = 0 To 31
         Mid$(hmacsha, i + i + (hash(i) > 15) + 2) = Hex(hash(i))
      Next
    
      ComputeHMACSHA256 = LCase(hmacsha)
    End Function
    
    
    Sub UsageExample()
      Debug.Print ComputeHMACSHA256("abcdef", "12345")
    End Sub
    

    【讨论】:

      【解决方案2】:

      当通过 COM 使用以支持重载时,.Net 函数具有基于 Name_n 的实现。由于GetBytes 被重载,您需要GetBytes_4(),这是接受字符串的重载,_2 用于ComputeHash()


      Function HashString(ByRef StringToHash As String, ByRef HachKey As String) As String
          Dim myEncoder As Object
          Dim myHMACSHA256 As Object
          Dim Key() As Byte '// all need to be arrays
          Dim Text() As Byte
          Dim HashCode() As Byte
          Dim hash As String
          Set myEncoder = CreateObject("System.Text.UTF8Encoding")
          Set myHMACSHA256 = CreateObject("System.Security.Cryptography.HMACSHA256")
          Key = myEncoder.GetBytes_4(HachKey)
          Text = myEncoder.GetBytes_4(StringToHash)
          HashCode = myHMACSHA256.ComputeHash_2(Text)
      
          Dim i As Long
          For i = 0 To UBound(HashCode)
              Debug.Print Format$(Hex(HashCode(i)), "00")
          Next
      End Function
      

      ?HashString("qwe", "rty")
      80
      D5
      22
      5D
      83
      06
      ...
      

      【讨论】:

      • 感谢您指出“重载”,但我在“GetBytes_4”方法中收到“无效限定符”错误
      猜你喜欢
      • 2020-12-27
      • 2020-03-29
      • 2013-02-19
      • 2022-07-28
      • 1970-01-01
      • 2021-02-16
      • 2014-05-02
      • 1970-01-01
      • 2011-05-10
      相关资源
      最近更新 更多