当前位置: 首页 > 编程语言 > C#

phpmd5algorithmwhichgivesthesameresultasc#share

时间:2023-04-10 15:25:15 C#

:stringinput="asd";System.Security.Cryptography.MD5alg=System.Security.Cryptography.MD5.Create();System.Text.UTF8Encodingenc=newSystem.Text.UTF8Encoding();byte[]hash=alg.ComputeHash(enc.GetBytes(input));字符串输出=Convert.ToBase64String(hash);//输出:eBVpbsvxyW5olLd5RW0zDg==Console.WriteLine(output);现在我需要在php中复制这种行为,$input="asd";$output=HashSomething($input);回声$输出;我怎样才能实现它?我检查了,但我注意到phpmd5没有到达最后==我错过了什么?注意:我无法更改C#行为,因为它已经实现并且使用此算法将密码保存在我的数据库中。问题是PHP的md5()函数默认返回哈希的十六进制变体,其中C#返回原始字节输出,然后必须对其进行base64编码以确保文本安全。如果您运行的是PHP5,则可以使用base64_encode(md5('asd',true))。请注意,md5()的第二个参数为真,这使得md5()返回原始字节而不是十六进制。你还记得在php中使用base64编码的md5哈希吗?$result=base64_encode(md5($password,true));第二个参数使md5返回原始输出,这与您在C#中使用的功能相同您的C#代码从字符串中获取UTF8字节;计算md5并存储为base64编码。所以你应该在php中做同样的事情,它应该是:$hashValue=base64_encode(md5(utf8_decode($inputString)))它应该像下面的php-r"echobase64_encode(md5(utf8_encode('asd'),true));"我有同样的问题......只是使用md5($myvar)工作。我用C#和PHP得到了相同的结果。GavinKendall发帖帮助我。我希望这对其他人有帮助。http://jachman.wordpress.com/2006/06/06/md5-hash-keys-with-c/以上是C#学习教程:phpmd5算法,给出和c#一样的结果分享全部内容,如果对大家有用,需要了解更多的C#学习教程。希望大家多多关注——返回System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))),“-”,“”);}本文收集自网络,不代表立场,如有侵权请点右联系管理员删除。如需转载请注明出处: