下面是我在SQL 2008 R2中调试成功的语句,已对关键语句做了备注,其它的看不懂的,百度一下吧。
--建立对称密钥
--CREATE SYMMETRIC KEY key1 WITH ALGORITHM=DES ENCRYPTION BY PASSWORD='123456'
OPEN SYMMETRIC KEY key1
DECRYPTION BY PASSWORD='123456' --必须与原设定口令一样
DECLARE @str NVARCHAR(100),@Encrypted VARBINARY(MAX),@DEEncrypted varchar(8000)
SET @str=N'AABBCC' --要加密的字符串
SELECT @Encrypted=ENCRYPTBYKEY(KEY_GUID('key1'),@str)
SELECT @DEEncrypted=CAST(DECRYPTBYKEY(@Encrypted) AS NVARCHAR(100))
SELECT '加密'=@Encrypted ,'解密'=@DEEncrypted --显示加密与解密结果
UPDATE [_TABLE1] SET mingwen = @Encrypted,mingeen = @DEEncrypted
CLOSE SYMMETRIC KEY key1
--删除对称密钥
--DROP SYMMETRIC KEY key1