Vba Excel“自动化错误”来自SHA256和HMACSHA256功能 [英] Vba Excel "automation error" from SHA256 and HMACSHA256 Functions

查看:1104
本文介绍了Vba Excel“自动化错误”来自SHA256和HMACSHA256功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的代码中出现自动化错误(请注意,此错误仅发生在Windows 10上)

Currently getting an Automation error in my code on the lines (note, this error only occurs on 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版有关。安装版本3.5的.netframework可以修复此错误,并允许代码正确运行。然而,这个电子表格将被给予客户端,我宁愿拥有该功能,而不必请求客户端安装3.5(电子表格需要是所有客户端需要使用其所有功能,即不必安装任何东西(办公室除外)都必须包含在excel文件中)

From my testing and research I have found that this error on these lines has something to do with .netframework version 4.6. Installing version 3.5 of .netframework fixes this error and allows the code to run correctly. However this spreadsheet is to be given to clients, and I would rather have the function work without having to request the client install 3.5 (the spreadsheet needs to be all the client needs to use all of its functionality, ie they must not have to install anything (other than office), all must be contained within the excel document)

有没有人知道这样做的另一种方法?我找到了一种使用类模块来做SHA256的方法,但是这不能用于HMACSHA256。我需要一种做这两种方法。

Does anyone know another way of doing this? I found a way to do a SHA256 using a class module, but this does not work HMACSHA256. I need a way to do both.

推荐答案

所以我自己解决了这个问题。我找到另一个类模块,完成先前由.net组件完成的所有作业。

So i fixed this myself in the end. I found another class module that does all the jobs previously done by the .net components

我发现类模块为
http://www.vbforums.com/showthread .php?635398-VB6-HMAC-SHA-256-HMAC-SHA-1-Use-Crypto-API

这里是我更新的代码:

and here is my updated Code:

Function HMACSHA256A(strToSign As String, strKey() As Byte)
Dim lngLoop As Long
Dim oUTF, oEnc
Dim HMAC() As Byte
Dim lastrow As Long
Dim byteString() As Byte

On Error GoTo err_handler

lastrow = FindLastRow

Set Test = New HS256
Test.InitHmac strKey
byteString = Test.ToUTF8(strToSign)
HMACSHA256A = Test.HMACSHA256(byteString)

Worksheets("Log Sheet").Cells(lastrow, 4) = "Pass"

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

这篇关于Vba Excel“自动化错误”来自SHA256和HMACSHA256功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆