Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MD5 Hash results not expected result

I've tried every example I can find on the web but I cannot get my .NET code to produce the same MD5 Hash results from my VB6 app.

The VB6 app produces identical results to this site: http://www.functions-online.com/md5.html

But I cannot get the same results for the same input in C# (using either the MD5.ComputeHash method or the FormsAuthentication encryption method)

Please help!!!!

As requested, here is some code. This is pulled straight from MSDN:

    public string hashString(string input)
    {
        // Create a new instance of the MD5CryptoServiceProvider object.
        MD5 md5Hasher = MD5.Create();

        // Convert the input string to a byte array and compute the hash.
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data 
        // and format each one as a hexadecimal string.
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }

My test string is:

QWERTY123TEST

The results from this code is:

8c31a947080131edeaf847eb7c6fcad5

The result from Test MD5 is:

f6ef5dc04609664c2875895d7da34eb9

Note: The result from the TestMD5 is what I am expecting

Note: I've been really, really stupid, sorry - just realised I had the wrong input. As soon as I hard-coded it, it worked. Thanks for the help

like image 781
IThasTheAnswer Avatar asked Dec 03 '22 14:12

IThasTheAnswer


1 Answers

This is a C# MD5 method that i know works, i have used it to authenticate via different web restful APIs

   public static string GetMD5Hash(string input)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
        bs = x.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2").ToLower());
        }
        return s.ToString();

    }
like image 163
Daniel Avatar answered Dec 22 '22 17:12

Daniel