------SQL数据库加密-------------

转贴自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_keys
 
4)备份数据库主密钥
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_myCert
encryption by password='P@ssw0rd'
with subject='Self Signed Cert',
start_date='1/31/2006'
expiry_date='1/31/2008'
 
 
非对称密钥
创建新的密钥对
create asymmetric key asy_Key1
with algorithm=RSA_2048
encryption by password='P@ssw0rd'
 
 
对称密钥
创建新的密钥对
create symmetric key SymKeyMarketing3
with algorithm=AES_2048
encryption by certificate asy_Key1
 
使用对称密钥
使用前必须打开
open symmetric SymKeyMarketing3
decryption by certificate asy_Key1
 
sys.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 key
to file='g:/testdbkey.bak'
encryption by password='p@ssw0rd'
 
--5)删除服务主密钥对数据库主密钥的保护
--创建非对称密钥成功,自动使用服务主密钥解密并使用该数据库主密钥
create asymmetric key asy_Testkey1 with algorithm=RSA_1024
go
--删除服务主密钥对数据库主密钥的保护
alter master key
drop encryption by service master key
go
 
--查看数据库加密状态
select [name],is_master_key_encrypted_by_server
from sys.databases where name='TestDB'
 
--创建非对称密钥失败,因为数据库主密钥未打开
create asymmetric key asy_Testkey2 with algorithm=RSA_1024
go
--打开数据库主密钥
open master key decryption by password='p@ssw0rd'
select * from sys.openkeys
go
--创建非对称密钥成功
create asymmetric key asy_Testkey2 with algorithm=RSA_1024
go
 
--恢复服务主密钥对数据库主密钥的保护
alter master key
add encryption by service master key
close master key
go
 
--*****(3)证书
--1)创建自签名证书
create certificate cert_Testcert
encryption by password='p@ssw0rd'
with subject='TestCert1',
start_date='1/31/2006',
expiry_date='1/31/2008'
go
select * from sys.certificates
 
 
--2)从文件导入证书
Create certificate cert_TestCert2
From file=’g:/MSCert.cer’
Go
 
-- 3)备份导出证书和密钥
backup certificate cert_Testcert
to 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_TestCert
remove private key
go
 
--加密成功
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_Test
from file='g:/asy_Test.key'
encryption by password='p@ssw0rd'
go
 
select * 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_Salary
with algorithm=AES_192
encryption by password='p@ssw0rd'
go
 
select * from sys.symmetric_keys where [name]='sym_Salary'
 
------****(2)加密列数据
--1)打开对称密钥
open symmetric key sym_Salary
decryption by password='p@ssw0rd'
go
select * 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_Salary
go
select * 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_Salary
go
 
--(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 empsalary
close 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_Product
with 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]
go
create user [SPDeveloper] for login SPDeveloper with default_schema=[SPDeveloper]
go
create schema products authorization SPDeveloper
go
exec sp_addrolemember @rolename='db_owner',@membername='SPDeveloper'
 
--4)以SPDeveloper的身份创建存储过程products.usp_Products
execute as user='SPDeveloper'
go
create procedure products.usp_Products
as
select * from dbo.Products
go
 
revert
select user
 
--5)创建普通用户jerry
create login [jerry] with password='p@ssw0rd',default_database=[TestDB]
go
create user [jerry] for login jerry
go
 
--*******(2)使用证书签署存储过程
--1)授予用户jerry执行存储过程的权限
grant execute on products.usp_Products to jerry
 
--2)以jerry的身份执行存储过程失败,因为拥有权链的断裂的
execute as user='jerry'
select user
go
 
execute products.usp_Products
go
revert
 
--3)使用证书在当前数据库创建用户ProductsReader,并为该用户赋予读取products表的权限
create user ProductsReader for certificate cert_Products
go
grant select on products To ProductsReader
 
--4)使用证书签署当前存储过程
add signature to products.usp_Products by certificate cert_Products
 
--5)以jerry的身份重新执行存储过程,成功
--因为存储过程将以ProductsReader的权限上下文执行
execute as user='jerry'
select user
go
 
execute products.usp_Products
go
revert


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

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
kh-mod是基于MG2 0.5.1的一款免费的php开发的相册管理程序。相比MG2的简洁,kh_mod的功能更强大一些。本程序由dudumao和CU Klemens共同编写完成。 kh-mod 0.4.2 简体中文版 更新内容: - 修复: 一些关于sql(MySQL) "传统" 模式的问题 - MySQL接口MySQLi支持PHP 5.4及以上。如果在用一个低版本的PHP,新版本的接口一样可以使用 - 增加"每日统计" (选项|统计|点击统计)管理 - 控制面板增加“最新导入项目”系统目录,该目录包含所有最近上传和最近导入的文件 - 增加了"强制缩略图至设定值"选项, 如果输入同样的高度和宽度(选项|设置),现在将会生成方形缩略图 - Flowplayer的版本升级至3.2.16 - JSColor的版本升级至1.4.1 - Colorbox的版本升级至1.4.7(最新版本支持IE6) - 旋转图片的内存占用减少到30% - "mg2admin_convert.php"更名为"mg2admin_converter.php",同时也对代码进行了重写 - 如果数据库从平面文件切换到MySQL或者切换回去,现在会显示MySQL的计数器和关键词的记录数值。在早期的版本,所有文件(图片、视频或者音频)的数据仅作为一个数据记录。 由于这个原因,现在记录的数值要比过去的版本高。 - 在管理员邮箱中关于最新评论的通知中将会增加站点页面编码,尽可能避免邮件中的文本显示错误, 除此之外, 移除任何主题栏的标签 - 基于WebKit核心浏览器(Google Chrome, Safari等),修复控制面板界面的一些显示问题 - 一些小修复 - 更新语言包 特点: 无需数据库即可使用,也可以在后台切换至Mysql数据库 HTTP方式在线上传,FTP/SSH上传根目录导入 相册无限级向下分级,自由拖拽排序 相册缩略图自动生成,相册封面固定/随机显示图片 代码架构合理,极速明快,执行效率高 图像/相册支持隐藏、加密以及定时发布等 相册支持jpeg, jpg, gif, png, flv, mov, mp4, mp3等媒体文件发布 支持图片评论,评论信息通过Email通知管理员 可启用伪静态,易于SEO优化,便于外链地址使用 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值