PDA

View Full Version : سوال: hash security



amin_mehranfar2000
شنبه 21 شهریور 1388, 19:37 عصر
سلام دوستان من یک برنامه داشتم که برای ان user و password گذاشتم و من password hash مکنم وداخل data base سیو مکنم
وقتی passworde من حروف باش مشکلی ندارد و برنامه باز می شود وقتی password عدد باشد به مشکل بر می خورم و نمی توانم برنامه رو باز کنم؟
می شود کمکم کنید؟

raravaice
شنبه 21 شهریور 1388, 19:47 عصر
با چه الگویی عمل hash را انجام میدید؟

amin_mehranfar2000
شنبه 21 شهریور 1388, 20:18 عصر
private string encryptString(string strToEncrypt)// هش كردن كلمه عبور
{
UTF8Encoding ue = new UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Bytes to string
return System.Text.RegularExpressions.Regex.Replace
(BitConverter.ToString(hashBytes), "-", "").ToLower();
}

amin_mehranfar2000
یک شنبه 22 شهریور 1388, 10:17 صبح
lotfan komak konid

amin_mehranfar2000
یک شنبه 22 شهریور 1388, 14:46 عصر
کسی بلد نیست؟

debugger
یک شنبه 22 شهریور 1388, 14:56 عصر
الگوريتم هش مشكلي نداره

احتمال قوي جايي كه if گذاشتي و پسورد ها را مقايسه مي كنيد كدي را اشتباه نوشتي

ايا اعداد را به صورت string مي فرستي براي مقايسه يا int

در كل بايد كدت را بزاري تا بيشتر بشه روش مانور داد

amin_mehranfar2000
یک شنبه 22 شهریور 1388, 15:11 عصر
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text == "")
{
MessageBox.Show("نام خانوادگی نمی تواند مقدار تهی داشته باشد ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading);
textBox3.Focus();
}
else if (textBox4.Text == "")
{
MessageBox.Show("نام نمی تواند مقدار تهی داشته باشد ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading);
textBox4.Focus();
}
else if (textBox5.Text == "")
{
MessageBox.Show("نام کاربری نمی تواند مقدار تهی داشته باشد ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading);
textBox5.Focus();
}
else if (textBox6.Text == "") { MessageBox.Show("پسورد نمی تواند مقدار تهی داشته باشد ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading); textBox6.Focus(); }
else if (textBox7.Text != textBox6.Text) { MessageBox.Show("تکرار پسورد با خودپسورد برابر نمی باشد ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading); textBox7.Focus(); }
else if (check() == 0) { MessageBox.Show("این نام کاربری تکراری است ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading); }
else
{
con.ConnectionString = "server= A-5F7BEEE5ECB04;initial catalog=bil;integrated security=true";
com.CommandText = "insert into login (fname,llname,username,password) VALUES (@fname,@lname,@username,@password)";
com.Parameters.AddWithValue("@fname", textBox3.Text);
com.Parameters.AddWithValue("@lname", textBox4.Text);
com.Parameters.AddWithValue("@username", textBox5.Text);
com.Parameters.AddWithValue("@password", encryptString(textBox5.Text));
con.Open();
int ex = com.ExecuteNonQuery();
con.Close();
if (ex == 0) { MessageBox.Show("مشکلی در ثبت اطلاعات به وجود اماده برنامه رو دوباره اجرا کنید ", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading); }
else { MessageBox.Show("اطلاعات با موفقیت ثبت گردید", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading); }
textBox3.Text = "";
textBox5.Text = "";
textBox4.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
}

private string encryptString(string strToEncrypt)// هش كردن كلمه عبور
{
UTF8Encoding ue = new UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Bytes to string
return System.Text.RegularExpressions.Regex.Replace
(BitConverter.ToString(hashBytes), "-", "").ToLower();
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private int check()
{
com.CommandText = "select count(*) from login where username='" + textBox5.Text + "'";
com.Connection = con;
con.Open();
int ch = (int)com.ExecuteScalar();
con.Close();
if (ch == 0) return 1;
else return 0;
}
private void button1_Click(object sender, EventArgs e)
{
con.ConnectionString = "server= A-5F7BEEE5ECB04;initial catalog=bil;integrated security=true";
com.CommandText = " select count(*) from login where username='" + textBox1.Text + "' and password='" + encryptString(textBox2.Text) + "' ";
com.Connection = con;
con.Open(); ;
int test = (int)com.ExecuteScalar();
con.Close();
if (test == 1) { groupBox3.Enabled = true;
groupBox2.Enabled = true;
}
else
{
; MessageBox.Show("نام کاربی یا پسورد شما اشتباه می باشد", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading); textBox1.Focus(); groupBox2.Enabled = false;
groupBox3.Enabled = false;
}
textBox1.Text = ""; textBox1.Text = "";
textBox2.Text = "";
textBox2.Text = "";
}
}
}

debugger
یک شنبه 22 شهریور 1388, 15:34 عصر
نوع فيلدي كه پسورد در ان ذخيره مي شود را از نوع text يا nvarchar بگير

amin_mehranfar2000
یک شنبه 22 شهریور 1388, 15:39 عصر
char(40) هست

debugger
یک شنبه 22 شهریور 1388, 16:22 عصر
char(40) هست
نوع فيلدي كه پسورد در ان ذخيره مي شود را از نوع text يا nvarchar بگير