关于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删除数据库中所有表是不难的 ...
随机推荐
- 原生JS的DOM节点操作
DOM(Document Object Model/文档对象模型)是针对HTML和XML文档的一个API.DOM节点树:在文档中出现的空格.回车.标签.注释.文本.doctype.标签等都属于DOM节 ...
- PHP 13: 类
原文:PHP 13: 类 Notice: This article is working on progress!本章将介绍PHP类.现在,基本上每种语言都支持面向对象,当然PHP也不列外.PHP在以 ...
- 快速构建Windows 8风格应用19-基础控件II
原文:快速构建Windows 8风格应用19-基础控件II 本篇博文接着上篇博文<快速构建Windows 8风格应用18-基础控件I>介绍开发Windows 8风格应用中常用控件. Sli ...
- Windows 8 常用第三方SDK使用概览
原文:Windows 8 常用第三方SDK使用概览 应用开发过程中,我们或多或少会使用到第三方的公司平台的功能,例如:新浪微博.人人网.高德地图等. 那么在Windows 8 Store App开发中 ...
- AR9331中Linux内核启动中与IRQ中断相关的文件
先列出框架,具体后继再来分析. 首先是lds文件,该文件设置了各个section在FLASH或RAM中的先后顺序. 位于~/openwrt1407/build_dir/target-mips_34kc ...
- openwrt构建过程探索
参考网站:http://wiki.openwrt.org/doc/howto/buildroot.exigence 需要下载必要的库文件,编译器等... 1 首先要获得openwrt的源码,参考ope ...
- 条件变量signal与unlock的顺序
编写同步队列时,有用到条件变量,对操作队列的线程进行同步.当队列为空时,允许get线程挂起,直到add线程向队列添加元素并通过唤醒条件变量,get线程继续向下运行.条件变量在多线程程序中用来实现“等待 ...
- Easyui布局
Easyui入门视频教程 第03集---Easyui布局 Easyui入门视频教程 第03集---Easyui布局 目录 ----------------------- Easyui入门视频教程 ...
- Lambda表达式的几种使用方式
Lambda 的表达式的编写格式如下: x=> x * 1.5 当中 “ => ” 是 Lambda 表达式的操作符,在左边用作定义一个参数列表,右边可以操作这些参数. 例一, 先把 in ...
- [转]unload dynamic library needs two dlclose() calls?
src: http://stackoverflow.com/questions/8793099/unload-dynamic-library-needs-two-dlclose-calls Quest ...