SQL 2005 資料加密

2021-08-31 14:30:57 字數 1366 閱讀 9745

--1.建立資料庫主金鑰

use master key

gocreate master key

encrypyion by password = 'password'

go

--2.建立存放加密資料的表

create table dbo.sectable

(id int identity primary key,

[data] nvarchar(100))

go

--3.建立數字證書,數字證書的金鑰被資料庫主金鑰加密

create certificate tomcert

with

subject = 'tom certificate',

expiry_date = '2007-01-01'

--4.建立對稱金鑰,被用證書對其進行加密

create symmetric key sym_tom

with algorithm = desx encryption by certificate tomcrrt

--5.使用證書解開對稱金鑰,將資料使用對稱金鑰加密後儲存於表中,完成後關閉對稱金鑰

open symmetric key sym_tom decryption by certicate tomcert

insert into sectable (data) values(encryptbykey(key_guid('sym_tom'),n'tom1'))

insert into sectable (data) values(encryptbykey(key_guid('sym_tom'),n'tom2'))

insert into sectable (data) values(encryptbykey(key_guid('sym_tom'),n'tom3'))

close symmetric key sym_tom

--6.檢視被加密的資料內容

select * from dbo.sectable

--7.使用證書解開對稱金鑰,解密資料表中的資料,完成後關閉對稱金鑰

select id,cast(decryptbykey(data) as nvarchar) from dbo.sectable

close symmetric key sym_tom

SQL 2005加密資料方法

示例一,使用證書加密資料.建立測試資料表 create table tb id int identity 1,1 data varbinary 8000 go 建立證書一,該證書使用資料庫主金鑰來加密 create certificate cert demo1 with subject n cert...

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...