如何使用Winform对密码进行加密? [英] How do I make my Password Encrypted using Winform?
本文介绍了如何使用Winform对密码进行加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已完成以下操作,现在我希望使用诸如MD5之类的任何加密模型来保护我的密码.请帮助我使用加密过程.我只需要了解加密.
I have done following things ,Now I want my password to be secured with any of Encryption Model like MD5. Please help me in Using Encryption Process.I just need to know about encryption .
private void button1_Click(object sender, EventArgs e)
{
string connection = "Server = JV66-INTERN\\SQLEXPRESS; Database = testdb; Trusted_Connection = True";
SqlConnection connect = new SqlConnection(connection);
connect.Open();
//MessageBox.Show(connect.State.ToString());
SqlCommand cmd = new SqlCommand("SELECT username,password FROM Users WHERE username=''" + textBox1.Text + "'' and password=''" + textBox2.Text + "''", connect);
string result;
result = (string)cmd.ExecuteScalar();
// MessageBox.Show(result);
if (result == textBox1.Text)
{
LoggedInForm newf = new LoggedInForm(textBox1.Text.ToString());
newf.ShowDialog();
}
else
{
MessageBox.Show("The Given Account is Not Present in Database");
}
[edit]已添加代码块-OriginalGriff [/edit]
[edit]Code block added - OriginalGriff[/edit]
推荐答案
MD5不是加密算法-它是哈希算法.区别在于,哈希是不可逆的,并且不需要加密密钥-对于密码,这是必须采用的方法.但是,正式将MD5归类为损坏的-您应该改用SHA.
这里有一个提示/技巧,可以帮助您:密码存储:操作方法. [^ ]
MD5 is not an encryption algorithm - it is a hashing algorithm. The difference is that hashing is not reversible, and does not require an encryption key - for passwords it is the way to go. However, MD5 is officialy classed as broken - you should use SHA instead.
There is a Tip / Trick here that will help: Password Storage: How to do it.[^]
另外,请不要对数据库进行那样的访问:这是蓄意或偶然的SQL注入攻击的邀请,这可能会破坏数据库.改用参数化查询:
On a separate note, do not do your database access like that: it is an invitation to a deliberate or accidental SQL Injection attack which could destroy your database. Use Parametrized queries instead:
SqlCommand cmd = new SqlCommand("SELECT username,password FROM Users WHERE username=@UN and password=@PW", connect);
cmd.Parameters.AddWithValue("@UN", textBox1.Text);
cmd.Parameters.AddWithValue("@PW", textBox2.Text);
string result;
result = (string)cmd.ExecuteScalar();
根据您的需要,您可能需要选择自己的加密算法.您可能需要阅读不同类型的加密算法.对于一个简单的示例,您可能需要尝试以下链接.
使用C#进行简单加密 [使用C#进行简单解密 [
Depending on your needs, you may want to choose your own encryption algorithm. You may want to read about the different types of encryption algorithms. For a simple one, you may want to try out the following links.
Simple Encryption in C#[^]
Simple Decryption in C#[^]
这篇关于如何使用Winform对密码进行加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文