求大神帮忙相同的HEX字符串NET下得到的结果不一样。。

infinite2016 2016-03-27 08:44:11
字符串 7BBD37EC8530F606FD238DADA2295AD38649F39B

PHP下实现

$s = hex2bin('7BBD37EC8530F606FD238DADA2295AD38649F39B');
echo .sha1($s);

结果 013020a8b080a1edb49a9af0f51df0b07349e66a

NET 下就不一样了。
.NET下的 结果 F3BDE78A9EC17F9E6EFDC5369CE024AB692D5F46
代码


string h = To("7BBD37EC8530F606FD238DADA2295AD38649F39B");
Console.WriteLine(SHA1(h));
public string To(string hash)
{
string s = hash;
byte[] buff = new byte[s.Length / 2];
int index = 0;
for (int i = 0; i < s.Length; i += 2)
{
buff[index] = Convert.ToByte(s.Substring(i, 2), 16);
++index;
}
string result = Encoding.Default.GetString(buff);
// Console.Write(result);
return result;
}
private static string SHA1(string text)
{
byte[] cleanBytes = Encoding.Default.GetBytes(text);
byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes);
return BitConverter.ToString(hashedBytes).Replace("-", "");
}

...全文
177 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
infinite2016 2016-03-28
  • 打赏
  • 举报
回复
引用 10 楼 shingoscar 的回复:
[quote=引用 7 楼 infinite2016 的回复:] [quote=引用 6 楼 shingoscar 的回复:] php里的string对应到c#的类型是byte[] 那些转换都是多余的
BYTE[] 也试了不行的··没辙了才来的··[/quote] public string byte[] To(string hash) { string s = hash; byte[] buff = new byte[s.Length / 2]; int index = 0; for (int i = 0; i < s.Length; i += 2) { buff[index] = Convert.ToByte(s.Substring(i, 2), 16); ++index; } string result = Encoding.Default.GetString(buff); // Console.Write(result); return result; return buff; } private static string SHA1(string byte[] text) { byte[] cleanBytes = Encoding.Default.GetBytes(text); byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes text); return BitConverter.ToString(hashedBytes).Replace("-", ""); } 不知道你怎么试的,这样改结果就是一样的[/quote] 搞定了,谢谢你了。。
Poopaye 2016-03-28
  • 打赏
  • 举报
回复
引用 7 楼 infinite2016 的回复:
[quote=引用 6 楼 shingoscar 的回复:] php里的string对应到c#的类型是byte[] 那些转换都是多余的
BYTE[] 也试了不行的··没辙了才来的··[/quote] public string byte[] To(string hash) { string s = hash; byte[] buff = new byte[s.Length / 2]; int index = 0; for (int i = 0; i < s.Length; i += 2) { buff[index] = Convert.ToByte(s.Substring(i, 2), 16); ++index; } string result = Encoding.Default.GetString(buff); // Console.Write(result); return result; return buff; } private static string SHA1(string byte[] text) { byte[] cleanBytes = Encoding.Default.GetBytes(text); byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes text); return BitConverter.ToString(hashedBytes).Replace("-", ""); } 不知道你怎么试的,这样改结果就是一样的
infinite2016 2016-03-27
  • 打赏
  • 举报
回复
引用 8 楼 zanfeng 的回复:
比较两个算法吧。这个东西估计是算法就不一样吧。php的.net估计是有出入的。
Hello World! 48656c6c6f20576f726c6421 2ef7bde608ce5404e97d5f042f95f89f1c232871 这样的文本算出来又是对的·非常规文本的HEX算出来就不对,搞不懂了。
足球中国 2016-03-27
  • 打赏
  • 举报
回复
比较两个算法吧。这个东西估计是算法就不一样吧。php的.net估计是有出入的。
infinite2016 2016-03-27
  • 打赏
  • 举报
回复
引用 6 楼 shingoscar 的回复:
php里的string对应到c#的类型是byte[] 那些转换都是多余的
BYTE[] 也试了不行的··没辙了才来的··
Poopaye 2016-03-27
  • 打赏
  • 举报
回复
php里的string对应到c#的类型是byte[] 那些转换都是多余的
Poopaye 2016-03-27
  • 打赏
  • 举报
回复
是哪一不一样呢? 打错一个字
infinite2016 2016-03-27
  • 打赏
  • 举报
回复
引用 4 楼 shingoscar 的回复:
是哪一不一样呢? 打错一个字
string result = Encoding.Default.GetString(buff); 返回的字节减少了 改成 string result = Encoding.ASCII.GetString(buff); 后字节倒是没减少但是结果算出来还是不对。
infinite2016 2016-03-27
  • 打赏
  • 举报
回复
引用 1 楼 shingoscar 的回复:
是哪一部不一样呢?
.net 算出来的不一样。
infinite2016 2016-03-27
  • 打赏
  • 举报
回复
.NET 算出来的不一样。。
Poopaye 2016-03-27
  • 打赏
  • 举报
回复
是哪一部不一样呢?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧