SQL 2005加密資料方法

2021-06-16 05:54:24 字數 3444 閱讀 8715

--示例一,使用證書加密資料. 

--建立測試資料表 

create table tb(id int identity(1,1),data varbinary(8000)); 

go --建立證書一,該證書使用資料庫主金鑰來加密 

create certificate cert_demo1  

with  

subject=n'cert1 encryption by database master key', 

start_date='2008-01-01', 

expiry_date='2008-12-31' 

go --建立證書二,該證書使用密碼來加密 

create certificate cert_demo2 

encryption by password='liangck.123' 

with  

subject=n'cert1 encrption by password', 

start_date='2008-01-01', 

expiry_date='2008-12-31' 

go --此時,兩個證書已經建立完,現在可以用這兩個證書來對資料加密 

--在對錶tb做insert時,使用encryptbycert加密 

insert tb(data) 

select encryptbycert(cert_id(n'cert_demo1'),n'這是證書1加密的內容-liangck');  --使用證書1加密 

insert tb(data) 

select encryptbycert(cert_id(n'cert_demo2'),n'這是證書2加密的內容-liangck');  --使用證書2加密 

--ok.現在已經對資料加密保證了.現在我們select看看 

select * from tb ; 

--現在對內容進行解密顯示. 

--解密時,使用decryptbycert 

select 證書1解密=convert(nvarchar(50),decryptbycert(cert_id(n'cert_demo1'),data)), 

--使用證書2解密時,要指定decryptbycert的第三個引數, 

--因為在建立時,指定了encryption by password. 

--所以這裡要通過這個密碼來解密.否則解密失敗 

證書2解密

=convert(nvarchar(50),decryptbycert(cert_id(n'cert_demo2'),data,n'liangck.123')) 

from tb ; 

--我們可以看到,因為第2條記錄是證書2加密的.所以使用證書1將無法解密.所以返回null 

證書1解密                                              證書2解密 

這是證書1加密的內容-liangck                                 null 

null                                               這是證書2加密的內容-liangck 

(2 行受影響) 

go --刪除測試證書與資料表 

drop certificate cert_demo1; 

drop certificate cert_demo2; 

drop table tb; 

go --示例二,使用對稱金鑰加密資料, 

--對稱金鑰又使用證書來加密. 

--建立測試資料表tb 

create table tb(id int identity(1,1),data varbinary(8000)); 

go --建立證書,該證書用於加密對稱金鑰. 

create certificate cert_demo 

encryption by password=n'liangck.123' 

with 

subject=n'cert encryption by password', 

start_date='2008-01-01', 

expiry_date='2008-12-31' 

go --建立對稱金鑰 

create symmetric key sym_demo 

with 

algorithm=des  --使用des加密演算法 

encryption by certificate cert_demo --使用cert_demo證書加密 

go  

--要使用sym_demo對稱金鑰.必需使用open symmetric key來開啟它 

open symmetric key sym_demo 

decryption by certificate cert_demo 

with password=n'liangck.123' 

--插入加密資料 

insert tb(data) 

select encryptbykey(key_guid(n'sym_demo'),n'這是加密的資料,能顯示出來嗎?') 

--關閉金鑰 

close symmetric key sym_demo 

--插入完加密資料,現在使用select來查詢一下資料 

select * from tb  

go --現在來解密此資料 

--同樣,還是要先開啟對稱金鑰 

open symmetric key sym_demo 

decryption by certificate cert_demo 

with password=n'liangck.123' 

select convert(nvarchar(50),decryptbykey(data)) --這裡可見,資料已經解密出來了. 

from tb  

close symmetric key sym_demo 

go --刪除測試 

drop symmetric key sym_demo 

drop certificate cert_demo 

drop table tb 

--示例三,還有一種方法加密資料更簡單 

--就是使用encryptbypassphrase 

--建立測試資料表tb 

create table tb(id int identity(1,1),data varbinary(8000)); 

go insert tb(data) 

select encryptbypassphrase(n'這是密碼,用來加密的',n'這是要加密的內容'); 

--解密 

select convert(nvarchar(50),decryptbypassphrase(n'這是密碼,用來加密的',data)) 

from tb  

go drop table tb

SQL 2005 資料加密

1.建立資料庫主金鑰 use master key gocreate master key encrypyion by password password go 2.建立存放加密資料的表 create table dbo.sectable id int identity primary key,da...

MSSQL加密資料表 sql2005

建立資料庫主秘鑰 use adb go create master key encryption by password p8ssw0rd go 建立存放加密資料的表 create table dbo.sectable id int identity primary key,data nvarcha...

sql 2005 迴圈處理資料

對oracle而言用游標很容易就可以實現,也不會太耗費資源 相對來說 而sql用游標確相當的慢,以下用row number實現的乙個迴圈處理 declare date datetime,maxid int id int begin select maxid count from nt fundchi...