C#里面如何對數(shù)據(jù)庫里面的密碼字段加密?加密以后如何在在程序里面取值?
都是使用MD5加密,推薦一篇文檔給你。有很詳細的加密和解密方法,還有就是C#中有自帶的加密和解密方法。隨便你使用哪種。
為什么要解密呢?MD5是可以窮舉破解,但是應用中不需要解密,要不然就不安全了。如果你要查找當前用戶輸入的密碼是否正確,你加密一下去數(shù)據(jù)庫里查詢就可以了?
密碼用md5加密保存到數(shù)據(jù)庫,然后用戶登錄時你把他的密碼在MD5加密一次跟數(shù)據(jù)庫里面的比較就行了。
方法:
密碼子段類型為binary(50)。應用System Security.Cryptography名稱空間下的SHA1類的ComputeHash()方法將字符密碼進行哈希散列運算轉換為byte[]類型對象,保存入數(shù)據(jù)庫。
//哈系散列轉換
publicbyte[] getSaltedPassword(string password)
{
SHA1 sha1=SHA1.Create();
//應用System.Text空間下的Unicode.GetBytes方法獲得byte.
byte[] bytePassword=sha1.ComputeHash(Encoding.Unicode.GetBytes(password));
return bytePassword;
}
//數(shù)據(jù)存入,直接將byte[]保存入binary字段
publicint AccountRegister(string accountName,string password,string email)
{
byte[] bytePassword =this.getSaltedPassword(password);
SqlConnection myConnection =new SqlConnection(this.GetConnStr);
myConnection.Open();
SqlCommand myCommand =new SqlCommand("Account_Add",myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
SqlParameter prmAccountName=myCommand.Parameters.Add(new SqlParameter("@AccountName",SqlDbType.VarChar,50));
prmAccountName.Value=accountName;
SqlParameter prmPassword=myCommand.Parameters.Add(new SqlParameter("@password",SqlDbType.Binary,50));
prmPassword.Value=bytePassword;
SqlParameter prmEmail=myCommand.Parameters.Add(new SqlParameter("@email",SqlDbType.VarChar,50));
prmEmail.Value=email;
int myInt=myCommand.ExecuteNonQuery();
myCommand.Dispose();
myConnection.Close();
return myInt;
}
//密碼比較。將字符密碼轉換為哈西散列后直接與數(shù)據(jù)庫binary密碼字段比較
publicint AccountVerify(string accountName,string password)
{
byte[] bytePassword =this.getSaltedPassword(password);
SqlConnection myConnection =new SqlConnection(this.GetConnStr);
myConnection.Open();
SqlCommand myCommand =new SqlCommand("Account_Check",myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
SqlParameter prmAccountName=myCommand.Parameters.Add(new SqlParameter("@AccountName",SqlDbType.VarChar,50));
prmAccountName.Value=accountName;
SqlParameter prmPassword=myCommand.Parameters.Add(new SqlParameter("@password",SqlDbType.Binary,50));
prmPassword.Value=bytePassword;
SqlParameter prmReturnValue=myCommand.Parameters.Add(new SqlParameter("@Return_Value",SqlDbType.Int,4));
prmReturnValue.Direction=ParameterDirection.ReturnValue;
myCommand.ExecuteNonQuery();
int accountID=(int)prmReturnValue.Value;
myCommand.Dispose();
myConnection.Close();
return accountID;
}
//相關Store procedure
//登陸驗證
CREATE PROCEDURE Account_Check @AccountName varchar(50),@Password binary(50)
AS
Declare @AccountId int;
Select @AccountId= AccountID From Accounts
Where accountName=@accountname;
If isnull(@AccountID,0)=0
Select @AccountId=-1; --//賬號錯誤!
Else
Begin
Select @accountID=null
Select @AccountId= AccountID From Accounts
Where accountName=@accountname and password=@password;
If isnull(@AccountID,0)=0
Select @AccountID=0; --//密碼錯誤!
End
Return @AccountID;
//用戶增加
CREATE PROCEDURE Account_Add @accountName varchar(50),@password binary (50),@email varchar(50)
AS
insert into Accounts(accountName,password,email)
values(@accountName,@password,@email);
return @@Error;
FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox2.Text, "md5");//使用MD5加密
不解密就MD5,sha-1這類hash加密,解密的話,自己寫加密算法。。。