关于SQL 数据表中的密码加密

时间:2023-03-09 00:53:56
关于SQL 数据表中的密码加密

首先,都知道一个字节(byte)等于八位二进制数。在数据表中将密码字段设置为binary类型,再结合哈希散列运算可以实现密码加密。

下面介绍下binary 和 varbinary:

binary 和 varbinary
固定长度 (binary) 的或可变长度 (varbinary) 的 binary 数据类型。

binary [ ( n ) ]

固定长度的 n 个字节二进制数据。N 必须从 1 到 8,000。存储空间大小为 n+4 字节。

varbinary [ ( n ) ]

n 个字节变长二进制数据。n 必须从 1 到 8,000。存储空间大小为实际输入数据长度 +4 个字节,而不是 n 个字节。输入的数据长度可能为 0 字节。在 SQL-92 中 varbinary 的同义词为 binary varying。

注释
如果在数据定义或变量声明语句中没有指定 n,默认长度为 1。如果没有用 CAST 函数指定 n,默认长度为 30。

当列数据项大小一致时应使用 binary。

当列数据项大小不一致时应使用 varbinary。

接下来说明实现方法:

密码子段类型为binary(50)。应用System Security.Cryptography名称空间下的SHA1类的ComputeHash()方法将字符密码进行哈希散列运算转换为byte[]类型对象,保存入数据库。

实例代码:

 //哈系散列转换
public byte[] getSaltedPassword(string password)
{
SHA1 sha1=SHA1.Create();
      //应用System.Text空间下的Unicode.GetBytes方法获得byte.
byte[] bytePassword=sha1.ComputeHash(Encoding.Unicode.GetBytes(password));
return bytePassword;
}
 //数据存入,直接将byte[]保存入binary字段
public int 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,));
prmAccountName.Value=accountName;
SqlParameter prmPassword=myCommand.Parameters.Add(new SqlParameter("@password",SqlDbType.Binary,));
prmPassword.Value=bytePassword;
SqlParameter prmEmail=myCommand.Parameters.Add(new SqlParameter("@email",SqlDbType.VarChar,));
prmEmail.Value=email;
int myInt=myCommand.ExecuteNonQuery();
myCommand.Dispose();
myConnection.Close();
return myInt;
}
 //密码比较。将字符密码转换为哈西散列后直接与数据库binary密码字段比较
public int 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,));
prmAccountName.Value=accountName;
SqlParameter prmPassword=myCommand.Parameters.Add(new SqlParameter("@password",SqlDbType.Binary,));
prmPassword.Value=bytePassword;
SqlParameter prmReturnValue=myCommand.Parameters.Add(new SqlParameter("@Return_Value",SqlDbType.Int,));
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(),@Password binary()
AS
Declare @AccountId int;
Select @AccountId= AccountID From Accounts
Where accountName=@accountname;
If isnull(@AccountID,)=
Select @AccountId= -; --//账号错误!
Else
Begin
Select @accountID=null
Select @AccountId= AccountID From Accounts
Where accountName=@accountname and password=@password;
If isnull(@AccountID,)=
Select @AccountID=; --//密码错误!
End
   Return @AccountID;
 //用户增加
CREATE PROCEDURE Account_Add @accountName varchar(),@password binary (),@email varchar()
AS
insert into Accounts(accountName,password,email)
values(@accountName,@password,@email);
return @@Error;