在ADO.NET中,向數據庫添加數據時,怎樣對數據中的密碼進行加密?(也就是說在數據表中也看不到用戶的密
碼,只是一些經過編譯后的字符串,以防止數據庫管理員利用用戶的密碼進行非法操作。)
首先,在c#WinForm程序中引入命名空間,"using System.Web.Security;",此命名空間是專門用來對程序進
行安全設置的;
其次,定義一個string類型的變量,用來接收用輸入的密碼;
string passWord = this.textBox1.Text.Trim();
取到密碼之后,接下來便是對密碼進行加密處理:
string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5");
最后,將加密后的密碼pwd添加到數據庫中去。
insert into userInfo(uName,pwd) values('{0}','{1}');select @@identity", this.txtUID.Text.Trim
(),passwrod);
示例代碼:
using System.Web.Security;
//取得文本框中的密碼
string pwd = this.txtPwd1.Text.Trim();
//對密碼加密
string passwrod = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5");
//創建SQL語句,將加密后的密碼保存到數據庫中
string insCmd =
string.Format("insert into userInfo(uName,pwd) values('{0}','{1}');select @@identity",
this.txtUID.Text.Trim(),passwrod);
using (SqlCommand cmd = new SqlCommand(insCmd, Form1.Connection))
{
int uid = Convert.ToInt32(cmd.ExecuteScalar());
//int uid = int.Parse(cmd.ExecuteScalar());//error
if (uid > 0)
{
string mess = string.Format("恭喜,注冊成功!您的號碼是{0}",uid);
MessageBox.Show(mess);
}
else
{
MessageBox.Show("對不起,注冊失敗了!");
}
}
這樣加密之后保證了用戶密碼的安全,但是又出現了一個問題,即用戶登錄時怎樣對密碼進行驗證,該不會讓
用戶去記住加密后的那一長串字符串吧? 答案當然是否定的,那怎樣解決呢?
應該這樣解決:
在用戶登錄時,得到用戶輸入的密碼;
然后,將取到的密碼再次進行加密;
之后,根據用戶名取出該用戶在數據庫中的真實密碼;
最后,將剛剛進行加密的密碼與數據庫密碼進行比對,即可完成用戶登錄操作。
示例代碼:
string pwd = this.txtPwd1.Text.Trim();
string pwd1 = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5");
string uid = this.txtUID.Text.Trim();
string selCmd = string.Format("select pwd from userINfo where uName='{0}'", uid);
string password = "";
using (SqlCommand cmd = new SqlCommand(selCmd, Form1.Connection))
{
password= cmd.ExecuteScalar().ToString();
}
if (password == pwd1)
{
MessageBox.Show("登錄成功");
}
else
{
MessageBox.Show("密碼錯誤!");
}
完整實例(復制即可用):
1.數據庫代碼:
use tempdb
go
if exists (select * from sysobjects where name = 'UserInfo')
drop table UserInfo
go
create table UserInfo
(
uId int identity(1,1) not null,
uName nvarchar(20) not null,
uAge int not null,
password nvarchar(200) not null
)
go
alter table UserInfo
add constraint PK_uID primary key (uId)
alter table UserInfo
add constraint CK_uAge check (uAge between 0 and 100)
go
select * from UserInfo
2.c#代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web.Security; //安全加密
namespace 密碼加密示例
{
public partial class Form1 : Form
{
//創建數據庫連接字符串
static readonly string strConn = "Data Source=.;Initial Catalog=tempdb;Integrated Security=True";
//創建數據庫連接對象
static SqlConnection connection = null;
//屬性
public static SqlConnection Connection
{
get
{
if (connection == null || connection.State != ConnectionState.Open)
{
connection = new SqlConnection(strConn); //連接數據庫
connection.Open(); //打開數據庫
}
return Form1.connection; //返回一個連接
}
}
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 檢查用戶輸入
/// </summary>
/// <returns></returns>
private bool CheckInput()
{
if (string.IsNullOrEmpty(this.txtName.Text))
{
this.errorPro.SetError(this.txtName, "用戶名不能為空!");
this.txtName.Focus();
return false;
}
else
{
this.errorPro.Dispose(); //終止提示錯誤
}
if (string.IsNullOrEmpty(this.txtAge.Text))
{
this.errorPro.SetError(this.txtAge, "姓名不能為空!");
this.txtAge.Focus();
return false;
}
else
{
this.errorPro.Dispose();
}
if (string.IsNullOrEmpty(this.txtPass.Text))
{
this.errorPro.SetError(this.txtPass, "密碼不能為空!");
}
else
{
this.errorPro.Dispose();
}
return true;
}
/// <summary>
/// 添加數據
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
if (this.CheckInput())
{
//獲取用戶輸入的密碼
string password = this.txtPass.Text.Trim();
//對密碼進行加密
string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");
//創建SQL語句,將加密后的密碼保存到數據庫
string insCmd = string.Format("insert into UserInfo values ('{0}','{1}','{2}')",
this.txtName.Text.Trim(), this.txtAge.Text.Trim(),pwd);
using (SqlCommand cmd = new SqlCommand(insCmd,Form1.Connection))
{
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("恭喜您,注冊成功!");
}
else
{
MessageBox.Show("對不起,注冊失敗···");
}
}
}
}
}
}
完!