How to make PHP MD5 and C # MD5 consistent?

Source: Internet
Author: User
There is the code for C # generation MD5 as follows:

   class CreateMD5    {        static void Main(string[] args)        {            string source = "提问指南";            using (MD5 md5Hash = MD5.Create())            {                string hash = GetMd5Hash(md5Hash, source);                Console.WriteLine( hash);            }        }            static string GetMd5Hash(MD5 md5Hash, string input)        {            //这里是 Unicode            byte[] data = md5Hash.ComputeHash(Encoding.Unicode.GetBytes(input));            StringBuilder sBuilder = new StringBuilder();            for (int i = 0; i < data.Length; i++)            {                sBuilder.Append(data[i].ToString("x2"));            }            return sBuilder.ToString();        }    }

The MD5 generated by the above code is F5DA53705563C657581A6D0853286FDC
The problem now is that the MD5 generated by C # is inconsistent with the MD5 generated by PHP

Because of business constraints, you cannot change C # code, only from PHP.

Reply content:

There is the code for C # generation MD5 as follows:

   class CreateMD5    {        static void Main(string[] args)        {            string source = "提问指南";            using (MD5 md5Hash = MD5.Create())            {                string hash = GetMd5Hash(md5Hash, source);                Console.WriteLine( hash);            }        }            static string GetMd5Hash(MD5 md5Hash, string input)        {            //这里是 Unicode            byte[] data = md5Hash.ComputeHash(Encoding.Unicode.GetBytes(input));            StringBuilder sBuilder = new StringBuilder();            for (int i = 0; i < data.Length; i++)            {                sBuilder.Append(data[i].ToString("x2"));            }            return sBuilder.ToString();        }    }

The MD5 generated by the above code is F5DA53705563C657581A6D0853286FDC
The problem now is that the MD5 generated by C # is inconsistent with the MD5 generated by PHP

Because of business constraints, you cannot change C # code, only from PHP.

MD5 one step before the operation

$tmp = mb_convert_encoding('提问指南', 'utf-16le', 'utf8');
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.