首先,都知道一个字节(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;

关于SQL 数据表中的密码加密的更多相关文章

  1. sql数据表中的值重新命名

    select u.id,u.name,u.sex, 2 (case u.sex 3 when 1 then '男' 4 when 2 then '女' 5 else '空的' 6 end 7 )性别 ...

  2. Sql数据表中的关系

  3. SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型

    原文:SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测 ...

  4. SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int

    --SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int --关键说明:--1.从系统表syscolumns中的查询所有xtype='48'的记录得到类型为[tinyint]的字段- ...

  5. 在数据表中添加一个字段的SQL语句怎么写

    如果要在数据表中添加一个字段,应该如何表示呢?下面就为您介绍表添加字段的SQL语句的写法,希望可以让您对SQL语句有更深的认识.   通用式: alter table [表名] add [字段名] 字 ...

  6. 读取数据表中第m条到第n条的数据,SQL语句怎么写?

    原文:读取数据表中第m条到第n条的数据,SQL语句怎么写? 对于MySQL或者Oracle来说,如果实现从Table 表中取出第 m 条到第 n 条的记录操作,我们需要TOP函数(不是所有的数据库都支 ...

  7. mssql sqlserver 使用sql脚本检测数据表中一列数据是否连续的方法分享

    原文地址:http://www.maomao365.com/?p=7335 摘要:    数据表中,有一列是自动流水号,由于各种操作异常原因(或者插入失败),此列数据会变的不连续,下文将讲述使用sql ...

  8. Sql Server删除数据表中重复记录 三种方法

    本文介绍了Sql Server数据库中删除数据表中重复记录的方法. [项目]数据库中users表,包含u_name,u_pwd两个字段,其中u_name存在重复项,现在要实现把重复的项删除![分析]1 ...

  9. 转:Sql Server中清空所有数据表中的记录

    如果要删除数据表中所有数据只要遍历一下数据库再删除就可以了,清除所有数据我们可以使用搜索出所有表名,构造为一条SQL语句进行清除了,这里我一一给各位同学介绍.   使用sql删除数据库中所有表是不难的 ...

随机推荐

  1. 空间闹钟-v1.6更新!

    (假设图片无法显示可查看我的qq空间:http://user.qzone.qq.com/805853418/blog/1398785778) 生活助手系列--空间闹钟================= ...

  2. leetcode第11题--Container With Most Water

    Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  3. 智能的API、云服务和SOA测试解决方案——Parasoft SOAtest

    依赖Parasoft测试解决方案的机构,不仅有小企业,政府机构,还有世界500强集团.Parasoft公司推出的Parasoft SOAtest,提供了API.云服务和SOA最全面的测试解决方案.此次 ...

  4. 在Ubuntu 12.04 - 64bit中安装CodeSourcery时提示错误

    安装时提示错误,Your 64-bit Linux host is missing the 32-bit libraries requied to install and use Sourcery C ...

  5. jquery实现文字上下滚动效果

    文字上下滚动是经常用到的js效果,这里介绍一种上下渐隐渐出的文字展现效果! 代码实现很简单,只需要引入jquery就可以. 代码如下: <!DOCTYPE> <head> &l ...

  6. Jqury笔记

    1.  --------------- -var aa = new Array(); aa.push(1); alert(aa[0]); var aa=[];也表示一个数组: ------------ ...

  7. Windows 下让 Python 多个版本共存(支持 pip)

    转载自 http://blog.kgzx.net/index.php/archives/40/ 因为类库兼容性的关系,写实际项目时都是用 Python 2,但自己试验性的写点小东西还是喜欢用 Pyth ...

  8. oracle琐碎笔记

    Oracle知识点 ps:由于是自己看的所以笔记比较乱,大家谅解 Commit rollback Sql核心语句之select Selct中要用到以下语句 From语句 Where语句 Group b ...

  9. JS实现等比例缩放图片

    JS实现等比例缩放图片 2014-01-19 21:57 by 龙恩0707, 40 阅读, 0 评论, 收藏, 编辑 JS实现等比例缩放图片 有时候我们前端页面只有500×500像素的宽和高的布局, ...

  10. android中自定义Theme以及TitleBar

    1.自定义Theme. 在res/values/styles.xml中的resources块中添加如下代码: <style name="StatusBarBackground" ...