【问题标题】:Excel VBA SHA1 functionExcel VBA SHA1 函数
【发布时间】:2018-03-10 21:05:12
【问题描述】:

我正在运行这个模块,但它一直给我一个错误,并且调试将我发送到这一行:

Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")

运行时错误'-2146233079(80131509':自动化错误)

我添加了对mscorlib 类型库的引用,但它仍然无法正常工作。

Public Function SHA1(sIn As String, Optional bB64 As Boolean = 0) As String
    'Set a reference to mscorlib 4.0 64-bit

    'Test with empty string input:
    '40 Hex:   da39a3ee5e6...etc
    '28 Base-64:   2jmj7l5rSw0yVb...etc

    Dim oT As Object, oSHA1 As Object
    Dim TextToHash() As Byte
    Dim bytes() As Byte

    Set oT = CreateObject("System.Text.UTF8Encoding")
    Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")

    TextToHash = oT.GetBytes_4(sIn)
    bytes = oSHA1.ComputeHash_2((TextToHash))

    If bB64 = True Then
       SHA1 = ConvToBase64String(bytes)
    Else
       SHA1 = ConvToHexString(bytes)
    End If

    Set oT = Nothing
    Set oSHA1 = Nothing

End Function
Private Function ConvToBase64String(vIn As Variant) As Variant

    Dim oD As Object

    Set oD = CreateObject("MSXML2.DOMDocument")
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.base64"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToBase64String = Replace(oD.DocumentElement.Text, vbLf, "")

    Set oD = Nothing

End Function

Private Function ConvToHexString(vIn As Variant) As Variant

    Dim oD As Object

    Set oD = CreateObject("MSXML2.DOMDocument")

      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.Hex"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToHexString = Replace(oD.DocumentElement.Text, vbLf, "")

    Set oD = Nothing

End Function

我在代码中更改了这个

'Set oT = CreateObject("System.Text.UTF8Encoding")
'Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")
With New UTF8Encoding
    TextToHash = .GetBytes_4(sIn)
End With

With New SHA1Managed
    bytes = .ComputeHash_2((TextToHash))
End With

现在我遇到了同样的错误,但在使用新的 SHA1Managed

这就是我现在拥有代码的方式,但在 With New SHA1Managed 上仍然出现错误

公共函数 SHA1(sIn As String, Optional bB64 As Boolean = 0) As String '设置参考

'Test with empty string input:
'40 Hex:   da39a3ee5e6...etc
'28 Base-64:   2jmj7l5rSw0yVb...etc

Dim oT As Object, oSHA1 As Object
'Dim TextToHash() As Byte
'Dim bytes() As Byte

'Set oT = CreateObject("System.Text.UTF8Encoding")
'Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")
With New UTF8Encoding
    TextToHash = .GetBytes_4(sIn)
End With

With New SHA1Managed
    bytes = .ComputeHash_2((TextToHash))
End With

TextToHash = oT.GetBytes_4(sIn)
bytes = oSHA1.ComputeHash_2((TextToHash))

If bB64 = True Then
   SHA1 = ConvToBase64String(bytes)
Else
   SHA1 = ConvToHexString(bytes)
End If

Set oT = Nothing
Set oSHA1 = Nothing

结束函数

我更改了对 v2.05 mscorlib.tlb 的引用,但仍然遇到同样的错误

【问题讨论】:

  • 你得到的确切错误是什么?
  • 运行时错误 '-2146233079 (80131509': 自动化错误
  • en.wikibooks.org/wiki/Visual_Basic_for_Applications/… ,这是我顺便找到这段代码的地方
  • 既然设置了对 mscorlib 的引用,为什么还要使用后期绑定?
  • 除了@Mat'sMug 对后期绑定和早期绑定的解释之外,还有大量文章可以通过互联网搜索获得。如果您打算在 VBA 中进行任何高级编程,那么您需要了解它。

标签: .net excel vba sha1


【解决方案1】:

如果您要引用类型库,则无需使用 CreateObject 在该库中创建任何内容的实例。

我不太了解后期绑定 – Alex Mikhaylov 7 分钟前

这就是它发生的地方:

Dim oT As Object, oSHA1 As Object

通过处理Object 接口,VBA 没有对象成员的编译时知识;对它的调用将是后期绑定,即在运行时解决。您不会获得 IntelliSense,并且 VBA 将编译您在调用其成员时犯的任何错字或错误:您只会在运行时知道有问题。

Early-binding 意味着您正在使用 VBA 在编译时知道的类型:您获得 IntelliSense 的所有内容,而 VBA 将拒绝编译错字,或缺少非可选参数的成员调用。

然后是这样的:

Sub test()

    Dim inputBytes() As Byte
    With New UTF8Encoding
        inputBytes = .GetBytes_4("TEST")
    End With

    Dim outputBytes() As Byte
    With New SHA1Managed
        outputBytes = .ComputeHash_2((inputBytes))
    End With

    Debug.Print ConvToHexString(outputBytes)

End Sub

注意With New 块负责创建和销毁对象引用,因此您甚至不需要它们的局部变量。


使用 .NET 2.0 框架 中对 mscorlib.tlb 的引用进行测试(显然适用于 32 位或 64 位版本)- 即时窗格 (Ctrl+G) 输出:

Sheet1.Test
984816fd329622876e14907634264e6f332e9fb3

尝试删除引用,确认对话框,然后再次打开它并浏览C:\Windows\Microsoft.NET\Framework\v2.0.50727,然后选择mscorlib.tlb(不是.dll)。

【讨论】:

  • 我做了这些改变 'Set oT = CreateObject("System.Text.UTF8Encoding") 'Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed") With New UTF8Encoding TextToHash = .GetBytes_4( sIn) End With New SHA1Managed bytes = .ComputeHash_2((TextToHash)) End With
  • 老兄,Newing 对象的全部意义在于您不需要CreateObject 它们。抱歉,如果我的回答不够清楚...只需删除这些 CreateObject 调用,并通过查看对象浏览器中该 mscorlib 类型库可用的内容来验证您使用的类型是否正确 (F2)。
  • 嘿,非常感谢大家的帮助,我应该如何处理代码
  • 删除CreateObject 调用和局部变量
  • 我更新了代码,但在使用新的 SHA1Managed 时遇到了同样的错误
猜你喜欢
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 2018-08-18
  • 2013-06-30
  • 2020-12-22
  • 1970-01-01
相关资源
最近更新 更多