2017-10-16 65 views
-5

如何以我的注册表形式对密码进行加密并将其发送给MySQL,然后我将转到我的登录表单并输入用户名和密码。 告诉我一些例子如何将生成的加密密码从C#发送到MySQL

string input_fname = textBox1.Text; 
     string input_mname = textBox2.Text; 
     string input_lname = textBox3.Text; 
     string input_address = textBox6.Text; 
     string input_age = textBox5.Text; 
     string input_password = getMD5(textBox4.Text); 

     // establishing connection 
     MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); 
     builder.Server = "127.0.0.1"; 
     builder.UserID = "root"; 
     builder.Password = "seven7"; 
     builder.Database = "justsing"; 
     MySqlConnection connection = new MySqlConnection(builder.ToString()); 
     connection.Open(); 

     // sql command 
     string newuser_sql = "INSERT INTO `justsing`.`karaokeadmin` (`FirstName`, `MiddleName`, `LastName`, `Address`, `Age`, `password`) VALUES (@FirstName,@MiddleName,@LastName,@Address,@Age,@password)"; 
     MySqlCommand newuser = new MySqlCommand(newuser_sql, connection); 
     newuser.CommandText = newuser_sql; 
     newuser.Parameters.AddWithValue("@FirstName", input_fname); 
     newuser.Parameters.AddWithValue("@MiddleName", input_mname); 
     newuser.Parameters.AddWithValue("@LastName", input_lname); 
     newuser.Parameters.AddWithValue("@Address", input_address); 
     newuser.Parameters.AddWithValue("@Age", input_age); 
     newuser.Parameters.AddWithValue("@password", input_password); 
     newuser.ExecuteNonQuery(); 
     MessageBox.Show("Inserted Succesfully"); 
    } 
    public string getMD5(string text) 
    { 
     MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 
     md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text)); 
     byte[] result = md5.Hash; 
     StringBuilder str = new StringBuilder(); 
     for (int i = 0; i < result.Length; i++) 
     { 
      str.Append(result[i].ToString("X2")); 
     } 
     return str.ToString(); 
    } 
} 

这里是验证的登录密码,如果用户名和密码相匹配

private void button5_Click(object sender, EventArgs e) 
    { 
     string lname = textBox7.Text; 
     string pass = textBox8.Text; 
     if (lname == "" || pass == "") 
     { 
      MessageBox.Show("Empty Fields Detected ! Please fill up all the fields"); 
     } 
     bool r = validate_login(lname, pass); 
     if (r) 
     { 
      JustSingAdminControlPanel js = new JustSingAdminControlPanel(); 
      js.Show(); 
     } 
     //code kung ano gustong buksan 
     else 
     { 
      MessageBox.Show("Wrong Input"); 
     } 
    } 
+0

告诉我们你正在尝试的是什么以及你有什么问题。 – spodger

+0

从这里开始,这是开始:https://www.codeproject.com/Articles/704865/Salted-Password-Hashing-Doing-it-Right – Stefan

+0

我试过使用MD5,但后来我读了一些部分的stackoverflow MD5不能被解密......这是一个单向散列 –

回答

0

做到这一点,标准的方法是从形式发送到您的API中cleartext(但使用HTTPS,这是必不可少的),然后将其散列在后端(单独问题如何执行此操作),最后将该散列保存在MYSQL数据库中。你不想加密密码,你想散列它们。

+0

您可以在这里阅读有关md5 clss的信息https://msdn.microsoft.com/library/system.security.cryptography.md5(v=vs.110).aspx – Gianlucca

+2

@Gianlucca请勿使用MD5 - https:// security .stackexchange.com/questions/19906/is-md5-considered-insecure – Smartis

+1

@smartis我不知道这些漏洞。谢谢! – Gianlucca