2015-11-24 19 views
1

你好,我想這部分代碼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(? 

類似但不完全是

upd:與BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); 我得到了:1f3870 be274f6c49b3e31a0c6728957f

,我沒有任何問題,當我使用MD5( 「蘋果」,假)

+2

這有幫助嗎? http://stackoverflow.com/questions/5821677/md5-hashing-does-not-match-in-c-sharp-and-php –

+2

你正在打印原始的二進制垃圾,其中一些字符被誤解,由您的輸出環境解釋。例如一些字節序列LOOK就像是多字節字符。你的md5非常好,這只是一個輸出/顯示問題。 –

+0

副節點:'MD5CryptoServiceProvider'是'IDisposable',因此應該*丟棄* –

回答

0

試試這個:

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

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

或選擇其他編碼格式。

+0

我嘗試了它,但不幸的是我沒有得到預期的結果 –