我在我的代码中的这些行出现了自动化错误(请注意,此错误仅在Windows 10上发生)
Set oUTF = CreateObject("System.Text.UTF8Encoding")
Set oEnc = CreateObject("System.Security.Cryptography.SHA256Managed")
这是完整的函数。
Function HMACSHA256(strToSign As String, strKey() As Byte)
Dim lngLoop As Long
Dim oUTF, oEnc
Dim HMAC() As Byte
Dim lastrow As Long
On Error GoTo err_handler
Set oUTF = CreateObject("System.Text.UTF8Encoding")
Set oEnc = CreateObject("System.Security.Cryptography.HMACSHA256")
oEnc.key = strKey
HMAC = oEnc.ComputeHash_2(oUTF.GetBytes_4(strToSign))
HMACSHA256 = HMAC
Exit Function
err_handler:
Worksheets("Log Sheet").Cells(lastrow, 4) = "Fail"
Worksheets("Log Sheet").Cells(lastrow, 5) = Err.Description
MsgBox Err.Description, vbCritical
End Function
从我的测试和研究中,我发现这些行上的错误与.NET Framework版本4.6有关。安装.NET Framework 3.5版本可以修复此错误并允许代码正确运行。然而,这个电子表格将被提供给客户使用,我宁愿使函数能够在不要求客户安装3.5的情况下正常工作(电子表格需要是客户使用其所有功能所需的全部内容,即他们不必安装任何东西(除了办公软件)。所有内容必须包含在Excel文档中)。
有人知道另一种方法吗?我找到了一种使用类模块进行SHA256的方法,但这不适用于HMACSHA256。我需要一种同时做到这两个的方法。
System.Text.UTF8Encoding
和System.Security.Cryptography.SHA256Managed
应该在参考列表中。前往 --> 工具 --> 引用。 - Techie