2017-03-01 118 views
3

我正在使用以下VBA函数来获取文件的SHA256值;system.security.cryptography.SHA256在.NET 4.5(Windows 10)中管理

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 

这完全适用于2013年的Word在Windows 7环境下,我不知道(假设3.5)的.NET版本上运行。

它现在已经启动到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的早期版本,一些情况检查涵盖不同的版本,但我不知道如何执行此操作。

+0

瞎猜,但你尝试添加从'C的mscorlib程序:\ WINDOWS \ Microsoft.NET \ Framework64 \ v4.0.30319'? –

+0

恐怕没有用。这可能是因为它在一个企业环境中,所以我对我有什么访问权限有点锁定。我设法通过绕过mscorlib来整理这个问题。 –

回答