1

I'm trying to convert String to md5 that what i did :

  public static string MD5Hash(string input)
        {
            StringBuilder hash = new StringBuilder();
            MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
            byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(input));

            for (int i = 0; i < bytes.Length; i++)
            {
                hash.Append(bytes[i].ToString("x2"));
            }
            return hash.ToString();
        }

The code above whene i tested in MD5Hash("MyCode") its returned : 6403df8223bf310152ad235731e79902 (32 char)

but the md5 on my database is 0x6403DF8223BF310152AD235731E79902000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (64 char)

it's missing the "0X" in the begginig and the "zeros" in the ending

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Bfs
  • 25
  • 1
  • 7

4 Answers4

3

This is Working for me -

    public static string MD5Hash(string text)
    {
        MD5 md5 = new MD5CryptoServiceProvider();

        //compute hash from the bytes of text  
        md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));


        //get hash result after compute it  
        byte[] result = md5.Hash;

        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < result.Length; i++)
        {
            //change it into 2 hexadecimal digits  
            //for each byte  
            strBuilder.Append(result[i].ToString("x2"));
        }

        return strBuilder.ToString();
    }
1

The hash size for the MD5 algorithm is 128 bits.

The ComputeHash methods of the MD5 class return the hash as an array of 16 bytes. Note that some MD5 implementations produce a 32-character, hexadecimal-formatted hash.

So you have to check in the database why are you getting extra characters?

and for your information, the correct MD5 value for MyCode will be 8e995273698f53088fe2ad4a0888d213

Code to generate MD5 below.

public static string GenerateMD5(string input)
{
    
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = md5.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}

Reference: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5?view=netcore-3.1#remarks

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • thanks for replying . i just replace "MyCode" to my real code that's why – Bfs Jun 21 '20 at 12:19
  • i'm getting extra char bcuz the type of column is binary(64) – Bfs Jun 21 '20 at 12:20
  • so as suggested please change the length to 32, but still, I doubt why are you getting 0x – Vivek Nuna Jun 21 '20 at 12:22
  • and i can't change the type of the column in database bcuz i can't , i'm working on existing database . thanks – Bfs Jun 21 '20 at 12:23
  • the admin of database said that they where inserting the password as ***"HASHBYTES('md5','MyCode')"*** in sql using query – Bfs Jun 21 '20 at 12:25
  • then you don't need to generate the MD5 in the C# code. just pass the actual value and let the query convert it – Vivek Nuna Jun 21 '20 at 12:38
1

try this

    public static string Md5Hash(string text)
    {
        using (var md5 = new MD5CryptoServiceProvider())
        {
            using (var tds = new TripleDESCryptoServiceProvider())
            {
                tds.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(encryptionKey));
                tds.Mode = CipherMode.ECB;
                tds.Padding = PaddingMode.PKCS7;

                using (var transform = tds.CreateEncryptor())
                {
                    byte[] textBytes = UTF8Encoding.UTF8.GetBytes(text);
                    byte[] bytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length);
                    return Convert.ToBase64String(bytes, 0, bytes.Length);
                }
            }
        }
    }
Anahita Ameri
  • 11
  • 1
  • 2
0

According to your examples, what generates the 64-Chars strings in your database is something like:

public static string MD5Hash(string input)
{
    using (var md5 = new MD5CryptoServiceProvider())
        return string.Concat("0x",
            BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)))
            .Replace("-", string.Empty).PadRight(62, '0'));
}

I wouldn't name the function MS5Hash in this case and as you can see, the database strings are concatenated strings and the MD5 hash is just the middle part of each.