SQLServer 2008中的代碼平安(七) 證書加密。本站提示廣大學習愛好者:(SQLServer 2008中的代碼平安(七) 證書加密)文章只能為提供參考,不一定能成為您想要的結果。以下是SQLServer 2008中的代碼平安(七) 證書加密正文
留意:證書和非對稱密鑰異樣的消費資本。
我們看一組例子:
示例1、創立數據庫證書
創立數據庫證書:CREATE SYMMETRIC KEY (http://msdn.microsoft.com/en-us/library/ms187798.aspx)
USE DB_Encrypt_Demo
GO
--創立證書
CREATE CERTIFICATE cert_Demo --證書稱號
ENCRYPTION BY PASSWORD = 'asdfG!!!' --加密證書的暗碼
WITH SUBJECT = 'DB_Encrypt_Demo Database Encryption Certificate',--證書主題
START_DATE = '3/14/2011', EXPIRY_DATE = '10/20/2012'--起止日期
GO
示例2、檢查數據庫中的證書
應用目次視圖sys.certificates(http://msdn.microsoft.com/en-us/library/ms189774.aspx)來檢查。
--檢查以後數據庫中的證書
use DB_Encrypt_Demo
go
--檢查證書
SELECT name, pvt_key_encryption_type_desc, issuer_name
FROM sys.certificates
----成果前往
/*
name pvt_key_encryption_type_desc issuer_name
cert_Demo ENCRYPTED_BY_PASSWORD DB_Encrypt_Demo Database Encryption Certificate
*/
示例3、備份和復原證書
創立證書後,也能夠應用BACKUP CERTIFICATE(http://msdn.microsoft.com/en-us/library/ms178578.aspx)敕令備份到文件,為了平安地保留或在其他數據庫中復原它。
--備份證書
BACKUP CERTIFICATE cert_Demo
TO FILE = 'H:\SqlBackup\certDemo.BAK'--證書備份途徑,用來加密
WITH PRIVATE KEY (FILE='H:\SqlBackup\certDemoPK.BAK',--證書私鑰文件途徑,用來解密
ENCRYPTION BY PASSWORD = '1234GH!!!',--加密私鑰暗碼
DECRYPTION BY PASSWORD = 'asdfG!!!' )--解密私鑰暗碼
--備份後,可以在其他數據庫中應用這個證書,或應用DROP CERTIFICATE敕令刪除它。
DROP CERTIFICATE cert_Demo
GO
--從備份文件中復原證書到數據庫中
CREATE CERTIFICATE cert_Demo
FROM FILE = 'H:\SqlBackup\certDemo.BAK'
WITH PRIVATE KEY (FILE = 'H:\SqlBackup\certDemoPK.BAK',
DECRYPTION BY PASSWORD = '1234GH!!!' ,--解密私鑰暗碼
ENCRYPTION BY PASSWORD = 'asdfG!!!')--加密私鑰暗碼
示例4、治理證書的私鑰
應用ALTER CERTIFICATE( http://msdn.microsoft.com/en-us/library/ms189511.aspx)敕令為證書增長或刪除私鑰。這個敕令許可刪除私鑰(默許經由過程數據庫主密鑰時行加密)、增長私鑰或修正私鑰的暗碼。
--從證書中刪除私鑰
ALTER CERTIFICATE cert_Demo
REMOVE PRIVATE KEY
--從備份文件為既有證書從新增長私鑰
ALTER CERTIFICATE cert_Demo
WITH PRIVATE KEY
(FILE = 'H:\SqlBackup\certDemoPK.BAK',
DECRYPTION BY PASSWORD = '1234GH!!!' ,--解密私鑰暗碼
ENCRYPTION BY PASSWORD = 'asdfG!!!')--加密私鑰暗碼
--修正既有私鑰的暗碼
ALTER CERTIFICATE cert_Demo
WITH PRIVATE KEY (DECRYPTION BY PASSWORD = 'asdfG!!!',
ENCRYPTION BY PASSWORD = 'mynewpassword!!!13E')
示例5、應用證書加密息爭密。
應用函數EncryptByCert加密數據。(http://msdn.microsoft.com/zh-cn/library/ms174361.aspx)
--從證書中刪除私鑰
ALTER CERTIFICATE cert_Demo
REMOVE PRIVATE KEY
--從備份文件為既有證書從新增長私鑰
ALTER CERTIFICATE cert_Demo
WITH PRIVATE KEY
(FILE = 'H:\SqlBackup\certDemoPK.BAK',
DECRYPTION BY PASSWORD = '1234GH!!!' ,--解密私鑰暗碼
ENCRYPTION BY PASSWORD = 'asdfG!!!')--加密私鑰暗碼
--修正既有私鑰的暗碼
ALTER CERTIFICATE cert_Demo
WITH PRIVATE KEY (DECRYPTION BY PASSWORD = 'asdfG!!!',
ENCRYPTION BY PASSWORD = 'mynewpassword!!!13E')
上面是一個例子:
USE DB_Encrypt_Demo
GO
--拔出測試數據
INSERT dbo.PWDQuestion
(CustomerID, PasswordHintQuestion, PasswordHintAnswer)
VALUES
(10, '您出身的病院稱號?',
EncryptByCert(Cert_ID('cert_Demo'), '北京四合院家中'))
--檢查明文
SELECT CAST(PasswordHintAnswer as varchar(200)) PasswordHintAnswer
FROM dbo.PWDQuestion
WHERE CustomerID = 10
--檢查原文 [email protected]
SELECT PasswordHintQuestion,
CAST(DecryptByCert(Cert_ID('cert_Demo'),PasswordHintAnswer,
N'mynewpassword!!!13E')
as varchar(200)) PasswordHintAnswer
FROM dbo.PWDQuestion WHERE CustomerID = 10
示例6、應用對稱密鑰對數據停止加密息爭密。
在後面的文章中,你曾經看到翻開用非對稱密鑰加密的對稱密鑰的演示,它分兩個步調,起首用OPEN SYMMETRIC KEY敕令,然後是現實的DecryptByKey函數挪用。SQL Server也供給了可以或許將這兩個步調合二為一的額定的解密函數:DecryptByKeyAutoAsymKey(http://msdn.microsoft.com/en-us/library/ms365420.aspx)和DecryptByKeyAutoCert(http://msdn.microsoft.com/en-us/library/ms182559.aspx)
USE DB_Encrypt_Demo
GO
--本例應用數據庫主暗碼加密,因此不須要暗碼。[email protected]
----Create master Key Encryption By password='123ASD!'
----go
--創立非對稱密鑰 [email protected]
CREATE ASYMMETRIC KEY asymDemo_V2
WITH ALGORITHM = RSA_512
--創立對稱密鑰 [email protected]
CREATE SYMMETRIC KEY sym_Demo_V2
WITH ALGORITHM = TRIPLE_DES
ENCRYPTION BY ASYMMETRIC KEY asymDemo_V2
--翻開對稱密鑰,拔出記載
OPEN SYMMETRIC KEY sym_Demo_V2
DECRYPTION BY ASYMMETRIC KEY asymDemo_V2
INSERT dbo.PWDQuestion
(CustomerID, PasswordHintQuestion, PasswordHintAnswer)
VALUES
(22, '您出身的病院稱號?',
EncryptByKey(Key_GUID('sym_Demo_V2'), '邵逸夫病院'))
CLOSE SYMMETRIC KEY sym_Demo_V2
此時,應用DecryptByKeyAutoAsymKey解密數據,只須要一個操作
SELECT CAST(DecryptByKeyAutoAsymKey(ASYMKEY_ID('asymDemo_V2'),NULL,
PasswordHintAnswer) as varchar)
FROM dbo.PWDQuestion
WHERE CustomerID = 22
小結:
1、本文重要引見證書的創立、刪除、檢查和用它來修正加密方法、停止數據的加密息爭密。
2、證書加密和非對稱密鑰加密絕對對稱密鑰加密更加消費資本。
下文將重要引見SQL Server中最為使人鼓舞的通明數據加密(TDE)