惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
博客园 - Franky
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
博客园 - 司徒正美
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
博客园_首页
S
SegmentFault 最新的问题
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
H
Help Net Security
MongoDB | Blog
MongoDB | Blog

博客园 - 董晓涛

如何将存储过程执行后的结果集放入临时表 Detect SQL timeout from ASP & Issue RollbackTrans SQL Server 2005 symmetric encrytion sample 如何解决:Error 14274: 无法添加、更新或删除从MSX服务器上发起的作业. Useful Links(to Learn SQL Server) Convert IP To Numberic Generate table structure Generate Time Dim Generate Insert data script on a table. - 董晓涛 Execute T-SQL asynchronously Don't Use Select * Sql Server数据库被置疑后解决方法 SQL Server 2005中对BLOB的支持(ntext,text and image) SQL Server 2000 Service Pack 4 is released 数据规范化 Microsoft SQL Server 2005 and Vs.net 2005 April 2005 Version XML IN 20 MINUTES! CLR Integrated in SQL Server 2005 XQuery in SQL Server 2005
Encrypting Data With the SQL Server Encrypt Function
董晓涛 · 2005-06-29 · via 博客园 - 董晓涛

A common question I'm asked by clients is how to encrypt data and store it in SQL Server. One of the major problems I see in the field is when people store sensitive data unencrypted into SQL Server. For example, if a password is stored unencrypted into SQL Server, a malitious user could easily read all of the passwords with a simple select statement. You can develop your own COM mechanisms to encrypt the password but in this article, we'll discuss a method that is build into SQL Server.

Since SQL Server 6.x, you can use the ENCRYPT function to encrypt data with the same method used by the WITH ENCRYPTION keyword. There's a rather large problem that I will discuss after the example. To use the ENCRYPT function, use it before the string value as shown below:

SELECT ENCRYPT('TestPW1')

Will output the following result:

------------------------------ 
0x5400650073007400500057003100
 
(1 row(s) affected)

Let's go ahead and create a sample table and try out this function. Create the following user table and load the sample data below:

CREATE TABLE Users ( 
UserID Varchar(10),
UserPW Varchar (20))
 
INSERT INTO USERS values('TestUser1',ENCRYPT('TestPW1'))
INSERT INTO USERS values('TestUser2',ENCRYPT('TestPW2'))
INSERT INTO USERS values('TestUser3',ENCRYPT('TestPW3'))
INSERT INTO USERS values('TestUser4',ENCRYPT('TestPW4'))

If you now select the data it will appear encrypted. Notice if you run a SELECT ENCRYPT('TestPW1') that the data that you see different that what appears when you select out of the Users table after you insert the value. The algorithm that SQL Server uses to encrypt the data is relativily easy and is case sensitive until it's stored into a table. At that point it becomes very difficult to read.

Data stored in an encrypted column can be used to store passwords. Once encrypted, you can't directly unencrypt the data. You could only perform checks against it as shown below:

SELECT * from Users where UserID = 'TestUser2' 
            and UserPW = ENCRYPT('TestPW2')

Keep in mind that the above command is case sensitive. If you want this to be case-insensitive, it is best to store all the data in uppercase by using the UPPER function. The UPPER function will have to be used in the insert statement as well as the select statement that we've mentioned. For example, the insert statement would look like this:

INSERT INTO USERS values('TestUser1',ENCRYPT(UPPER('TestPW1')))
 
SELECT * from Users where UserID = 'TestUser2' 
            and UserPW = ENCRYPT(UPPER('TestPW2'))

Another note to mention is that like any nicely encrypted data, the data may appear one length when viewing it, but is actually stored at a different length.

UserID     UserPW               
---------- -------------------- 
TestUser1  T
TestUser2  T
TestUser3  T
TestUser4  T
 
(4 row(s) affected)

But in actuality if we select the length of the field by using the LEN function, we can see the true length.

 
userid     Length      
---------- ----------- 
TestUser1  14
TestUser2  14
TestUser3  14
TestUser4  14
 
(4 row(s) affected)

With that point made, make sure that the length of your column represents the encrypted length, not the unencrypted lenth. Now, for the large caveat that I mentioned earlier. This stored procedure is unsupported by Micrososft and they could easily change it or rip it out. A good example of this function changing was between SQL Server 6.5 and 7.0. Another common question is decryption. Obiously, Microsoft does not make this easy and strings can only be easily decrypted using the comparison technique I showed earlier. If you don't use this method to encrypt your passwords, there are other methods that are much better. These methods are much more robust and secure than what we discussed. For example, Les Smith has 2 articles that show you how to use Java or COM to encrypt passwords at : http://www.sqlservercentral.com/columnists/lsmith/usingjavatoencryptpasswords.asp. If you're serious about encryption of your data, the encrypt function is not the way to go. Instead, use one of the other custom methods that Les Smith mentions in his articles. These types of methods of encryption will not change from release to release but require slightly more work.