DECRYPTBYKEYAUTOASYMKEY (Transact-SQL)

利用使用非對稱金鑰自動解密的對稱金鑰來解密。

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

語法

DecryptByKeyAutoAsymKey ( akey_ID , akey_password 
        , { 'ciphertext' | @ciphertext }
    [ , { add_authenticator | @add_authenticator } 
    [ , { authenticator | @authenticator } ] ] )

引數

  • akey_ID
    這是用來保護對稱金鑰之非對稱金鑰的識別碼。akey_ID 是 int。

  • akey_password
    這是保護非對稱金鑰之私密金鑰的密碼。如果私密金鑰受資料庫主要金鑰保護,則可以是 NULL。akey_password 是 varchar。

  • 'ciphertext'
    這是以金鑰加密的資料。ciphertext 是 varbinary。

  • @ciphertext
    這是 varbinary 類型的變數,其中包含使用金鑰加密的資料。

  • add_authenticator
    指出驗證器是否要與純文字一起加密。必須是加密資料時傳遞至 EncryptByKey 的相同值。如果使用驗證器,則為 1。add_authenticator 是 int。

  • @add_authenticator
    指出驗證器是否要與純文字一起加密。必須是加密資料時傳遞至 EncryptByKey 的相同值。

  • authenticator
    這是要產生驗證器的資料。必須符合已提供給 EncryptByKey 的值。authenticator 是 sysname。

  • @authenticator
    這是含有要產生驗證器之資料的變數。必須符合已提供給 EncryptByKey 的值。

傳回類型

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

備註

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

權限

需要對稱金鑰的 VIEW DEFINITION 權限和非對稱金鑰的 CONTROL 權限。

範例

下列範例顯示如何使用 DecryptByKeyAutoAsymKey 來簡化執行解密的程式碼。此程式碼應該執行於新安裝的 AdventureWorks 資料庫副本。

--Create the keys and certificate.
USE AdventureWorks;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'mzkvdMlk979438teag$$ds987yghn)(*&4fdg^';
OPEN MASTER KEY DECRYPTION BY PASSWORD = 'mzkvdMlk979438teag$$ds987yghn)(*&4fdg^';
CREATE ASYMMETRIC KEY SSN_AKey 
    WITH ALGORITHM = RSA_2048 ; 
GO
CREATE SYMMETRIC KEY SSN_Key_02 WITH ALGORITHM = DES
    ENCRYPTION BY ASYMMETRIC KEY SSN_AKey;
GO
--
--Add a column of encrypted data.
ALTER TABLE HumanResources.Employee
    ADD EncryptedNationalIDNumber2 varbinary(128); 
OPEN SYMMETRIC KEY SSN_Key_02
   DECRYPTION BY ASYMMETRIC KEY SSN_AKey;
UPDATE HumanResources.Employee
SET EncryptedNationalIDNumber2
    = EncryptByKey(Key_GUID('SSN_Key_02'), NationalIDNumber);
GO
--Close the key used to encrypt the data.
CLOSE SYMMETRIC KEY SSN_Key_02;
--
--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_02
   DECRYPTION BY ASYMMETRIC KEY SSN_AKey;
SELECT NationalIDNumber, EncryptedNationalIDNumber2  
    AS 'Encrypted ID Number',
    CONVERT(nvarchar, DecryptByKey(EncryptedNationalIDNumber2)) 
    AS 'Decrypted ID Number'
    FROM HumanResources.Employee;
CLOSE SYMMETRIC KEY SSN_Key_02;
--
--OPTION TWO, using DecryptByKeyAutoAsymKey()
SELECT NationalIDNumber, EncryptedNationalIDNumber2 
    AS 'Encrypted ID Number',
    CONVERT(nvarchar, DecryptByKeyAutoAsymKey ( AsymKey_ID('SSN_AKey') , NULL ,EncryptedNationalIDNumber2)) 
    AS 'Decrypted ID Number'
    FROM HumanResources.Employee;
GO