在SQL Server查询中解密密码

时间:2013-06-28 08:24:33

标签: sql sql-server

CREATE ASYMMETRIC KEY Asym_EPassword  
WITH ALGORITHM = RSA_512 
ENCRYPTION BY PASSWORD = testdemo123

我已使用上述加密非对称密钥加密了我的表格的Password列。

现在我要解密password查询

中的select

有人可以建议怎么做吗?

1 个答案:

答案 0 :(得分:1)

SELECT CONVERT(NVARCHAR(100), 
    DecryptByAsymKey(AsymKey_ID('Asym_EPassword'),
    YourColumn, 'testdemo123')) AS PlainText
FROM YourTable;

SQLFiddle不允许用户创建非对称密钥,因此将其复制/粘贴到您自己的SSMS:

CREATE ASYMMETRIC KEY Asym_EPassword
WITH ALGORITHM = RSA_512 
ENCRYPTION BY PASSWORD = 'testDemo123!'

DECLARE @PlainText NVARCHAR(100)
DECLARE @CipherText VARBINARY(MAX)

SET @PlainText = 'AAAAA'
SELECT @CipherText = EncryptByAsymKey(AsymKey_ID('Asym_EPassword'), @PlainText);

SELECT @CipherText;

SELECT CONVERT(NVARCHAR(100), DecryptByAsymKey(AsymKey_ID('Asym_EPassword'), @CipherText, N'testDemo123!'));