在网络上发送数据都会发生一些严重的安全问题,网络最大的担忧在于有人可以捕获数据,sqlserver支持采用证书来加密

1.创建证书

create master key encryption by

password = 'administrator123'

--用密码admini--

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

注意:加密的类型必须是varbinary,因为加密的数据是类型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