代码如下:
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
示例二、查看数据库中的证书
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)使用目录视图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
*/
示例三、备份和还原证书
创建证书后,也可以使用BACKUP CERTIFICATE(http://msdn.microsoft.com/en-us/library/ms178578.aspx)命令备份到文件,为了安全地保存或在其他数据库中还原它。
代码如下:
--备份证书
BACKUP CERTIFICATE cert_Demo
TO FILE = 'H:SqlBackupcertDemo.BAK'--证书备份路径,用来加密
WITH PRIVATE KEY (FILE='H:SqlBackupcertDemoPK.BAK',--证书私钥文件路径,用来解密
ENCRYPTION BY PASSWORD = '1234GH!!!',--加密私钥密码
DECRYPTION BY PASSWORD = 'asdfG!!!' )--解密私钥密码
--备份后,可以在其他数据库中使用这个证书,或使用DROP CERTIFICATE命令删除它。
DROP CERTIFICATE cert_Demo
GO
--从备份文件中还原证书到数据库中
CREATE CERTIFICATE cert_Demo
FROM FILE = 'H:SqlBackupcertDemo.BAK'
WITH PRIVATE KEY (FILE = 'H:SqlBackupcertDemoPK.BAK',
DECRYPTION BY PASSWORD = '1234GH!!!' ,--解密私钥密码
ENCRYPTION BY PASSWORD = 'asdfG!!!')--加密私钥密码
示例四、管理证书的私钥
使用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:SqlBackupcertDemoPK.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')
示例五、使用证书加密和解密。
使用函数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:SqlBackupcertDemoPK.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
代码如下:
--查看原文 3w@live.cn
SELECT PasswordHintQuestion,
CAST(DecryptByCert(Cert_ID('cert_Demo'),PasswordHintAnswer,
N'mynewpassword!!!13E')
as varchar(200)) PasswordHintAnswer
FROM dbo.PWDQuestion WHERE CustomerID = 10
示例六、使用对称密钥对数据进行加密和解密。
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)在前面的文章中,你已经看到打开用非对称密钥加密的对称密钥的演示,它分两个步骤,首先用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
--本例使用数据库主密码加密,因而不需要密码。3w@live.cn
----Create master Key Encryption By password='123ASD!'
----go
--创建非对称密钥 3w@live.cn
CREATE ASYMMETRIC KEY asymDemo_V2
WITH ALGORITHM = RSA_512
--创建对称密钥 3w@live.cn
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)