MD5 Encryption

sdlangers

Centurion
Joined
Dec 3, 2002
Messages
118
Hi,

I'm trying to use the MD5CryptoServiceProvider class to create the hash code for a string.

However, it seems i can only return it as a base64 code, and i need the string as hex.

How can i convert it?

Thanks.

Here is my code:

Visual Basic:
    Private Function computeMD5(ByVal plainText As String) As String
        Dim ue As New UnicodeEncoding
        Dim bytes() As Byte = ue.GetBytes(plainText)
        Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider
        Dim hashBytes As Byte()

        hashBytes = md5.ComputeHash(bytes)

        ' Convert result into a base64-encoded string.
        Dim hashString As String

        hashString = Convert.ToBase64String(hashBytes)

        ' Return the result
        Return hashString
    End Function
 
So, so close - took me ages to hit on the correct way in MSDN
Visual Basic:
Private Function computeMD5(ByVal plainText As String) As String
        Dim ue As New UnicodeEncoding
        Dim bytes() As Byte = ue.GetBytes(plainText)
        Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider
        Dim hashBytes As Byte()

        hashBytes = md5.ComputeHash(bytes)

        ' Convert result into a base64-encoded string.
        Dim hashString As String

        hashString = BitConverter.ToString(hashBytes ).ToLower().Replace("-", "")     'This line will do the trick

        ' Return the result
        Return hashString
    End Function
 
Hi,

Thanks for your reply.

This doesnt seem to be what i need either. Problem is, i'm using it to pass to an interface which checks my md5 encrypted string. this means it has to be the exact implementation as theirs.

Theirs is as per the standard MD5

if you look at this site -
http://bfl.rctek.com/tools/?tool=hasher

it calculates the string correctly.

using the above code - we get a different string

Thanks for your help.
 
Thanks!

Visual Basic:
Private Function computeMD5(ByVal plainText As String) As String
        Dim ue As New UnicodeEncoding
        Dim bytes() As Byte = ue.GetBytes(plainText)
        Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider
        Dim hashBytes As Byte()


        hashBytes = md5.ComputeHash(bytes)

        ' Convert result into a base64-encoded string.
        Dim hashString As String

        Dim i As Integer
        For i = 0 To UBound(hashBytes)
            hashString += Convert.ToString(hashBytes(i), 16)
        Next

        ' Return the result
        Return hashString
    End Function

That seems to convert it alright.. but now its back to the same problem.. the string i get is different.

anyone know what is going on?

If you look at the above link and type in a simple string say "test", the encrypted string is:

098f6bcd4621d373cade4e832627b4f6

but the above function returns:

c859e2ec7419f59e79d7f1b774bfe6

????

thanks for your help!
 
Hi All,

OK - i solved it - you need to read in the plainText as UTF8 encoding.. The following gives the correct result:

Visual Basic:
    Private Function computeMD5(ByVal plainText As String) As String
        Dim ue As New UTF8Encoding      ' THIS WAS THE PROBLEM - needs to be UTF8
        Dim bytes() As Byte = ue.GetBytes(plainText)
        Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider
        Dim hashBytes() As Byte

        hashBytes = md5.ComputeHash(bytes)

        Dim hashString As String
        Dim i As Integer
        For i = 0 To UBound(hashBytes)
            hashString += Convert.ToString(hashBytes(i), 16)
        Next

        ' Return the result
        Return hashString
    End Function

hope this helps someone else.
 
A while ago I was trying to do the same thing (only in another language). I was hashing credit card numbers, and don't think I would have found it had it not been for the million+ records I was parsing and hashing.

one thing you need to add to your hash function is inside your for loop (sorry I don't know VB so I'm taking an honest stab at it :p)
Visual Basic:
Private Function computeMD5(ByVal plainText As String) As String
        Dim ue As New UTF8Encoding  
        Dim bytes() As Byte = ue.GetBytes(plainText)
        Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider
        Dim hashBytes() As Byte

        hashBytes = md5.ComputeHash(bytes)

        Dim hashString As String
        Dim i As Integer
        For i = 0 To UBound(hashBytes)
            ' ---- here ----
            Dim tempString As String
            tempString = Convert.ToString(hashBytes(i), 16)
            If (tempString.Length.Equals(0)) Then
                hashString += "00"
            ElseIf (tempString.Length.Equals(1)) Then
                hashString += "0" + tempString
            Else
                hashString += tempString
            End If
            ' ---- to here ----
        Next
        Return hashString
    End Function

I found that if the byte sometimes is null (or nothing) and won't be converted to zero's, so you have to do it manually. this fixes the problem..
 
Back
Top