Skip to content

Instantly share code, notes, and snippets.

@ken-itakura
Created November 8, 2016 12:58
Show Gist options
  • Save ken-itakura/c35455bccbae1544189e37b713698b75 to your computer and use it in GitHub Desktop.
Save ken-itakura/c35455bccbae1544189e37b713698b75 to your computer and use it in GitHub Desktop.
Excel, Access VBA Function to create MD5 for text
Public Function MD5Hex(textString As String) As String   
Dim enc   
Dim textBytes() As Byte   
Dim bytes   
Dim outstr As String       
Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")   
textBytes = textString   
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
@dandfra
Copy link

dandfra commented Jan 21, 2022

it didn't work for me, I had to explicitly convert the string to bytes using an UTF-8 encoding to get the correct result:

Public Function MD5Hex(textString As String) As String
  Dim enc
  Dim textBytes() As Byte
  Dim bytes
  Dim outstr As String
  
  Set encoder = CreateObject("System.Text.UTF8Encoding")
  Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  textBytes = encoder.GetBytes_4(textString)
  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
  Set encoder = Nothing
  
End Function

@volvisti
Copy link

Thanks ken-itakura and dandfra for the code.
By using the description on dummies.com I was able to insert a new column containing the crypted password in excel.
I'm not a programmer, more a "copy an paste tinkerer".
So I'm gently asking you for help to get the function as a button in a form into access.
The comlumns I use is passwort as source and cpasswort as target.

Can you please help me with that, if it's not too much time and effort.

Thanks and have a nice day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment