出于安全原因,我的应用程序中有几列需要加密。SQL Server 2008 怎么可能做到这一点?另外,我需要总是手动解密它们还是会自动发生?
谢谢,
萨钦
出于安全原因,我的应用程序中有几列需要加密。SQL Server 2008 怎么可能做到这一点?另外,我需要总是手动解密它们还是会自动发生?
谢谢,
萨钦
您可以创建一个Certificate
然后Symmetric Key
使用证书的一个:
CREATE CERTIFICATE YourCertificate
WITH SUBJECT = 'Encrypted Content';
GO
CREATE SYMMETRIC KEY SSN_Key
WITH ALGORITHM = AES_256
ENCRYPTION BY CERTIFICATE YourCertificate;
GO
解密:您可以使用DecryptByKey
:
来自 MSDN 的示例是:
SELECT CardNumber, CardNumber_Encrypted AS 'Encrypted card number',
CONVERT(nvarchar,
DecryptByKey(CardNumber_Encrypted, 1 ,
HashBytes('SHA1', CONVERT(varbinary, CreditCardID)))) AS 'Decrypted card number'
FROM Sales.CreditCard;
http://msdn.microsoft.com/en-us/library/ms179331(v=sql.105).aspx
看看 MSDN 帖子如何:加密数据列
首先:使用以下代码创建您的合成密钥:
USE [yourDB]
GO
CREATE ASYMMETRIC KEY ClaveAsym
WITH ALGORITHM = RSA_2048 ---->Take a look in this type of algorithms
ENCRYPTION BY PASSWORD = 'yourKey'
GO
请记住这一点,您必须始终声明要解密或加密的变量:
DECLARE @KEYID INT SET @KEYID = AsymKey_ID('ClaveAsym')
解密数据
SELECT columnA, CAST(DecryptByAsymKey(@keyid, columnUser, N'yourKey') AS VARCHAR(100)), CONVERT(VARCHAR(MAX), DECRYPTBYASYMKEY(@keyId, columnPass, N'yourKey')) FROM yourTable
加密数据
DECLARE @User VARCHAR(MAX), @pass VARCHAR(MAX)
SET @User = 'yourUser'
sET @pass = 'yourPass'
DECLARE @KEYID INT SET @KEYID = AsymKey_ID('ClaveAsym')
INSERT INTO yourTable( User, passw) VALUES EncryptByAsymKey (@keyid, @User ), EncryptByAsymKey (@keyid, @pass))