none
How to convert a md5 value to string

    Question

  • Hi, I want to convert a md5 value to string but i do not know how can do it
    i know how to convert a string value like "msdn" to a md5 value "8eedb43a46f91747432c28b51f6046"
    usin this code
    Public Function md5X(ByVal str As String) As String
            Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim ByteX As Byte() = md5.ComputeHash(System.Text.ASCIIEncoding.Default.GetBytes(str))
            Dim StringBuilderX As New System.Text.StringBuilder
            For i As Integer = 0 To ByteX.Length - 5
                StringBuilderX.Append(ByteX(i).ToString("x1"))
            Next
            Dim f As String
            f = StringBuilderX.ToString
            Return f
        End Function
    The Question is : how can convert a md5 value to a string value like a md5 value"8eedb43a46f91747432c28b" to string is "msdn"
    Sunday, September 06, 2009 4:56 PM

Answers

  • Try the below code:

    An alternate approach for decoding:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim source As String = "msdn"
            MessageBox.Show(source)
            Dim hash As String = getMd5Hash(source)
            'Returns true if both values are same!!
            MessageBox.Show(verifyMd5Hash("msdn", hash))
        End Sub
    
        Function getMd5Hash(ByVal input As String) As String
            ' Create a new instance of the MD5 object.
            Dim md5Hasher As MD5 = MD5.Create()
    
            ' Convert the input string to a byte array and compute the hash.
            Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))
    
            ' Create a new Stringbuilder to collect the bytes
            ' and create a string.
            Dim sBuilder As New StringBuilder()
    
            ' Loop through each byte of the hashed data 
            ' and format each one as a hexadecimal string.
            Dim i As Integer
            For i = 0 To data.Length - 1
                sBuilder.Append(data(i).ToString("x2"))
            Next i
    
            ' Return the hexadecimal string.
            Return sBuilder.ToString()
    
        End Function
    
        ' Verify a hash against a string.
        Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
            ' Hash the input.
            Dim hashOfInput As String = getMd5Hash(input)
            ' Create a StringComparer an compare the hashes.
            Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
            If 0 = comparer.Compare(hashOfInput, hash) Then
                Return True
            Else
                Return False
            End If
        End Function



    Thanks,
    Veera Reddy

    • Marked as answer by Jeff Shan Monday, September 14, 2009 1:14 AM
    Sunday, September 06, 2009 10:06 PM
  • Hi MEHDI3,

        Welcome to MSDN forums!
        As far as I know, different inputs can in theory return same MD5 hash (called a collision, because a MD5 hash doesn't use every letter of the alphabet  and it always have the same length,  so there's an infinite amount of strings for any one hash), so i’m afraid that you can’t be absolutely certain that you've determined the original string.
        You can refer to the following link for some information about MD5:   

     http://en.wikipedia.org/wiki/MD5


        But since collisions are very rare, so if you want to check whether a string is the original one, I think Kolan’s post can meet your requirement.


    Hope this helps

    Regards

    Jeff Shan
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Jeff Shan Monday, September 14, 2009 1:14 AM
    Tuesday, September 08, 2009 6:40 AM

All replies

  • Try the below code:

    An alternate approach for decoding:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim source As String = "msdn"
            MessageBox.Show(source)
            Dim hash As String = getMd5Hash(source)
            'Returns true if both values are same!!
            MessageBox.Show(verifyMd5Hash("msdn", hash))
        End Sub
    
        Function getMd5Hash(ByVal input As String) As String
            ' Create a new instance of the MD5 object.
            Dim md5Hasher As MD5 = MD5.Create()
    
            ' Convert the input string to a byte array and compute the hash.
            Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))
    
            ' Create a new Stringbuilder to collect the bytes
            ' and create a string.
            Dim sBuilder As New StringBuilder()
    
            ' Loop through each byte of the hashed data 
            ' and format each one as a hexadecimal string.
            Dim i As Integer
            For i = 0 To data.Length - 1
                sBuilder.Append(data(i).ToString("x2"))
            Next i
    
            ' Return the hexadecimal string.
            Return sBuilder.ToString()
    
        End Function
    
        ' Verify a hash against a string.
        Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
            ' Hash the input.
            Dim hashOfInput As String = getMd5Hash(input)
            ' Create a StringComparer an compare the hashes.
            Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
            If 0 = comparer.Compare(hashOfInput, hash) Then
                Return True
            Else
                Return False
            End If
        End Function



    Thanks,
    Veera Reddy

    • Marked as answer by Jeff Shan Monday, September 14, 2009 1:14 AM
    Sunday, September 06, 2009 10:06 PM
  • Hi MEHDI3,

        Welcome to MSDN forums!
        As far as I know, different inputs can in theory return same MD5 hash (called a collision, because a MD5 hash doesn't use every letter of the alphabet  and it always have the same length,  so there's an infinite amount of strings for any one hash), so i’m afraid that you can’t be absolutely certain that you've determined the original string.
        You can refer to the following link for some information about MD5:   

     http://en.wikipedia.org/wiki/MD5


        But since collisions are very rare, so if you want to check whether a string is the original one, I think Kolan’s post can meet your requirement.


    Hope this helps

    Regards

    Jeff Shan
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Jeff Shan Monday, September 14, 2009 1:14 AM
    Tuesday, September 08, 2009 6:40 AM