【问题标题】:system.security.cryptography.SHA256Managed in .NET 4.5 (Windows 10)system.security.cryptography.SHA256Managed in .NET 4.5 (Windows 10)
【发布时间】:2017-07-20 21:33:56
【问题描述】:

我正在使用以下 VBA 函数来获取文件的 SHA256 值;

Public Function FileToSHA256(sfilename As String) As String
    Dim enc
    Dim bytes
    Dim outstr As String
    Dim pos As Integer
    Set enc = CreateObject("System.Security.Cryptography.SHA256Managed")
    'Convert the string to a byte array and hash it
    bytes = GetFileBytes(sfilename)
    bytes = enc.ComputeHash_2((bytes))
    'Convert the byte array to a hex string
    For pos = 1 To LenB(bytes)
        outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
    Next
    FileToSHA256 = outstr 'Returns a 40 byte/character hex string
    Set enc = Nothing
End Function

Private Function GetFileBytes(ByVal path As String) As Byte()
    Dim lngFileNum As Long
    Dim bytRtnVal() As Byte
    lngFileNum = FreeFile
    If LenB(Dir(path)) Then ''// Does file exist?
        Open path For Binary Access Read As lngFileNum
        ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise 53
    End If
    GetFileBytes = bytRtnVal
    Erase bytRtnVal
End Function

这在 Word 2013 上的 Windows 7 环境下完美运行。我不知道它运行的 .NET 版本(假设为 3.5)。

它现在已在 Windows 10 环境(仍为 Word 2013)上启动。我们的 IT 部门告诉我这是在 .NET 4.5 上,但根据 VBA,它是从 .NET 框架 4.0.30319 运行的。

现在抛出错误 -

运行时错误 '-2146232576 (80131700)' 自动化错误

在这条线上;

Set enc = CreateObject("System.Security.Cryptography.SHA256Managed")

我在项目中引用了 MSCORLIB.DLL。

我不知道是否需要添加额外的引用或更改代码。

如果需要更改代码,则还需要覆盖 .NET 的早期版本,并通过案例检查来覆盖不同的版本,但我不知道该怎么做。

【问题讨论】:

  • 在黑暗中拍摄,但您是否尝试从 C:\Windows\Microsoft.NET\Framework64\v4.0.30319 添加 mscorlib?
  • 恐怕这行不通。可能是因为它在公司环境中,所以我对自己拥有的访问权限有点锁定。我已经设法通过一起规避 mscorlib 来解决这个问题。

标签: .net vba sha256


【解决方案1】:

我们在使用 Windows 10 和 .NET Framework 4.8 时遇到了同样的问题。解决方案是从 Windows 功能 (image of the Windows Features dialog) 添加 .NET Framework 3.5。无需更改代码。

微软.NET Framework 3.5安装说明:https://docs.microsoft.com/en-us/dotnet/framework/install/dotnet-35-windows-10

【讨论】:

    【解决方案2】:

    我一直无法让 mscorlib 工作。但是我找到了一个可以一起规避这一切的模块。它可能对其他有限/无法访问完整 .NET 框架的人有用。

    http://khoiriyyah.blogspot.com/2012/06/vb6-hash-class-md5-sha-1-sha-256-sha.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多