• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

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

时间:2015年04月02日 15:40:33    来源:魔法猪系统重装大师官网    人气:13763

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加密,解密的话,自己写加密算法。。。

里面,如何,对,数据,库里,面的,密码,字段,加密,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部