日期:2014-05-18  浏览次数:20778 次

用DecryptByKey函数解密字段问题?
我把密码字段设置为varchar,然后创建密钥,打开密钥,插入数据,可是读取的时候却显示为NULL,请问怎么回事
SQL code
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '??xxx'

CREATE SYMMETRIC KEY dcmy 
WITH ALGORITHM = DES 
ENCRYPTION BY PASSWORD = 'xxx??'

--打开对称密钥
OPEN SYMMETRIC KEY dcmy  DECRYPTION BY PASSWORD = 'xxx??'

--以加密的方式存放数据
INSERT INTO [tb_Teacher] (TeaName,TeaPwd) VALUES 
(
'老师五',

EncryptByKey(Key_GUID('dcmy'), '456456')

)

select * from tb_Teacher

select CONVERT(varchar, DecryptByKey(TeaPwd)) from tb_Teacher


对了,再问下,“PASSWORD = 'xxx??'”这句是什么意思???

------解决方案--------------------
要制定密码,
“PASSWORD = 'xxx??'”--这句就是就是指定密码的,
必须要将xxx??换成对应的密码即可.
还有,SQL版本应该是2005吧?
SQL code
create table [tb_Teacher]
(TeaName nvarchar(20)
,TeaPwd nvarchar(200));
GO

CREATE SYMMETRIC KEY dcmy 
WITH ALGORITHM = DES 
ENCRYPTION BY PASSWORD = '1234567890'

--打开对称密钥
OPEN SYMMETRIC KEY dcmy  DECRYPTION BY PASSWORD = '1234567890'


--以加密的方式存放数据
INSERT INTO [tb_Teacher] (TeaName,TeaPwd) VALUES 
(
'老师五',

EncryptByKey(Key_GUID('dcmy'), '456456')

)

select * from tb_Teacher
/*
TeaName              TeaPwd
-------------------- -----------------
老师五                  ?菮??劦洼?? ????娺簍????
*/
select CONVERT(varchar, DecryptByKey(TeaPwd)) from tb_Teacher
/*
456456
*/