2016-12-16 10 views
0

「緑色の角」にこれに手を差し伸べるのに十分親切な人はいますか?現在、このプロジェクトに取り組んでいます。今ここでの考え方は、データベースからパスワードを取得し、ユーザーが入力したものと照合することです。保存されたパスワードはBcryptを使用して暗号化されます。私は今、いろいろな方法で試してみましたが、まだパスワードを一致させることができません。保存されたパスワード(Bcrypt)がどのユーザーの入力に一致することができません

コードの一部です。

// String command used from SQL, match AccountNo with Accountbox, Password with Passwordbox, from the Accounts dbo. 
string a = string.Format("Select * from Accounts where AccountNo='{0}' and Password='{1}'", Accountbox ,Passwordbox); 

SqlCommand ACCcheck = new SqlCommand(a, conn); 
conn.Open(); 

SqlDataAdapter adapt1 = new SqlDataAdapter(ACCcheck); 
DataSet data1 = new DataSet();    
adapt1.Fill(data1); 

SqlDataReader read = ACCcheck.ExecuteReader(); 

try 
{ 
    if (read.Read()) 
    { 
     string salt = BCryptHelper.GenerateSalt(10); 
     string hash = BCryptHelper.HashPassword(Passwordbox, salt); 
     string OGpassword = read["Password"].ToString(); 
     string givenPass = BCryptHelper.HashPassword(hash, Passwordbox); 

     if (givenPass.Equals(OGpassword)) 
     { 
      //if (read.HasRows) // if input data valid, then proceed. if not then close conn and force retry. 
      //{ 
      MessageBox.Show("WORDKING!"); 
      conn.Close(); 
      read.Close(); 

      string q = string.Format("Select * from Transactions where AccountNo =" + Accountbox); // Fetch data from transaction table 

      SqlCommand cmd = new SqlCommand(q, conn); // SQL query, checks if what the user has written is a match with whats on the Db. Accountbox and Passwordbox are Inputbox I've used for this program. 

ここにSQLエラーがあるのか​​、それとも壊れているBcryptの部分なのか分かりません。

援助に感謝します。

答えて

1

thatsのない私はhashを取得するために同じ文字列を使用する場合は

string str = "asdf"; 
     string pass = BCrypt.Net.BCrypt.HashPassword(str, 10); 
     string pass2 = BCrypt.Net.BCrypt.HashPassword(str, 10); 

     bool a = pass == pass2; 

常にfalseとなりますstringが同じで異なるhash場合でも、それが返すなどをbcryptを使用する場合は、データベースからパスワードと一致する方法bcrypt理由代わりに、あなたは、独自のメソッドを使用する必要があり、パスワードを確認するためにそのように動作しません。今Verify

BCrypt.Net.BCrypt.Verify(str, pass); 

それはここ01 trueを返します。はtxtboxから取得するパスワード文字列で、passはデータベースに格納されているhashed passwordです。

0

初めてするためのパスワードを暗号化するとき、あなたはあなたのテーブルで暗号化されたパスワードと一緒にを保存する必要があります。 パスワードを解読するときは、暗号化に使用したのと同じ塩を使用する必要があります。あなたがあなたのテーブルに塩を保存する場合

、そしてあなたも、あなたのselect文からとパスワード=「{1}」条件を削除し、ユーザー・アカウント・レコードを取得した後、パスワードを確認する必要があります。

関連する問題