我想将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”的方法提前
谢谢,像一个魅力:) – Behnam2016