SQL数据库加密方法

xiaoxiao2021-03-01  21

转贴自teched讲师: 牛可

基本概念:

第一层 服务主密钥

备份服务主密钥backup service master key to file='c:\smk.bak'encryption by password='P@ssw0rd'restore service master key from file='c:\smk.bak'decryption by password='P@ssw0rd'第二层 数据库主密钥1)必须先在该数据库上创建数据库主密钥才能使用create master key encryption by password='P@ssw0rd'2)使用数据库主密钥-如果数据库主密钥使用服务密钥进行保护,则在使用时会自动打开opren master key decryption by password='P@ssw0rd'3)查看数据库主密钥状态sys.symmetric_keys4)备份数据库主密钥backup master key to file='c:\smk.bak'encryption by password='P@ssw0rd'restore master key from file='c:\smk.bak'decryption by password='P@ssw0rd'数字证书创建自签名create certificate cert_myCertencryption by password='P@ssw0rd'with subject='Self Signed Cert',start_date='1/31/2006'expiry_date='1/31/2008'非对称密钥创建新的密钥对create asymmetric key asy_Key1with algorithm=RSA_2048encryption by password='P@ssw0rd'对称密钥创建新的密钥对create symmetric key SymKeyMarketing3with algorithm=AES_2048encryption by certificate asy_Key1使用对称密钥使用前必须打开open symmetric SymKeyMarketing3decryption by certificate asy_Key1sys.open_keys数据列加密-使用对称密钥加密大量的列数据-考虑使用证书,非对称密钥保护对称密钥防止绕过加密数据列的攻击-使用验证器注:在加密列上的索引将变得无效加密数据列的长度增长,建议使用varbinary(max)数据类型修改已有的dml语句以支持加密的数据列-----***********示例1 了解数据库加密体系结构*****-------************(1) 服务主密钥--准备工作--创建测试数据库TestDB--1)备份服务主密钥backup service master key to file='g:\smk.bak'encryption by password='p@ssw0rd'--2)生成新的主密钥Alter service master key regenerate--3)从备份文件还原服务主密钥Restore service master key from file= file='g:\smk.bak'encryption by password='p@ssw0rd'--*************(2) 数据库主密钥--1)为数据库创建数据库主密钥create master key encryption by password='p@ssw0rd'go--2)查看数据库加密状态select [name],is_master_key_encrypted_by_server from sys.databases where name='TestDB'--3)查看数据库主密钥的信息select * from sys.symmetric_keys --4)备份数据库主密钥backup master keyto file='g:\testdbkey.bak'encryption by password='p@ssw0rd'--5)删除服务主密钥对数据库主密钥的保护--创建非对称密钥成功,自动使用服务主密钥解密并使用该数据库主密钥create asymmetric key asy_Testkey1 with algorithm=RSA_1024go--删除服务主密钥对数据库主密钥的保护alter master keydrop encryption by service master keygo--查看数据库加密状态select [name],is_master_key_encrypted_by_server from sys.databases where name='TestDB'--创建非对称密钥失败,因为数据库主密钥未打开create asymmetric key asy_Testkey2 with algorithm=RSA_1024go--打开数据库主密钥open master key decryption by password='p@ssw0rd'select * from sys.openkeysgo--创建非对称密钥成功create asymmetric key asy_Testkey2 with algorithm=RSA_1024go--恢复服务主密钥对数据库主密钥的保护alter master keyadd encryption by service master keyclose master keygo--*****(3)证书--1)创建自签名证书create certificate cert_Testcertencryption by password='p@ssw0rd'with subject='TestCert1',start_date='1/31/2006',expiry_date='1/31/2008'goselect * from sys.certificates--2)从文件导入证书Create certificate cert_TestCert2From file=’g:\MSCert.cer’Go-- 3)备份导出证书和密钥backup certificate cert_Testcertto file='g:\Testcert.cer'with private key(decryption by password='p@ssw0rd',file='g:\TestCert_pvt',--私密钥encryption by password='p@ssw0rd' )go--4)使用证书加解密数据--加密:使用证书的公钥declare @cleartext varbinary(200)declare @cipher varbinary(200)set @cleartext=convert( varbinary(200),'Test text string')set @cipher=EncryptByCert(Cert_ID('cert_TestCert'),@cleartext)select @cipher--解密:使用证书的私钥select convert(varchar(200),DecryptByCert(Cert_ID('cert_TestCert'),@cipher,N'p@ssw0rd')) as [cleartext]--5) 删除证书私钥alter certificate cert_TestCertremove private keygo--加密成功declare @cleartext varbinary(200)declare @cipher varbinary(200)set @cleartext=convert( varbinary(200),'Test text string')set @cipher=EncryptByCert(Cert_ID('cert_TestCert'),@cleartext)select @cipher--解密失败:因为私钥被删除select convert(varchar(200),DecryptByCert(Cert_ID('cert_TestCert'),@cipher,N'p@ssw0rd')) as [cleartext]--***(4) 非对称密钥--1)使用sn.exe生成非对称密钥,安装vs2005后有sn.exe, 在命令行方式下执行sn -k g:\asy_Test.key--2)从文件创建非对称密钥create asymmetric key asm_Testfrom file='g:\asy_Test.key'encryption by password='p@ssw0rd'goselect * from sys.asymmetric_keys--***********示例2 使用加密保护列数据-----*****(1) 准备工作--1) 创建示例表create table empsalary(EmpID int,Title nvarchar(50),Salary varbinary(500))go--2) 创建数据库主密钥create master key encryption by password='p@ssw0rd'go--3) 创建用于加密的对称密钥create symmetric key sym_Salarywith algorithm=AES_192encryption by password='p@ssw0rd'goselect * from sys.symmetric_keys where [name]='sym_Salary'------****(2)加密列数据--1)打开对称密钥open symmetric key sym_Salarydecryption by password='p@ssw0rd'goselect * from sys.openkeys--2)向表中插入数据,并对salary列加密insert into empsalary values (1,'CEO',EncryptByKey(KEY_GUID('sym_Salary'),'20000'))insert into empsalary values (2,'Manager',EncryptByKey(KEY_GUID('sym_Salary'),'10000'))insert into empsalary values (3,'DB Admin',EncryptByKey(KEY_GUID('sym_Salary'),'5000'))--3)关闭打开的对称密钥close symmetric key sym_Salarygoselect * from sys.openkeys--4) 查看表中的数据select * from empsalary--(3)解密并访问被加密的数据列--1)打开对称密钥open symmetric key sym_Salary decryption by password='p@ssw0rd'go--2)使用对称密钥解密并访问被加密的列select empid,title,cast(DecryptByKey(Salary) as varchar(20)) as salary from empsalary--3) 关闭对称密钥close symmetric key sym_Salarygo--(4)绕过加密数据的攻击--1)攻击者使用其他数据行的加密数据替换某一行的数据update empsalary set salary=(select salary from empsalary where empid=1)where empid=3--2)查看被攻击后解密的数据open symmetric key sym_Salary decryption by password='p@ssw0rd'select empid,title,cast(DecryptByKey(Salary) as varchar(20)) as salary from empsalaryclose symmetric key sym_Salary--(5)使用验证器防止绕过加密数据的攻击--1)删除前面添加的数据行delete empsalary--2)向表插入数据,并对salary列的数据使用验证器进行加密open symmetric key sym_Salary decryption by password='p@ssw0rd'insert into empsalary values (1,'CEO',EncryptByKey(KEY_GUID('sym_Salary'),'20000',1,'1'))insert into empsalary values (2,'Manager',EncryptByKey(KEY_GUID('sym_Salary'),'10000',1,'2'))insert into empsalary values (3,'DB Admin',EncryptByKey(KEY_GUID('sym_Salary'),'5000',1,'3'))--3)解密并访问被加密的数据select empid,title,cast(DecryptByKey(Salary,1,cast(empid as varchar(3))) as varchar(20)) as salary from empsalary--4)用同样的方法篡改数据update empsalary set salary=(select salary from empsalary where empid=1)where empid=3--5)被篡改后的加密了的数据列变成无效select empid,title,cast(DecryptByKey(Salary,1,cast(empid as varchar(3))) as varchar(20)) as salary from empsalary--***********示例3 使用数字证书签署存储过程--*****(1)准备--1)创建数据库主密钥create master key encryption by password='p@ssw0rd'--2)创建签署存储过程所需要的证书create certificate cert_Productwith subject='Products Sign',start_date='1/31/2006',expiry_date='1/31/2008'go--3)创建SPDeveloper登录帐户和用户,该用户创建并访问Products表的存储过程create login [SPDeveloper] with password='p@ssw0rd',default_database=[TestDB]gocreate user [SPDeveloper] for login SPDeveloper with default_schema=[SPDeveloper]gocreate schema products authorization SPDevelopergoexec sp_addrolemember @rolename='db_owner',@membername='SPDeveloper'--4)以SPDeveloper的身份创建存储过程products.usp_Productsexecute as user='SPDeveloper'gocreate procedure products.usp_Productsasselect * from dbo.Productsgorevertselect user--5)创建普通用户jerrycreate login [jerry] with password='p@ssw0rd',default_database=[TestDB]gocreate user [jerry] for login jerry go--*******(2)使用证书签署存储过程--1)授予用户jerry执行存储过程的权限grant execute on products.usp_Products to jerry--2)以jerry的身份执行存储过程失败,因为拥有权链的断裂的execute as user='jerry'select usergoexecute products.usp_Productsgorevert--3)使用证书在当前数据库创建用户ProductsReader,并为该用户赋予读取products表的权限create user ProductsReader for certificate cert_Productsgogrant select on products To ProductsReader--4)使用证书签署当前存储过程add signature to products.usp_Products by certificate cert_Products--5)以jerry的身份重新执行存储过程,成功--因为存储过程将以ProductsReader的权限上下文执行execute as user='jerry'select usergoexecute products.usp_Productsgorevert

本文来自博客,转载请标明出处:http://blog.csdn.net/cuoguo1111/archive/2006/11/29/1419515.aspx

相关资源:SQLServer中数据加密方法
转载请注明原文地址: https://www.6miu.com/read-3650229.html

最新回复(0)