Encryption

使用“Cryptography.HMACSHA1”加密,如何解密(VBA)

  • May 5, 2016

我有一個在 VBA 中執行良好的加密常式。但現在我需要編寫解密程序,我被困住了。對於加密,我使用 HMACSHA1 並將加密字節傳輸到 base 64。我可以解碼 base 64 並返回相同的字節數組,但是關於如何使用 HMACSHA1 解密有什麼建議嗎?到目前為止,這是我所擁有的(抱歉,如果這不是提出此類問題的合適場所):

Private Function fDecrypto(sText As String, sPass As String) As String

Dim encoder As Object
Dim crypto As Object
Dim bEncrypted() As Byte
Dim i As Integer

' base64 to encrypted byte array

Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument
Set objNode = objXML.createElement("b64")

objNode.DataType = "bin.base64"
objNode.Text = sText
bEncrypted = objNode.nodeTypedValue

Set objNode = Nothing
Set objXML = Nothing

' decrypt using HMACSHA1

Set encoder = CreateObject("System.Text.UTF8Encoding")
Set crypto = CreateObject("System.Security.Cryptography.HMACSHA1")

' **TODO - decrypt here**

Set encoder = Nothing
Set crypto = Nothing

End Function


Private Function fEncrypto(sText As String, sPass As String) As String

Dim encoder  As Object
Dim crypto As Object
Dim i As Integer
Dim bPass() As Byte
Dim bText() As Byte
Dim bEncrypted() As Byte

Set encoder = CreateObject("System.Text.UTF8Encoding")
Set crypto = CreateObject("System.Security.Cryptography.HMACSHA1")

bText = encoder.Getbytes_4(sText)
bPass = encoder.Getbytes_4(sPass)
crypto.key = bPass

bEncrypted = crypto.ComputeHash_2(bText)

Set encoder = Nothing
Set crypto = Nothing

' encrypted byte array to base64

Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument
Set objNode = objXML.createElement("b64")

objNode.DataType = "bin.base64"
objNode.nodeTypedValue = bEncrypted
EncodeBase64 = objNode.Text

Set objNode = Nothing
Set objXML = Nothing

End Function

HMAC 是消息認證碼 (MAC)。MAC 產生“標籤”,可用於防止消息被篡改(有點像數字簽名)。沒有辦法“解密”一個 HMAC 標籤來取回原始消息,所以聽起來 HMAC 不是你要找的。

我建議改用 AES-GCM,因為它旨在用於加密。我不確定如何使用 VBA 中的 AES-GCM:我建議在 StackOverflow 上提出這個問題(這個問題在這裡是題外話)。

引用自:https://crypto.stackexchange.com/questions/35097