【发布时间】:2019-10-10 03:39:51
【问题描述】:
我在 C# 上有一个代码,我试图重写为 PHP,当涉及到加密时,我的 PHP 结果与 C# 代码生成的数据库中的哈希不匹配
public sealed class MD5Encryption
{
[DebuggerNonUserCode]
public MD5Encryption()
{
}
public static string Encode(string message)
{
return Base64.ConvertToBase64(new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(message)));
}
public static string EncodeWithSalt(string message, string salt)
{
return MD5Encryption.Encode(salt + message);
}
}
这是一个 C# ConvertToBase64
public static string ConvertToBase64(byte[] inputBytes)
{
return Convert.ToBase64String(inputBytes, 0, inputBytes.Length);
}
$string='6ec95f40-9fe3-4014-87d6-40c3b1fff77e'.'Danil18';
$strUtf8 = mb_convert_encoding($string, "UTF-8");
$encoded=md5($strUtf8);
$value=unpack('H*', $encoded);
echo base64_encode($encoded);//doesn't match maIdHxLbyqD2WkntiLGh2w==
如代码salt所示为6ec95f40-9fe3-4014-87d6-40c3b1fff77e
通过是Danil18。
DB值maIdHxLbyqD2WkntiLGh2w==,
PHP 输出OTlhMjFkMWYxMmRiY2FhMGY2NWE0OWVkODhiMWExZGI=
这段代码是否正确,我在 C# 类中缺少一些文本转换?
更新: 在深入研究 C# base64 后,这段代码仍然没有输出相同的结果
$string='6ec95f40-9fe3-4014-87d6-40c3b1fff77e'.'Danil18'; //doesn't match maIdHxLbyqD2WkntiLGh2w==
$string='e734cc98-71bd-45ca-b02c-3b0cf020eb6d'.'x160126@nwytg.net'; //KNv0/uYGHDYuSRxvgYdPoQ==
$strUtf8 = mb_convert_encoding($string, "UTF-8");
$encoded=md5($strUtf8);
//$value=unpack('H*', $encoded);
$value=unpack('C*', $encoded);
$chars = array_map("chr", $value);
$bin = join($chars);
$hex = bin2hex($bin);
//$bin = hex2bin($value);
//print_r($value);
echo base64_encode($hex);//doesn't match maIdHxLbyqD2WkntiLGh2w== , KNv0/uYGHDYuSRxvgYdPoQ==
【问题讨论】:
-
$value=unpack('H*', $encoded);- 如果你之后不使用 $value 做任何事情,那应该有什么好处? -
@04FS 我试过base64 $value[1] 结果还是不一样
-
你 shure
new MD5CryptoServiceProvider().ComputeHash只是在做 md5() 吗? var_dump(base64_decode("maIdHxLbyqD2WkntiLGh2w==")); php shell code:1: string(16) "���ʠ�ZI툱��" 这不是 md5 哈希 -
MD5 多年前已被贬低为加密哈希函数。不要将其用于文字或安全性。仅用于其他非加密目的
-
@myxaxa 我在 ConvertToBase64 中找到它,将此添加到问题代码中
标签: c# php utf-8 md5 cryptographic-hash-function