Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#和PHP中的不同MD5文件哈希_C#_Php_Encoding_Md5_Checksum - Fatal编程技术网

C#和PHP中的不同MD5文件哈希

C#和PHP中的不同MD5文件哈希,c#,php,encoding,md5,checksum,C#,Php,Encoding,Md5,Checksum,我在检查C#和PHP中文件的MD5校验和时遇到了一个小问题。PHP脚本计算的哈希与C#计算的哈希不同 在PHP中,我使用md5_file函数,我的C代码是: 受保护的字符串GetFileMD5(字符串文件名) { FileStream file=newfilestream(文件名,FileMode.Open); MD5 MD5=新的MD5CryptoServiceProvider(); 字节[]retVal=md5.ComputeHash(文件); file.Close(); StringBui

我在检查C#和PHP中文件的MD5校验和时遇到了一个小问题。PHP脚本计算的哈希与C#计算的哈希不同

在PHP中,我使用
md5_file
函数,我的C代码是:

受保护的字符串GetFileMD5(字符串文件名)
{
FileStream file=newfilestream(文件名,FileMode.Open);
MD5 MD5=新的MD5CryptoServiceProvider();
字节[]retVal=md5.ComputeHash(文件);
file.Close();
StringBuilder sb=新的StringBuilder();
for(int i=0;i
你知道如何计算相同的散列吗?我认为这可能与编码有关

提前谢谢

我用这个:

在比较php md5和c#md5时,我还没有遇到任何问题

我的C#生锈了,但威尔:

byte[] retVal = md5.ComputeHash(file);
实际读取整个文件?我认为这只是散列流对象。我认为您需要读取流,然后对整个文件内容进行散列

  int length = (int)file.Length;  // get file length
  buffer = new byte[length];      // create buffer
  int count;                      // actual number of bytes read
  int sum = 0;                    // total number of bytes read

  // read until Read method returns 0 (end of the stream has been reached)
  while ((count = file.Read(buffer, sum, length - sum)) > 0)
      sum += count;  // sum is a buffer offset for next reading
  byte[] retVal = md5.ComputeHash(buffer);

我不确定它是否真的按原样运行,但我认为需要类似的东西。

我敢打赌C使用
Windows-1250
编码,而您的PHP脚本使用
UTF-8
ISO-8859-1
编码。。。试着使两边的编码都相同……它与命令行工具md5sum相比如何?对于FileStream,您不能认为指针在开头。使用file.Seek(0,SeekOrigin.Begin)确保正确。msdn中的所有示例都在FileStream构造函数之后使用它。@Jack
md5sum
返回
d41d8cd98f00b204e9800998ecf8427e
但它有一点werid,因为它与md5相同(“”@shadyyx PHP不使用任何编码,PHP中的字符串与
字节[]
相同,所以PHP不需要解码。是关于md5文件的吗?或者仅仅是文本?不需要将文件解码为文本即可获得MD5。如果它不是UTF-8呢?它给出了与使用ComputeHash相同的结果。
System.Text.UTF8Encoding text = new System.Text.UTF8Encoding();
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();                
Convert2.ToBase16(md5.ComputeHash(text.GetBytes(encPassString + sess)));


class Convert2
{
   public static string ToBase16(byte[] input)
   {
      return string.Concat((from x in input select x.ToString("x2")).ToArray());
   }
}
byte[] retVal = md5.ComputeHash(file);
  int length = (int)file.Length;  // get file length
  buffer = new byte[length];      // create buffer
  int count;                      // actual number of bytes read
  int sum = 0;                    // total number of bytes read

  // read until Read method returns 0 (end of the stream has been reached)
  while ((count = file.Read(buffer, sum, length - sum)) > 0)
      sum += count;  // sum is a buffer offset for next reading
  byte[] retVal = md5.ComputeHash(buffer);