C# 提供与c相同结果的php md5算法#
我在C#中有一个哈希算法,简而言之,它是:C# 提供与c相同结果的php md5算法#,c#,php,md5,C#,Php,Md5,我在C#中有一个哈希算法,简而言之,它是: string input = "asd"; System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create(); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); byte[] hash = alg.ComputeHash(enc.GetBytes(input)); string
string input = "asd";
System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] hash = alg.ComputeHash(enc.GetBytes(input));
string output = Convert.ToBase64String(hash);
// outputs: eBVpbsvxyW5olLd5RW0zDg==
Console.WriteLine(output);
现在我需要在php中复制这种行为
$input = "asd";
$output = HashSomething($input);
echo $output;
我怎样才能做到呢
我查过了
- md5
- utf8_解码
- utf8_编码
- base64_编码
- base64_解码
- url_解码
注意:我无法更改C#行为,因为它已经用此算法实现,并且密码保存在我的数据库中。您记得用php对md5哈希进行base64编码吗
$result=base64_编码(md5($password,true))代码>
第二个参数使md5返回原始输出,这与您在C#中使用的函数相同,您的C#代码从字符串中获取UTF8字节;计算md5并存储为base64编码。因此,您应该在php中执行相同的操作,应该是:
$hashValue = base64_encode(md5(utf8_decode($inputString)))
问题是PHP的md5()
函数默认情况下返回哈希的十六进制变量,其中C#返回原始字节输出,然后必须使用base64编码使其文本安全。如果您正在运行PHP5,则可以使用base64_编码(md5('asd',true))
。请注意,md5()
的第二个参数为true,这使得md5()
返回原始字节而不是十六进制。我也遇到了同样的问题……仅使用md5($myvar)就可以了。我得到了与C#和PHP相同的结果。加文·肯德尔的帖子帮助了我。我希望这能帮助别人
对于php,应该如下所示
php -r "echo base64_encode(md5(utf8_encode('asd'),true));"
该死。你跟我打赌。。。==通常来自Base64编码,您正在使用Convert.ToBase64String()进行操作,这就是您缺少的吗?是的,但是如何在php上实现它?我应该使用什么函数?md5的真标志只存在于PHP5或更高版本中。在早期版本中,您需要调用pack函数。
php -r "echo base64_encode(md5(utf8_encode('asd'),true));"