【问题标题】:How do I send generated encrypted password from C# to MySQL如何将生成的加密密码从 C# 发送到 MySQL
【发布时间】:2018-03-27 20:18:32
【问题描述】:

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

【问题讨论】:

  • 向我们展示您的具体尝试以及问题所在。
  • 从这里开始,这是开始:codeproject.com/Articles/704865/…
  • 我尝试使用 MD5,但后来我在 stackoverflow 的某些部分阅读了 MD5 无法解密...它是单向哈希
  • I tried using MD5 but then I read on some part of the stackoverflow MD5 cannot be decrypted ... it is a one-way hash 你做错了。阅读文章。
  • 我要展示我的代码

标签: c# mysql


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2010-12-02
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多