我能够使用以下代码在SQL中进行列加密:
USE EncryptionDemonstration
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ThisIsMySampleStrongPassword'
CREATE CERTIFICATE MyServerCertificate WITH SUBJECT = 'This is my Demonstration Certificate'
CREATE SYMMETRIC KEY SSN_Keys
WITH ALGORITHM = AES_256
ENCRYPTION BY CERTIFICATE MyServerCertificate;
GO
CREATE SYMMETRIC KEY CreditCard_Keys
WITH ALGORITHM = AES_128
ENCRYPTION BY CERTIFICATE MyServerCertificate;
ALTER TABLE Customer
ADD SSN_Encrypted varbinary(128),
CCN_Encrypted varbinary(128)
OPEN SYMMETRIC KEY CreditCard_Keys
DECRYPTION BY CERTIFICATE MyServerCertificate
OPEN SYMMETRIC KEY SSN_Keys
DECRYPTION BY CERTIFICATE MyServerCertificate
UPDATE Customer
SET SSN_Encrypted = EncryptByKey(Key_GUID('SSN_Keys')
, SocialSecurityNumber)
UPDATE Customer
SET CCN_Encrypted = EncryptByKey(Key_GUID('CreditCard_Keys ')
, CreditCardNumber)
GO
OPEN SYMMETRIC KEY SSN_Keys
DECRYPTION BY CERTIFICATE MyServerCertificate
OPEN SYMMETRIC KEY CreditCard_Keys
DECRYPTION BY CERTIFICATE MyServerCertificate
SELECT SocialSecurityNumber, SSN_Encrypted
AS 'Encrypted SSN',
CONVERT(varchar, DecryptByKey(SSN_Encrypted))
AS 'Decrypted SSN'
FROM Customer
SELECT CreditCardNumber, CCN_Encrypted
AS 'Encrypted Credit Card Number',
CONVERT(varchar, DecryptByKey(CCN_Encrypted))
AS 'Decrypted Credit Card Number'
FROM Customer
现在我的问题是,如果有权访问此数据库的任何人只要运行DecryptByKey函数来查看解密的值,这对安全性有何帮助?我对数据库主密钥和证书的使用不完全熟悉,请耐心等待。
答案 0 :(得分:2)
答案是,每个人都可以运行DecryptByKey
,但不是每个人都可以访问用于保护数据的对称密钥。 SQL Server中有严格的encryption hierarchy,并且您选择了以下路径:
因此,有权访问对称密钥的每个人都可以使用它,但要做到这一点,就需要访问保护给定对称密钥的证书。因此,您正在使用:
OPEN SYMMETRIC KEY ... DECRYPTION BY CERTIFICATE ...
尝试创建一个无权访问CERTIFICATE
的用户并执行以上语句:
DROP USER IF EXISTS [StackOverflow];
CREATE USER [StackOverflow] WITHOUT LOGIN;
EXECUTE AS USER = 'StackOverflow';
OPEN SYMMETRIC KEY [SK_SecurityUsers_V001] DECRYPTION BY CERTIFICATE [CERT_V001];
REVERT;
您将收到以下错误:
Msg 15151, Level 16, State 1, Line 7
Cannot find the symmetric key 'SK_SecurityUsers_V001', because it does not exist or you do not have permission.
因此,取决于谁可以使用对称密钥来控制对保护它的证书的访问。问题是,谁可以访问证书?
permission diagram显示的是大图,但基本上是:
或简而言之:
另外,关于备份的一些事情。例如,当您创建备份并将其还原到其他实例上时,您需要重新创建安全性层次结构。基本上在这样的数据库上,即使sys.admin
也无法访问密钥。
因此,如果有人偷了您的备份,则数据将再次受到保护。您需要备份证书并在新数据库中再次还原它,或者使用CERTENCODED创建相同的证书。
这只是接触表面,在对数据进行加密之前要对任何方面进行很好的计划(例如,加密列上的索引是无用的,因为在加密时会向每个值添加随机盐-多次加密一个值,您会得到不同的结果二进制文件)。