2023年6月21日发(作者:)
关于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 graphy名称空间下的SHA1类的ComputeHash()⽅法将字符密码进⾏哈希散列运算转换为byte[]类型对象,保存⼊数据库。实例代码:
1 //哈系散列转换2 public byte[] getSaltedPassword(string password)3 {4 SHA1 sha1=();5 //应⽤空间下的es⽅法获得byte.6 byte[] bytePassword=eHash(es(password));7 return bytePassword;8 }
1 //数据存⼊,直接将byte[]保存⼊binary字段 2 public int AccountRegister(string accountName,string password,string email) 3 { 4 byte[] bytePassword = tedPassword(password); 5 SqlConnection myConnection = new SqlConnection(nStr); 6 (); 7 SqlCommand myCommand = new SqlCommand("Account_Add",myConnection); 8 dType=Procedure; 9 SqlParameter prmAccountName=(new SqlParameter("@AccountName",r,50));10 =accountName;11 SqlParameter prmPassword=(new SqlParameter("@password",,50));12 =bytePassword;13 SqlParameter prmEmail=(new SqlParameter("@email",r,50));14 =email;15 int myInt=eNonQuery();16 e();17 ();18 return myInt;19 } 1 //密码⽐较。将字符密码转换为哈西散列后直接与数据库binary密码字段⽐较 2 public int AccountVerify(string accountName,string password) 3 { 4 byte[] bytePassword = tedPassword(password); 5 SqlConnection myConnection = new SqlConnection(nStr); 6 (); 7 SqlCommand myCommand = new SqlCommand("Account_Check",myConnection); 8 dType=Procedure; 9 SqlParameter prmAccountName=(new SqlParameter("@AccountName",r,50));10 =accountName;11 SqlParameter prmPassword=(new SqlParameter("@password",,50));12 =bytePassword;13 SqlParameter prmReturnValue=(new SqlParameter("@Return_Value",,4));14 ion=Value;15 eNonQuery();16 int accountID=(int);17 e();18 ();19 return accountID;20 }//相关存储过程(Store procedure) 1 //登陆验证 2 CREATE PROCEDURE Account_Check @AccountName varchar(50),@Password binary(50) 3 AS 4 Declare @AccountId int;
5 Select @AccountId= AccountID From Accounts 6 Where accountName=@accountname; 7 If isnull(@AccountID,0)=0
8 Select @AccountId= -1; --//账号错误!
9 Else10 Begin11 Select @accountID=null12 Select @AccountId= AccountID From Accounts13 Where accountName=@accountname and password=@password;14 If isnull(@AccountID,0)=015 Select @AccountID=0; --//密码错误!
16 End
17 Return @AccountID;1 //⽤户增加2 CREATE PROCEDURE Account_Add @accountName varchar(50),@password binary (50),@email varchar(50)3 AS4 insert into Accounts(accountName,password,email)5 values(@accountName,@password,@email);6 return @@Error;
2023年6月21日发(作者:)
关于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 graphy名称空间下的SHA1类的ComputeHash()⽅法将字符密码进⾏哈希散列运算转换为byte[]类型对象,保存⼊数据库。实例代码:
1 //哈系散列转换2 public byte[] getSaltedPassword(string password)3 {4 SHA1 sha1=();5 //应⽤空间下的es⽅法获得byte.6 byte[] bytePassword=eHash(es(password));7 return bytePassword;8 }
1 //数据存⼊,直接将byte[]保存⼊binary字段 2 public int AccountRegister(string accountName,string password,string email) 3 { 4 byte[] bytePassword = tedPassword(password); 5 SqlConnection myConnection = new SqlConnection(nStr); 6 (); 7 SqlCommand myCommand = new SqlCommand("Account_Add",myConnection); 8 dType=Procedure; 9 SqlParameter prmAccountName=(new SqlParameter("@AccountName",r,50));10 =accountName;11 SqlParameter prmPassword=(new SqlParameter("@password",,50));12 =bytePassword;13 SqlParameter prmEmail=(new SqlParameter("@email",r,50));14 =email;15 int myInt=eNonQuery();16 e();17 ();18 return myInt;19 } 1 //密码⽐较。将字符密码转换为哈西散列后直接与数据库binary密码字段⽐较 2 public int AccountVerify(string accountName,string password) 3 { 4 byte[] bytePassword = tedPassword(password); 5 SqlConnection myConnection = new SqlConnection(nStr); 6 (); 7 SqlCommand myCommand = new SqlCommand("Account_Check",myConnection); 8 dType=Procedure; 9 SqlParameter prmAccountName=(new SqlParameter("@AccountName",r,50));10 =accountName;11 SqlParameter prmPassword=(new SqlParameter("@password",,50));12 =bytePassword;13 SqlParameter prmReturnValue=(new SqlParameter("@Return_Value",,4));14 ion=Value;15 eNonQuery();16 int accountID=(int);17 e();18 ();19 return accountID;20 }//相关存储过程(Store procedure) 1 //登陆验证 2 CREATE PROCEDURE Account_Check @AccountName varchar(50),@Password binary(50) 3 AS 4 Declare @AccountId int;
5 Select @AccountId= AccountID From Accounts 6 Where accountName=@accountname; 7 If isnull(@AccountID,0)=0
8 Select @AccountId= -1; --//账号错误!
9 Else10 Begin11 Select @accountID=null12 Select @AccountId= AccountID From Accounts13 Where accountName=@accountname and password=@password;14 If isnull(@AccountID,0)=015 Select @AccountID=0; --//密码错误!
16 End
17 Return @AccountID;1 //⽤户增加2 CREATE PROCEDURE Account_Add @accountName varchar(50),@password binary (50),@email varchar(50)3 AS4 insert into Accounts(accountName,password,email)5 values(@accountName,@password,@email);6 return @@Error;
发布评论