[SQL SERVER][Security]瞭解透明資料加密 (TDE)#實做

[SQL SERVER][Security]瞭解透明資料加密 (TDE)#簡介

這裡我將簡單測試啟用TDE並執行資料庫備份後,備份檔案是否再也看不到使用者資料是以純文字來顯示。

 

啟用TDE

Create a Master Key


USE master;

CREATE MASTER KEY

ENCRYPTION BY PASSWORD = 'Password123';

GO



clip_image002

Create a Certificate


CREATE CERTIFICATE MyTDECert 
WITH SUBJECT = 'TDECertificate'
GO

image

Create a Database Encryption Key


USE master;
CREATE DATABASE DemoTDE
GO
USE DemoTDE;
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER CERTIFICATE MyTDECert
GO 
 

image

 

 

 

 

 

 

 

 

 

備份Master Key和憑證

image


--備份Master Key 
OPEN MASTER KEY DECRYPTION BY PASSWORD = 'Password123';
BACKUP MASTER KEY TO FILE = N'd:\expmasterkey' 
    ENCRYPTION BY PASSWORD = 'Password123';
GO




--備份憑證
BACKUP CERTIFICATE MyTDECert 
  TO FILE = N'D:\MyTDECert.cer'
        WITH PRIVATE KEY (
        FILE = N'D:\MyTDECert.pvk',
        ENCRYPTION BY 
        PASSWORD = 'Password123');

image

 

Enable TDE


ALTER DATABASE DemoTDE
SET ENCRYPTION ON
GO
SELECT [name], is_encrypted FROM sys.databases
GO

image

到這裡就算了完成了啟用TDE的全部過程,接下來我們簡單測試使用者資料是否以純文字來顯示。

 

建立測試Table並新增三筆資料


create table dbo.mytest
(
 c1 int,
 c2 nvarchar(10)
)
insert into dbo.mytest 
values
(1,'rico'),
(2,'ricoisme'),
(3,'iamrico')

image 


select * from dbo.mytest

image 

執行備份(啟用TDE):由於執行備份會先執行checkpoint,這時會將記憶體的資料寫入硬碟中。


Backup Database DemoTDE
To Disk=N'd:\DemoTDEFull.bak'

image

使用wordpad開啟DemoTDEFull.bak,並搜尋rico關鍵字資料

image

image

image (果然沒找到) 

接下來關閉TDE來看看是否會有什麼不同。

 

關閉TDE


ALTER DATABASE DemoTDE      
SET ENCRYPTION OFF 
GO 
SELECT [name], is_encrypted FROM sys.databases 
GO

image

先清空後再塞入資料


truncate table dbo.mytest 
insert into dbo.mytest 
values
(1,'rico'),
(2,'ricoisme'),
(3,'iamrico')
Go

執行備份(沒有啟用TDE)


Backup Database DemoTDE
To Disk=N'd:\DemoTDEFullwithoutTDE.bak'

image

使用wordpad開啟DemoTDEFullwithoutTDE.bak,並搜尋rico關鍵字資料

image

image

image (真的找到相關測試資料了)