我已按照http://msdn.microsoft.com/en-us/library/ms179331.aspx对 SQL Server 中的列进行加密。
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'T3stP@ssword'
CREATE CERTIFICATE TestCert WITH SUBJECT = 'SSN Encryption'
CREATE SYMMETRIC KEY HRKey
WITH ALGORITHM = DES
ENCRYPTION BY CERTIFICATE TestCert;
OPEN SYMMETRIC KEY HRKey
DECRYPTION BY CERTIFICATE TestCert;
UPDATE [dbo].[Person]
SET [EncryptedSSN] = EncryptByKey(Key_GUID('HRKey'), CAST(SSN AS varchar(10)));
SELECT TOP 10 * FROM Person
1 Tom Thomson 111111111 0x0026DA624DBCA04CBCBF621FD5F......
2 Steve Stephenson 222222222 0x0026DA624DBCA04CBCBF6.....
表明我已经加密了数据,这正是我想要的。
OPEN SYMMETRIC KEY HRKey
DECRYPTION BY CERTIFICATE TestCert;
SELECT PersonID, Firstname, Lastname [SSN],
CONVERT(VARCHAR, DecryptByKey([EncryptedSSN]))
AS 'Decrypted SSN'
FROM [dbo].[Person]
1 Tom Thomson 111111111 111111111
2 Steve Stephenson 222222222 222222222
Close SYMMETRIC KEY HRKey
忽略我还有一个未加密的列,问题是这有什么真正改进,我很容易打开密钥并用它来解密数据。我想我缺少一些理解。