2016-05-25 2 views
3

私はこのC#のコードがあります:あなたはonline versionの.asを見ることができます。ここC#コードPHP同等のmd5ハッシュ

public static string Encript(string strData) 
{ 
    string hashValue = HashData(strData); 
    string hexHashData = ConvertStringToHex(hashValue); 
    return hexHashData; 
} 

public static string HashData(string textToBeEncripted) 
{ 
    //Convert the string to a byte array 
    Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted); 

    //Compute the MD5 hash algorithm 
    Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash); 

    return System.Text.Encoding.Unicode.GetString(byteHashValue); 
} 


public static string ConvertStringToHex(string asciiString) 
{ 
    string hex = ""; 
    foreach (char c in asciiString) 
    { 
     int tmp = c; 
     hex += String.Format("{0:x2}", (uint) System.Convert.ToUInt32(tmp.ToString())); 
    } 
    return hex; 
} 

を、あなたは文字列「テスト」私は出力「5c82e9e41c7599f790ef1d774b7e6bf」 を取得し、これがあるために見ることができます私はPHP側

$a = "test"; 
$a = mb_convert_encoding($a, "UTF-16LE"); 
$a = md5($a); 
echo $a; 

に試みたが、PHPコードの値が何であるかを「c8059e2ec7419f590e79d7f1b774bfe6」.Whyが動作していませんか?

編集:C#のコードが間違っていると私はエンコーディングを変換することはあなたに別の答えを与えているものであると信じてい

+2

「5c82e9e41c7599f790ef1d774b7e6bfは、」ここにしようとhttp://md5.gromweb.com/ –

+0

098f6bcd4621d373cade4e832627b4f6 MD5ではないエンコーディングがらくたを失い、「テスト」の正しいMD5ハッシュです。 – Viezevingertjes

+0

あなたは本当に 'System.Text.Encoding.Unicode.GetString(byteHashValue)'を使ってはいけません。ハッシュ値は、有効なUTFでないシーケンスを含むバイナリデータです。文字列として返す必要がある場合は、Base64エンコーディングまたは16進数のエンコーディングをバイト配列から直接取得する必要があります。 –

答えて

1

を交換する必要があるように見える、

$a = mb_convert_encoding($a, "UTF-16LE"); 
+0

私はすでに質問をする前に値を "098f6bcd4621d373cade4e832627b4f6"としていますが、C#の値と一致しません。 –

3

正しいMD5ハッシュせずにそれを試してみてくださいPHPの 'test'は '098f6bcd4621d373cade4e832627b4f6'です。

は私が

public static string CreateMD5(string input) 
    { 
     // Use input string to calculate MD5 hash 
     using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) 
     { 
      byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); 
      byte[] hashBytes = md5.ComputeHash(inputBytes); 

      // Convert the byte array to hexadecimal string 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < hashBytes.Length; i++) 
      { 
       sb.Append(hashBytes[i].ToString("X2")); 
      } 
      return sb.ToString(); 
     } 
    } 

それはPHPと同じハッシュを生成しますが、「エンコーディングを変換する」メソッドずに使用しているだろうC#で、このコードでそれをテストしました。

+0

ASCIIエンコーディングを使用する場合は、これが正しい値です。しかし、OPはUnicodeエンコーディングを使用しています。これは異なるinputBytes値を与えます。 –

+0

これは彼が求めているのではなく、C#コードが間違った結果を出したためにPHPコードを修正しようとしていました。 Atleast、それは私が彼のコメントから作るものです。 – Viezevingertjes

+1

PHPプログラムをC#にマッチさせようとしているのか、C#をPHPにマッチさせようとしているのかは少し不明です。 –

0

問題は、結果がC#コードで間違って変換されていることです。 ComputeHashを呼び出した後にコードにブレークポイントを設定し、byteHashValueの値を調べると、そのコードはc8059e2e...であることがわかります。

または、ちょうどあなたのComputeHashメソッドにこのコードを追加:私はなるようにコードを書き換えることをお勧め

Console.WriteLine(BitConverter.ToString(byteHashValue)); 

public static string Encript(string strData) 
    { 
     string hashValue = HashData(strData); 
     return hashValue; 
    } 

    public static string HashData(string textToBeEncripted) 
    { 
     //Convert the string to a byte array 
     Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted); 

     //Compute the MD5 hash algorithm 
     Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash); 

     return BitConverter.ToString(byteHashValue).Replace("-", ""); 
    } 

ああ、サイドノート:単語が「暗号化され、 "not" Encript "

+0

Downvoter:downvoteの理由を指定するのが通例です。 –

0

あなたの場合の問題はエンコーディングではなく、C#側の文字列への変換です。どこでも同じエンコーディングを使用する限り、期待どおりに動作するはずです。ただし、ほとんどのオンラインハッシャはASCIIエンコーディングを使用しますが、System.Text.Encoding.UnicodeはUTF-16を使用するため、結果はオンラインエンコーダとは異なります。

以下のコードは、あなたのPHPスニペットと同じ結果が得られます(つまり、c8059e2ec7419f590e79d7f1b774bfe6

public static void Main(string[] args) 
{ 
    string a = "test"; 
    string en = HashData(a); 
    Console.WriteLine(en); 
} 

public static string HashData(string textToBeEncripted) 
{ 
    Byte[] byteDataToHash = System.Text.Encoding.Unicode.GetBytes(textToBeEncripted); 
    Byte[] byteHashValue = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(byteDataToHash); 
    System.Text.StringBuilder s = new System.Text.StringBuilder(); 
    foreach (var b in byteHashValue) 
     s.Append(b.ToString("x2")); 
    return s.ToString(); 

}

あなたの代わりにSystem.Text.Encoding.ASCIIを使用している場合は他の回答で提案されているように、あなたは098f6bcd4621d373cade4e832627b4f6を取得します。しかし、PHPコードでもASCIIエンコーディングを使用する必要があります。

これは、UTF16ではすべての文字が1バイトだけでなく2バイトで表されるためです。したがって、byteDataToHashのサイズは2倍になります([116, 0, 101, 0, 115, 0, 116, 0]とあなたの場合は[116, 101, 115, 116])。もちろん、別のバイトベクトルは異なるハッシュ値につながります。しかし、上記のように、すべてのコンポーネントが同じエンコーディングを使用している限り、どちらを使用するかは重要ではありません。