By using VBA automatic data conversion, from string to array of bytes, like :
Dim textBytes() As Byte
textBytes = textString
the result (array of bytes), is in unicode format (2 bytes for each character).
You have to to discard every even bytes (only take the odd bytes), like this :
Public Function MD5Hex(textString As String) As String
Dim enc
Dim pos As Long
Dim tempBytes() As Byte
Dim textBytes() As Byte
Dim bytes
Dim outstr As String
Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
'textBytes = textString
tempBytes = textString
ReDim textBytes(0 To UBound(tempBytes) \ 2)
For pos = 0 To UBound(textBytes)
textBytes(pos) = tempBytes(pos * 2)
Next pos
bytes = enc.ComputeHash_2(textBytes)
For pos = 1 To LenB(bytes)
outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
Next
MD5Hex = outstr
Set enc = Nothing
End Function
Sub Test()
Debug.Print MD5Hex("Hello World")
End Sub
Bookmarks