views:

1074

answers:

2

Hi,

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

+9  A: 

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();

    }
Daniel
That doesn't produce the same results as the http://www.functions-online.com/md5.html website though.
Tunic
Then you are doing something wrong - it seems to produce exactly the same results. I would suggest you check your whitespace and character set - you are most likely inserting something like an extra new line, or you are using unicode characters that are being treated differently by the online form (either treated with a different encoding, or are being dropped entirely)
David
Perhaps you are appending a line terminator? Perhaps you are mangling the input by passing binary data through a UTF-8 encoder? I did check the functions-online site and pass in a test vector of one value -- MD5("abc") = 900150983cd24fb0d6963f7d28e17f72 which it does compute correctly.
Liudvikas Bukys
A: 

What makes the "functions-online" site (http://www.functions-online.com/md5.html) an authority on MD5? For me, it works OK only for ISO-8859-1. But when I try pasting anything other than ISO-8859-1 into it, it returns the same MD5 hash. Try Cyrillic capital B by itself, code point 0x412. Or try Han Chinese symbol for water, code point 0x98A8. As far as I know, the posted C# applet is correct.