標籤:des blog http io os 使用 ar strong sp
---------------------------------------------------------------------------------------------------------------------------------------------------------
第一層:
服務主要金鑰、它在有需要的時候自動產生、比如說要加密資料庫主要金鑰的時候。而且不可以手工create。
然、
我們還是可以對其進行備份與還原的。
backup service master key to file = ‘E:\server_master_key.bak‘
encryption by password = ‘123456‘; -- 沒有with
go
-------------------------
restore service master key from file = ‘E:\server_master_key.bak‘
decryption by password = ‘123456‘; -- 沒有with
go
第二層:
資料庫主要金鑰、它要我們手工的create 、它用來加密 認證,對稱金鑰,非對稱金鑰。
create master key encryption by password = ‘123456‘;
go
-----------------------------
如前面所說的、資料庫主要金鑰是通過服務主要金鑰加密的! 那為什麼在create master key 中還要有
ecryption by password 這一項呢!這是因為SQL Server 提供兩種訪問方式
1、自動型,簡單的說就是SQL server 知道你要用了會去自動開啟 master key,然而它也有不好的
的地方就是它要與許可權關聯。就是說如果你的帳號沒有適當的許可權就打不開。
2、手動型,就是說你要手動的開啟與關閉。
--------------------------------------
A、去除service master key 加密
alter master key drop encryption by service master key;
go; -- 沒有with
---------------------------------------
B、增加 service master key 加密
open master key decryption by password = ‘1234567‘
alter master key add encryption by service master key;
close master key; -- 沒有with
go
------------------------------
然、資料庫主要金鑰也支援備份與還原
backup master key to file = ‘E:\master_key.bak‘
encryption by password = ‘123456‘; -- 沒有with
go
---------------------------
restore master key from file = ‘E:\master_key.bak‘
decryption by password = ‘123456‘
encryption by password = ‘1234567‘ -- 沒有with 但是一定要加encryption by password 這一項才可以正確的運行。
go
---------------------------
-----------總結:service master key 與 master key 的操作都不帶with-----------------
第二層:
非對稱式加密部分、它用來加密資料。
建立:
create asymmetric key asy_key_test
with
algorithm = rsa_512
encryption by password = ‘123456‘;
go
刪除:
drop asymmetric key asy_key_test;
go
加密:
insert into T(X,Y) values(1,ENCRYPTBYASYMKEY(ASYMKEY_ID(‘asy_key_test‘),‘this is the clear text‘));
go
解密:
select X,cast(
DECRYPTBYASYMKEY(ASYMKEY_ID(‘asy_key_test‘),Y,N‘abcdef‘) as varchar)
from T;
go
對於非對稱的密鑰、加密是用公開金鑰完成的,解密是用私密金鑰。
也就是說如果我們把密鑰的私密金鑰部分修改,解密時會出錯
alter asymmetric key asy_key_test
with
private key(encryption by password = ‘abcdef‘,decryption by password = ‘123456‘);
go -- 小心with 後面是小括弧!
-------------------------------------------------------------------------------------------------------------------
對稱式加密部分、
建立:
create symmetric key sym_key_test
with
algorithm = TRIPLE_DES
encryption by password = ‘123456‘;
-- encryption by asymmetric key asy_key_name;也是可以用
go
刪除:
drop symmetric key sym_key_test;
使用:
open symmetric key sym_key_test decryption by password = ‘123456‘
insert into T3(X,Y) values(1,ENCRYPTBYKEY(KEY_GUID(‘sym_key_test‘),‘this is the text!‘));
select cast(DECRYPTBYKEY(Y) as varchar)from T3;
close symmetric key sym_key_test;
go
--------------- 加密時還是要有key_guid 的,解密時就不用這麼多了--------------------------------------
認證部分:
認證有自己的公開金鑰與私密金鑰,還有到期時間,支援備份還還原就是說認證的可移植性強。
建立:
create certificate ctf_test
encryption by password = ‘123456‘
with
subject = ‘認證名:ctf_test‘,
start_date = ‘2014-10-16‘,
expiry_date = ‘2015-10-16‘
go
備份:
backup certificate ctf_test
to file = ‘E:\ctf_public.bak‘
with private key(file= ‘E:\ctf_private.bak‘,decryption by password= ‘123456‘,encryption by password= ‘123456‘);
go
還原:
create certificate ctf_test
from file = ‘E:\ctf_public.bak‘
with private key(file= ‘E:\ctf_private.bak‘,decryption by password= ‘123456‘,encryption by password= ‘123456‘);
go
管理憑證的私密金鑰:
刪除、
alter certificate ctf_test
remove private key;
增加、
alter certificate ctf_test
with private key
(file= ‘E:\ctf_private.bak‘,decryption by password= ‘123456‘,encryption by password= ‘123456‘);
go
使用、
create table T4(X int ,Y varbinary(max));
go
加密:
insert into T4(X,Y) values(1,ENCRYPTBYCERT(CERT_ID(‘ctf_test‘),‘this is the text‘));
go
解密:
select * ,cast(DECRYPTBYCERT(CERT_ID(‘ctf_test‘),Y,N‘123456‘) as varchar) as [解密文本]
from T4;
go
SQL Server 加密層級