Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have used a function to MD5 hash a password, however, the results are not correct:

 

Correct Result--> a985c9764e0e6d738ff20f2328a0644b

My Result --> a985c9764ee6d738ff2f2328a0644b

 

Correct Result--> 098f6bcd4621d373cade4e832627b4f6

My Result --> 98f6bcd4621d373cade4e832627b4f6

 

Private Function GetPasswordHash(ByVal Password As String) As String
       Dim MD5Hasher As New System.Security.Cryptography.MD5CryptoServiceProvider
       Dim Encoder As New System.Text.ASCIIEncoding
       Dim FinalString As String
       FinalString = ToHexString(MD5Hasher.ComputeHash(Encoder.GetBytes(Password)))
       Return FinalString
   End Function

   Private Function ToHexString(ByVal ByteArray As Byte()) As String
       Dim i, j, k As Integer
       Dim thisstring As StringBuilder = New StringBuilder
       j = ByteArray.GetLowerBound(0)
       k = ByteArray.GetUpperBound(0)
       For i = j To k
           thisstring.Append(Hex(ByteArray(i)))
       Next
       Return thisstring.ToString
   End Function

 

I think the problem is that somewhere inside the ByteArray, it is dropping the leading 0s, as in changing:

 

09

 

to

 

9

 

And thereby screwing up the process... can anyone help? :)

Posted

try this:

 

    Private Function ToHexString(ByVal ByteArray As Byte()) As String
       Dim i, j, k As Integer
       Dim str As String
       Dim thisstring As System.Text.StringBuilder = New System.Text.StringBuilder()
       j = ByteArray.GetLowerBound(0)
       k = ByteArray.GetUpperBound(0)
       For i = j To k
           str = Convert.ToString(ByteArray(i), 16)
           If str.Length < 2 Then str = "0" + str
           thisstring.Append(str)
       Next
       Return thisstring.ToString
   End Function

 

hope this helps!

 

Andreas

Posted

Thanks m8, but I already found the solution myself :)

 

       Dim md5Hasher As New MD5CryptoServiceProvider

       Dim hashedDataBytes As Byte()
       Dim encoder As New UTF8Encoding

       hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(TextBox1.Text))

       Dim b As Byte
       For Each b In hashedDataBytes
           Label1.Text &= String.Format("{0:X2}", b)
       Next b

 

Does the trick nicely :)

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