输入用户名和密码后,为了安全期间想在数据库中密码栏里只能看到很多没有规则的数据。我应该怎么做?
存入数据库前先加密啊,找个加密类
可以建立或找一种加密组件,然后每次建立新用户时将加密过得乱码放到库中.
有这样的组件.我们用到过.
先将密码加密,存入数据库,登陆时再将其解密.
可以利用 md5加密啊 登陆输入密码的时候也加密刚输入的密码 再把数据库中以前用md5加密的字段值取出来 比较就可以了
public string MD5(String str)
{
MD5 md5=new MD5CryptoServiceProvider();
byte[] data=System.Text.Encoding.Default.GetBytes(str);
byte[] result=md5.ComputeHash(data);
String ret="";
for(int i=0;i<result.Length;i++)
ret+=result[i].ToString("x").PadLeft(2,0);
return ret;
}
网上多的是,呵呵
刚好前面老师分了任务给我就是加密解密的,自己慢慢看吧 这里是 md5加密的 先是通过 textbox把密码加密 写到数据库,然后登陆的时候 通过加密输入的密码,然后与数据库中已经加密的密码做比较。
private void button10_Click(object sender, System.EventArgs e)
{
string reGex,reGex2;
byte[] bt=UTF8Encoding.UTF8.GetBytes(textPass1.Text.Trim());
MD5CryptoServiceProvider objMD5;
objMD5=new MD5CryptoServiceProvider();
byte[] output=objMD5.ComputeHash (bt);
reGex=BitConverter.ToString (output);
reGex2=reGex.Replace("-","");
if (textName1.Text=="" || textPass1.Text =="")
{
MessageBox.Show("您提交的信息不完全","提示!");
return;
}
try
{
string strCon = " Provider=Microsoft.Jet.OLEDB.4.0; Data Source=usermd5.mdb ";
OleDbConnection myConn=new OleDbConnection(strCon);
string strInsert = "INSERT INTO cc_admin(usernam,passwor) VALUES ("
+ textName1.Text +","
+ reGex2 +")";
myConn.Open();
OleDbCommand myCmd = new OleDbCommand(strInsert,myConn);
myCmd.ExecuteNonQuery();
myConn.Close();
MessageBox.Show("添加用户成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void button28_Click(object sender, System.EventArgs e)
{
string reGex,reGex2;
byte[] bt=UTF8Encoding.UTF8.GetBytes(textPass.Text.Trim());
MD5CryptoServiceProvider objMD5;
objMD5=new MD5CryptoServiceProvider();
byte[] output=objMD5.ComputeHash (bt);
reGex=BitConverter.ToString (output);
reGex2=reGex.Replace("-","");
if (textName.Text=="" || textPass.Text =="")
{
MessageBox.Show("您提交的信息不完全","提示!");
return;
}
string strCon = " Provider=Microsoft.Jet.OLEDB.4.0; Data Source=usermd5.mdb ";
OleDbConnection myConn=new OleDbConnection(strCon);
string strSel = "select count(*) from cc_admin where usernam="+textName.Text+" and passwor="+reGex2+"";
myConn.Open();
OleDbCommand myCmd = new OleDbCommand(strSel,myConn);
int Count=(int)myCmd.ExecuteScalar();
myConn.Close();
if(Count==0)
{
MessageBox.Show("没有这个用户!请核对您是否输入正确!","提示!");
textName.Text="";
textPass.Text="";
return;
}
try
{
MessageBox.Show("通过验证!","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
textName.Text="";
textPass.Text="";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}