본문 바로가기
MS-SQL

[MS-SQL] DB 암호화 알고리즘 사용법 (AES 알고리즘)

by 김뽀씨 2015. 7. 17.

음...MS-SQL에서 제공하는 암호화 알고리즘.

오랫만에 다시 할려니 기억이..으하하하


--******************************************
-- 기존 키 삭제
--******************************************
drop symmetric key keyTest
drop certificate cert4EncKey
drop master key

--******************************************
-- Master Key 생성,백업 
--******************************************

create master key encryption by password = 'testpassword'
backup master key to file='C:\MasterKey.keyBak' encryption by password='testpassword'
backup service master key to file='C:\serviceMasterKey.keyBak' encryption by password='testpassword'

 

--******************************************
-- 인증서 생성 , 백업
--******************************************
create certificate cert4EncKey with subject='암호화 대칭키를 위한 인증서', start_date='2012-04-14', expiry_date='2999-12-31'
backup Certificate cert4EncKey to file='c:\cert.keyBak'
  with private key (file='c:\cert_pri.keyBak', encryption by password='testpassword')


--******************************************
-- 암복호에서 실제 사용될 대칭키 생성 (identity와 key_source 항목을 정의해야 재생성시 키값 유지 가능)
--******************************************
create symmetric key keyTest with  ALGORITHM=AES_128
                                  ,Identity_value = 'Data Encrypt 20120414'
                                  ,Key_source     = 'Test Database Security'
                             encryption by certificate cert4EncKey


--***************************************************************
-- 복원시
--***************************************************************
--1) 마스터키 재생 (이떄. 마스터 키는 백업 되어 있어야함)
RESTORE MASTER KEY
    FROM FILE = 'C:\MasterKey.keyBak'
    DECRYPTION BY PASSWORD = 'testpassword' 
    ENCRYPTION BY PASSWORD = 'testpassword';
GO

--2) 기존 대칭키랑 인증서 삭제 (대칭키, 인증서 순으로 삭제)
DROP SYMMETRIC KEY keyTest;
DROP CERTIFICATE cert4EncKey;

--3) 마스터키 오픈 후 인증서, 대칭키 재생성