欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

如何使php的MD5与C#的MD5一致?

程序员文章站 2022-04-16 17:39:55
...
有c#生成MD5的代码如下:
   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 

上述代码生成的MD5是 f5da53705563c657581a6d0853286fdc
现在问题是,c#生成的MD5 与 PHP 生成的MD5 不一致

由于业务限制,不能更改c#代码,只能从PHP下手。

回复内容:

有c#生成MD5的代码如下:

   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 

上述代码生成的MD5是 f5da53705563c657581a6d0853286fdc
现在问题是,c#生成的MD5 与 PHP 生成的MD5 不一致

由于业务限制,不能更改c#代码,只能从PHP下手。

md5前操作一步

$tmp = mb_convert_encoding('提问指南', 'utf-16le', 'utf8');
相关标签: php md5