2016-11-19 40 views
0

我有一個用$ hash = password_hash($ accountpassword,PASSWORD_DEFAULT);登錄的主頁。 (http://php.net/manual/de/function.password-hash.phpmySQl用c登錄#

密碼保存爲哈希以$ 2y $開頭。

然後創建在C#登錄:

//crypting the PW that user enter 
     string cryptedPassword = Crypter.Blowfish.Crypt(textBox_password.Text); 


     string user = textBox_username.Text; 
     string pass = cryptedPassword; 
     if (user == "" || pass == "") 
     { 
      MessageBox.Show("Empty Fields Detected ! Please fill up all the fields"); 
      return; 
     } 
     bool r = validate_login(user, pass); 
     if (r) 
      MessageBox.Show("Correct Login Credentials"); 
     else 
      MessageBox.Show("Incorrect Login Credentials"+cryptedPassword); 

我的驗證方法:

private bool validate_login(string user, string pass) 
    { 
     db_connection(); 
     MySqlCommand cmd = new MySqlCommand(); 
     cmd.CommandText = "Select * from users where [email protected] and [email protected]"; 
     cmd.Parameters.AddWithValue("@user", user); 
     cmd.Parameters.AddWithValue("@pass", pass); 
     cmd.Connection = connect; 
     MySqlDataReader login = cmd.ExecuteReader(); 
     if (login.Read()) 
     { 
      connect.Close(); 
      return true; 
     } 
     else 
     { 
      connect.Close(); 
      return false; 
     } 
    } 

在Crypter.Blowfish.Crypt(textBox_password.Text)是撥錯。我成爲哈希以$ 2a開頭$

任何人都可以幫助解決這個問題嗎?

回答

1

當您使用Crypter.Blowfish.Crypt方法時,您需要指定第二個參數salt,如果沒有提供,那麼爲您生成一個隨機參數。顯然你會在隨後的調用中獲得不同的隨機值。

Alternativly你可以得到Crypter庫要檢查的哈希提供的密碼:

if (Crypter.CheckPassword(password, cryptedPassword)) { 
    // Valid password code here 
    return true; 
} else { 
    // Invalid password code here 
    return false; 
} 

你顯然需要讀取數據庫的加密的密碼,但我相信這個方法將意味着你不」 t需要爲密碼散列提供/存儲salt。

HTH

+0

嗯php腳本不是我的工作。我怎樣才能找出2參數? – Andreas

+0

請使用類似'Crypter.Blowfish.GenerateSalt(6)'的東西,並存儲該值,或使用您自己選擇的字符串(不太安全)。我已經更新了我的答案,以包含一種替代方法,該方法可以使您無需存儲鹽即可執行檢查。 – Graeme