如何将生成的加密密码从C#发送到MySQL

时间:2017-10-16 10:27:49

标签: c# mysql

如何在我的注册表中加密密码并将其发送到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");
        }
    }

1 个答案:

答案 0 :(得分:0)

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