【问题标题】:Diffrent result of md5 between php and c#php和c#之间md5的不同结果#
【发布时间】:2015-11-24 14:39:30
【问题描述】:

您好,我尝试将此部分代码从 php 转换为 c#,结果相同:

PHP

md5("apple", true) //result :8pѕ'OlIігg(•

C#

    byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes("apple");
    byte[] hashedBytes =  MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
    return System.Text.Encoding.ASCII.GetString(hashedBytes); //result: 8p?'OlI??g(?

类似但不完全一样

更新:BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); 我有:1f3870be274f6c49b3e31a0c6728957f

而且我使用 md5("apple", false) 时没有任何问题

【问题讨论】:

  • 您正在打印原始二进制垃圾,其中一些字符被您的输出环境误解了。例如一些字节序列看起来像多字节字符。你的 md5 很好,只是输出/显示问题。
  • 侧节点:MD5CryptoServiceProviderIDisposable,因此应该disposed

标签: c# php md5


【解决方案1】:

试试这个:

 var md5 = System.Security.Cryptography.MD5.Create();
 byte[] inputBytes = Encoding.Default.GetBytes(input);
 byte[] hash = md5.ComputeHash(inputBytes);

 var s = Encoding.Default.GetString(hash);

或选择其他编码格式。

【讨论】:

  • 我试过了,但不幸的是我没有得到预期的结果
猜你喜欢
  • 2016-05-13
  • 1970-01-01
  • 2014-05-28
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多