DecryptByKeyAutoCert (Transact-SQL)
SQL Server 2005
新增:
2006 年 4 月 14 日
使用對稱金鑰解密,而對稱金鑰則會以憑證自動解密。
- cert_ID
-
這是用來保護對稱金鑰的憑證識別碼。 int。
- cert_password
-
這是保護憑證私密金鑰的密碼。如果私密金鑰受資料庫主要金鑰保護,則可以是 NULL。 varchar。
- 'ciphertext'
-
這是以金鑰加密的資料。 varbinary。
- @ciphertext
-
這是 varbinary 類型的變數,其中包含使用金鑰加密的資料。
- add_authenticator
-
指出驗證器是否要與純文字一起加密。必須是加密資料時傳遞至 EncryptByKey 的相同值。如果使用驗證器,則為 1。 int。
- @add_authenticator
-
指出驗證器是否要與純文字一起加密。必須是加密資料時傳遞至 EncryptByKey 的相同值。
- authenticator
-
這是要產生驗證器的資料。必須符合已提供給 EncryptByKey 的值。sysname。
- @authenticator
-
這是含有要產生驗證器之資料的變數。必須符合已提供給 EncryptByKey 的值。
下列範例顯示如何使用 DecryptByKeyAutoCert 來簡化執行解密的程式碼。此程式碼應該執行於新安裝的 AdventureWorks 資料庫副本。
--Create the keys and certificate.
USE AdventureWorks;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'mzkvdlk979438teag$$ds987yghn)(*&4fdg^';
OPEN MASTER KEY DECRYPTION BY PASSWORD = 'mzkvdlk979438teag$$ds987yghn)(*&4fdg^';
CREATE CERTIFICATE HumanResources037
WITH SUBJECT = 'Sammamish HR',
EXPIRY_DATE = '10/31/2009';
CREATE SYMMETRIC KEY SSN_Key_01 WITH ALGORITHM = DES
ENCRYPTION BY CERTIFICATE HumanResources037;
GO
----Add a column of encrypted data.
ALTER TABLE HumanResources.Employee
ADD EncryptedNationalIDNumber varbinary(128);
OPEN SYMMETRIC KEY SSN_Key_01
DECRYPTION BY CERTIFICATE HumanResources037 ;
UPDATE HumanResources.Employee
SET EncryptedNationalIDNumber
= EncryptByKey(Key_GUID('SSN_Key_01'), NationalIDNumber);
GO
--
--Close the key used to encrypt the data.
CLOSE SYMMETRIC KEY SSN_Key_01;
--
--There are two ways to decrypt the stored data.
--
--OPTION ONE, using DecryptByKey()
--1. Open the symmetric key
--2. Decrypt the data
--3. Close the symmetric key
OPEN SYMMETRIC KEY SSN_Key_01
DECRYPTION BY CERTIFICATE HumanResources037;
SELECT NationalIDNumber, EncryptedNationalIDNumber
AS 'Encrypted ID Number',
CONVERT(nvarchar, DecryptByKey(EncryptedNationalIDNumber))
AS 'Decrypted ID Number'
FROM HumanResources.Employee;
CLOSE SYMMETRIC KEY SSN_Key_01;
--
--OPTION TWO, using DecryptByKeyAutoCert()
SELECT NationalIDNumber, EncryptedNationalIDNumber
AS 'Encrypted ID Number',
CONVERT(nvarchar, DecryptByKeyAutoCert ( cert_ID('HumanResources037') , NULL ,EncryptedNationalIDNumber))
AS 'Decrypted ID Number'
FROM HumanResources.Employee;
社群新增項目
新增
顯示:

