C#里面对数据库里面的密码字段加密1

C#里面如何对数据库里面的密码字段加密?加密以后如何在在程序里面取值?

都是使用MD5加密,推荐一篇文档给你。有很详细的加密和解密方法,还有就是C#中有自带的加密和解密方法。随便你使用哪种。

为什么要解密呢?MD5是可以穷举破解,但是应用中不需要解密,要不然就不安全了。如果你要查找当前用户输入的密码是否正确,你加密一下去数据库里查询就可以了?

密码用md5加密保存到数据库,然后用户登录时你把他的密码在MD5加密一次跟数据库里面的比较就行了。

方法:

密码子段类型为binary(50)。应用System Security.Cryptography名称空间下的SHA1类的ComputeHash()方法将字符密码进行哈希散列运算转换为byte[]类型对象,保存入数据库。
//哈系散列转换
publicbyte[] getSaltedPassword(string password)
        {
            SHA1 sha1=SHA1.Create();
//应用System.Text空间下的Unicode.GetBytes方法获得byte.          byte[]bytePassword=sha1.ComputeHash(Encoding.Unicode.GetBytes(password));
            return bytePassword;
        }
//数据存入,直接将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;
        }
//密码比较。将字符密码转换为哈西散列后直接与数据库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加密,解密的话,自己写加密算法。。。

 

posted @ 2013-06-04 09:03  张国朋  阅读(1886)  评论(0)    收藏  举报