2016-04-03 148 views
1

我想将VB函数转换为VBA。该功能使用"System.Text.UTF8Encoding""System.Security.Cryptography.HMACSHA256"无法在VBA上使用.GetBytes和.ComputeHash方法

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

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

这里是我的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”的方法提前

回答

2

工作示例字符串并_2计算与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 
+0

谢谢,像一个魅力:) – Behnam2016

1

正确

由于当通过COM以用于支持重载.NET函数都基于Name_n实现。作为GetBytes超载你需要GetBytes_4()这是接受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 
... 
+0

感谢您指出“超载”,但我得到“无效的质量ifier“错误”GetBytes_4“方法 – Behnam2016

+0

感谢超载澄清 – user6698332