【发布时间】: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”方法
提前致谢
【问题讨论】: