2011-06-19 32 views
2

我有一個winform c#應用程序。它遠程連接到MySQL並匹配存儲在MySQL數據庫表中的密碼。密碼使用SHA1存儲。我存儲了一個默認值:「mypassword」,並使用SHA1對其進行散列處理。在MySQL中匹配文本輸入與哈希值

當應用程序運行時,首先要求用戶輸入密碼。爲此,如果我使用下面提到的函數,那麼當與由MySQL SHA1函數生成的函數進行比較時,它會生成不同的哈希字符串。如何匹配值?請幫忙。

public static string HashCode(string str) 
     { 
      string rethash = ""; 
      try 
      { 

       System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); 
       System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); 
       byte[] combined = encoder.GetBytes(str); 
       hash.ComputeHash(combined); 
       rethash = Convert.ToBase64String(hash.Hash); 
      } 
      catch (Exception ex) 
      { 
       string strerr = "Error in HashCode : " + ex.Message; 
      } 
      return rethash; 
     } 
    } 

我打電話這樣說:

string hashedvalue = HashCode("mypassword"); 
      MessageBox.Show("The hashed value is : " + hashedvalue); 

是散列法兩者有什麼不同?

回答

2

SHA-1從MySQL散列是一個十六進制編碼的串,而C#給出Base64編碼串,所以嘗試這

rethash = BitConverter.ToString(hash.Hash).Replace("-", "");