Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

   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

  • Administrators
Posted

So, so close - took me ages to hit on the correct way in MSDN

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Posted

Thanks!

 


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!

Posted

Hi All,

 

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

 

   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.

  • 3 weeks later...
Posted

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)

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..

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...