【发布时间】:2017-01-13 09:02:12
【问题描述】:
目前在我的代码中出现自动化错误(注意,此错误仅发生在 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
根据我的测试和研究,我发现这些行中的此错误与 .netframework 4.6 版有关。安装 .netframework 3.5 版可修复此错误并允许代码正确运行。但是,此电子表格将提供给客户,我宁愿让功能工作而不必请求客户端安装 3.5(电子表格需要是客户端使用其所有功能所需的全部,即他们必须不必安装任何东西(除了办公室),都必须包含在 excel 文档中)
有人知道另一种方法吗?我找到了一种使用类模块执行 SHA256 的方法,但这不适用于 HMACSHA256。我需要一种方法来做到这两点。
【问题讨论】:
-
这些
System.Text.UTF8Encoding和System.Security.Cryptography.SHA256Managed应该在参考列表Goto--> tools--.reference 中。 -
这是 Microsoft .NET Framework 类。计算机上是否安装了 Microsoft .NET Framework?
-
是的,.netframework 4.6 是默认从 windows 10 上的 windows 更新安装的,但是该代码仅适用于 3.5,默认情况下不安装
-
如果您已经可以访问实现 SHA256 的类模块,那么用 HMAC 包装它应该不会太复杂。查看spec summary 和一些sample pseudocode 了解它是如何完成的。可能比让 .NET 正常运行更容易。