在网络上发表报表,处理数据都会发生一些严重的安全问题。网络最大的担忧在于有人可以拦截通信信号并捕获数据。即使通信只限于局域网内,仍然有公司内部的人可以拦截高层管理者阅读的报表。SQL server 2005中支持采用证书的方式来加密数据

1.创建证书
create master key encryption by password = 'administrator123'
--用密码administrator123 在master数据上创建证书C1--
create certificate c1
encryption by password = 'administrator123'
with subject = 'c1 certificate',
start_date='03/08/2009',
expiry_date = '02/08/2020';
go
 
2.创建测试表,name字段要加密的列,数据类型为 varbinary
create table testB(id int identity(1,1),name varbinary(5000))

3.使用加密函数向测试表中写入一条测试数据
insert into testB(name)
select encryptbycert(cert_id('c1'),'test')

4.利用以下语句来提取加密的数据
select * from testB

5.利用以下语句来解密数据
select id ,cast(decryptbycert(cert_id('c1'),name ,
N'administrator123')as varchar(20)) from testB
 
 
  说明:DecryptByCert函数的返回类型为varbinary,所以要使用cast函数将二进制转换为原始类型
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />