DecryptByKeyAutoCert (Transact-SQL)

新增: 2006 年 4 月 14 日

使用對稱金鑰解密,而對稱金鑰則會以憑證自動解密。

主題連結圖示Transact-SQL 語法慣例

語法

DecryptByKeyAutoCert 
        ( cert_ID , cert_password , { 'ciphertext' | @ciphertext }
       [ , { add_authenticator | @add_authenticator } 
         [ , { authenticator | @authenticator } ] 
       ]
        )

引數

  • cert_ID
    這是用來保護對稱金鑰的憑證識別碼。 int
  • cert_password
    這是保護憑證私密金鑰的密碼。如果私密金鑰受資料庫主要金鑰保護,則可以是 NULL。 varchar
  • 'ciphertext'
    這是以金鑰加密的資料。 varbinary
  • @ciphertext
    這是 varbinary 類型的變數,其中包含使用金鑰加密的資料。
  • add_authenticator
    指出驗證器是否要與純文字一起加密。必須是加密資料時傳遞至 EncryptByKey 的相同值。**如果使用驗證器,則為 1int
  • @add_authenticator
    指出驗證器是否要與純文字一起加密。必須是加密資料時傳遞至 EncryptByKey 的相同值。
  • authenticator
    這是要產生驗證器的資料。必須符合已提供給 EncryptByKey 的值。sysname
  • @authenticator
    這是含有要產生驗證器之資料的變數。必須符合已提供給 EncryptByKey 的值。

傳回類型

varbinary,大小上限為 8,000 位元組。

備註

DecryptByKeyAutoCert 結合 OPEN SYMMETRIC KEY 和 DecryptByKey 的功能。可以在單一作業中解密對稱金鑰並且使用該金鑰來解密加密文字。

權限

需要 public 角色中的成員資格。

範例

下列範例顯示如何使用 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;

請參閱

參考

OPEN SYMMETRIC KEY (Transact-SQL)
EncryptByKey (Transact-SQL)
DecryptByKey (Transact-SQL)

其他資源

加密階層

說明及資訊

取得 SQL Server 2005 協助