关于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;
关于SQL 数据表中的密码加密的更多相关文章
- 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 )性别 ...
- Sql数据表中的关系
- SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型
原文:SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测 ...
- SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int
--SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int --关键说明:--1.从系统表syscolumns中的查询所有xtype='48'的记录得到类型为[tinyint]的字段- ...
- 在数据表中添加一个字段的SQL语句怎么写
如果要在数据表中添加一个字段,应该如何表示呢?下面就为您介绍表添加字段的SQL语句的写法,希望可以让您对SQL语句有更深的认识. 通用式: alter table [表名] add [字段名] 字 ...
- 读取数据表中第m条到第n条的数据,SQL语句怎么写?
原文:读取数据表中第m条到第n条的数据,SQL语句怎么写? 对于MySQL或者Oracle来说,如果实现从Table 表中取出第 m 条到第 n 条的记录操作,我们需要TOP函数(不是所有的数据库都支 ...
- mssql sqlserver 使用sql脚本检测数据表中一列数据是否连续的方法分享
原文地址:http://www.maomao365.com/?p=7335 摘要: 数据表中,有一列是自动流水号,由于各种操作异常原因(或者插入失败),此列数据会变的不连续,下文将讲述使用sql ...
- Sql Server删除数据表中重复记录 三种方法
本文介绍了Sql Server数据库中删除数据表中重复记录的方法. [项目]数据库中users表,包含u_name,u_pwd两个字段,其中u_name存在重复项,现在要实现把重复的项删除![分析]1 ...
- 转:Sql Server中清空所有数据表中的记录
如果要删除数据表中所有数据只要遍历一下数据库再删除就可以了,清除所有数据我们可以使用搜索出所有表名,构造为一条SQL语句进行清除了,这里我一一给各位同学介绍. 使用sql删除数据库中所有表是不难的 ...
随机推荐
- php操作xml并插入到数据库中
php操作xml并插入到数据库中 <? php header('content-type:text/html;charset=utf-8'); mysql_connect('localhost' ...
- Asp.Net MVC页面静态化功能实现一:利用IHttpModule,摒弃ResultFilter
上一篇有提到利用IHttpModule和ResultFilter实现页面静态化功能.后来经过一些改动,将ResultFilter中要实现的功能全部转移到IHttpModule中来实现 Asp.Net ...
- 由浅入深学习.NET CLR 系列:目录
经过对Android的一阵折腾,些许熟悉了一些Java的东东,又开始转战.NET.我觉得学习最好和工作不要相离太远,才会更加随笔随意,索性整理一些比较系统的.NET的基础知识学习学习.一提起学习.NE ...
- HPQC HP Quality Center windows 服务
HPQC HP Quality Center windows 服务已经启动的话,就不用运行run.bat 两个是一样的效果.
- REST 测试工具
两款 REST 测试工具 用CURL命令行测试REST API 无疑是低效率的,这里把最近使用的两款 Chrome 插件总结下 POSTMAN 简单易用 REST Console 功能强大 使用的话用 ...
- Entity Framework,TransactionScope 混合使用的问题讨论
using (var ts = new TransactionScope()) { string connStr = "Data Source=.;Initial Catalog=Test; ...
- Julia语言:让高性能科学计算人人可用
Julia语言:让高性能科学计算人人可用要:一群科学家对现有计算工具感到不满:他们想要一套开源系统,有C的快速,Ruby的动态,Python的通用,R般在统计分析上得心应手,Perl的处理字符串处理, ...
- NET系列文章
NET系列文章 由于博主今后一段时间可能会很忙(准备出书:<.NET框架设计—模式.配置.工具>,外加换了新工作),所以博客会很少更新: 在最近一年左右时间里,博主各种.NET技术类型的文 ...
- WCF基于MSMQ的事件代理服务
前言 公司目前楼主负责的项目正在改版升级,对之前的服务也在作调整,项目里有个操作日志的模块,就决定把日志单独提取出来,做个日志服务,所以就有了这篇文章 正文 MSMQ作为消息队列,B/S项目调用日志服 ...
- nginx 安装启动
[root@localhost ~]# wget http://nginx.org/download/nginx-0.7.67.tar.gz --2010-09-24 14:48:12-- http: ...