PHP MD5 Hash and .net MD5 not matching

Posted on 2011-10-12
Last Modified: 2012-05-12

I have to get data from an external PHP site to my .net site.

The developer uses the following PHP code to get a hash:

md5(md5($var1). $var2)


I am trying to get the same hash in vb .net but the hashes are different:

  Dim var1 As String = "1318437972"
  Dim var2 As String = "01CdzTgUNgxIg"
  Dim MD5Time As String = MD5(var1) & var2
  Dim MD5Out As String = MD5(MD5Time)


PHP Hash is 06b27a58c07faee223d69ca9e4a87163
VB Hash is 5750b2229f7d00f955b235616f633fdc

PS  - My MD5 Function is provided below

Thanks in advance


Private Function MD5(ByVal str As String) As String

        Dim provider As MD5CryptoServiceProvider
        Dim bytValue() As Byte
        Dim bytHash() As Byte
        Dim strOutput As String = ""
        Dim i As Integer

        provider = New MD5CryptoServiceProvider()

        bytValue = System.Text.Encoding.UTF8.GetBytes(str)

        bytHash = provider.ComputeHash(bytValue)


        For i = 0 To bytHash.Length - 1
            strOutput &= bytHash(i).ToString("x").PadLeft(2, "0")

        Return strOutput

    End Function

Open in new window

Question by:doctor069
    LVL 7

    Expert Comment

    I don't know much about php either, but one obvious thing that stands out is that it you are using utf-8 encoding and then getting the hex code for each byte..  I don't know if php uses utf-8 and I don't know if you want to be converting it to hex.  PHP might be encoding it as a base64 string, which sounds reasonable given its general web development use.  Also, I don't know if the "dot" is appending the string in php, so my appologies about not knowing for sure what I'm talking about.

    Here is what my hash function looks like:
    Public Function GenerateHash(ByVal aString As String, ByVal encoding As Text.Encoding, ByVal hashAlgorithm As HashAlgorithm) As String
        If encoding Is Nothing Then encoding = New Text.UTF8Encoding()
        If hashAlgorithm Is Nothing Then hashAlgorithm = New MD5CryptoServiceProvider()
        Dim byteSourceText() As Byte = encoding.GetBytes(aString)
        Dim hashBytes() As Byte = hashAlgorith.ComputeHash(byteSourceText)
        Return Convert.ToBase64String(hashBytes)
    End Function

    Open in new window

    I don't know if that helps you, but it might be worth a shot ;) Obviously you don't need to take in the optional encoding and hashAlgorithm arguments, that just a straight copy and paste from my personal encryption library..

    Instead of base64 encoding it you might try the literal string value of the bytes (again, you may want to check the encoding).
    Return encoding.GetString(hashBytes)

    Open in new window

    best of luck.
    LVL 82

    Expert Comment

    by:Dave Baldwin

    Accepted Solution

    Thanks -

    No matter what I do I always get...

    VB Hash is 5750b2229f7d00f955b235616f633fdc

    I think the issue is in the "double" hashing

    md5(md5($var1). $var2)

    From my asp .net eyes this looks like:
    $var1 get hashed first
    then we append $var2
    and hash again

    But I suspect that that is not the case in PHP


    Author Closing Comment

    Going to close this and try it in the PHP group

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now