我跑這個模塊,但它不斷給我一個錯誤和調試送我去這條線:Excel的VBA SHA1功能
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
這是我怎麼有碼現在,但仍然出現與新的SHA1管理錯誤
公共功能SHA1(SIN作爲字符串,可選bB64由於布爾= 0)作爲字符串 「設置refe
'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':自動化錯誤 –
https://en.wikibooks.org/wiki/Visual_Basic_for_Applications/String_Hashing_in_VBA,這是我發現此代碼的方式 –