在SQL Server中,怎样将数据加密存储到表中?该怎么解决

在SQL Server中,怎样将数据加密存储到表中?
在SQL Server中,希望能将用户的密码加密以后存储到表中,如何编写Transact-SQL语言,希望能给出程序代码。

------解决方案--------------------
http://topic.csdn.net/u/20100105/10/5a345ab4-17a9-45c0-a8ab-2f01e1000ea0.html?17358

参考这个加密的ROY大哥的
------解决方案--------------------
SQL code
if object_ID('test') is not null
    drop function test
go
create function test
(
    @Str nvarchar(4000),--加密的字符串
    @Flag bit,--1、加密 0、解密
    @Key nvarchar(50)--密文
)
returns nvarchar(4000)
with Encryption
as
begin
Declare @LenStr int,@i int,@Str2 nvarchar(4000),@Split nvarchar(2),@LenKey int
select @Str=@Str+'A',@LenStr=len(@Str),@i=1,@Str2='',@LenKey=Len(@Key+'A')-1
while @i<@LenStr
        select    @Split=substring(@Str,@i,1),
                @Split=nchar((unicode(@Split)+case @Flag    when 1 then unicode(substring(@Key+'A',@i%@LenKey+1,1))-1                                                
                                                    when 0 then 65535-unicode(substring(@Key+'A',@i%@LenKey+1,1))
                                                    else 0 end)%65535+cast(@Flag as int)),
                @Str2=@Str2+@Split,@i=@i+1
return @Str2

end
go


----------------------------------use-----------------------
select dbo.test(N'1234567',1,'dota')
select dbo.test(N' ¦”˜¤ª˜',0,'dota')

------解决方案--------------------
http://blog.csdn.net/fredrickhu/archive/2009/09/20/4574249.aspx
SQL code
转贴自teched讲师:  牛可   

基本概念:

第一层 服务主密钥

备份服务主密钥
backup service master key to file='c:\smk.bak'
encryption by password='P@ssw0rd'

restore service master key from file='c:\smk.bak'
decryption by password='P@ssw0rd'

第二层 数据库主密钥
1)必须先在该数据库上创建数据库主密钥才能使用
create master key encryption by password='P@ssw0rd'

2)使用数据库主密钥
-如果数据库主密钥使用服务密钥进行保护,则在使用时会自动打开
opren master key decryption by password='P@ssw0rd'

3)查看数据库主密钥状态
sys.symmetric_keys

4)备份数据库主密钥
backup master key to file='c:\smk.bak'
encryption by password='P@ssw0rd'

restore master key from file='c:\smk.bak'
decryption by password='P@ssw0rd'


数字证书
创建自签名
create certificate cert_myCert
encryption by password='P@ssw0rd'
with subject='Self Signed Cert',
start_date='1/31/2006'
expiry_date='1/31/2008'


非对称密钥
创建新的密钥对
create asymmetric key asy_Key1
with algorithm=RSA_2048
encryption by password='P@ssw0rd'


对称密钥
创建新的密钥对
create symmetric key SymKeyMarketing3
with algorithm=AES_2048
encryption by certificate asy_Key1

使用对称密钥
使用前必须打开
open symmetric SymKeyMarketing3
decryption by certificate asy_Key1

sys.open_keys


数据列加密
-使用对称密钥加密大量的列数据
-考虑使用证书,非对称密钥保护对称密钥

防止绕过加密数据列的攻击-使用验证器

注:
在加密列上的索引将变得无效
加密数据列的长度增长,建议使用varbinary(max)数据类型
修改已有的dml语句以支持加密的数据列

-----***********示例1 了解数据库加密体系结构*****-----

--************(1) 服务主密钥
--准备工作
--创建测试数据库TestDB
--1)备份服务主密钥
backup service master key to file='g:\smk.bak'
encryption by password='p@ssw0rd'

--2)生成新的主密钥
Alter service master key regenerate

--3)从备份文件还原服务主密钥
Restore service master key from file= file='g:\smk.bak'
encryption by password='p@ssw0rd'

--*************(2) 数据库主密钥
--1)为数据库创建数据库主密钥
create master key encryption by password='p@ssw0rd'