【发布时间】: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 来解决这个问题。