共用方式為


DECRYPTBYKEY自動對稱鍵 (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

此函式會為加密資料解密。 它首先使用單獨的非對稱金鑰解密對稱金鑰,然後使用第一步中提取的對稱金鑰解密加密資料。

Transact-SQL 語法慣例

語法

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

引數

akey_ID

用來加密對稱金鑰之非對稱金鑰的識別碼。 akey_ID 具有 int 資料類型。

akey_password

保護非對稱金鑰的密碼。 如果資料庫主要金鑰 (DMK) 保護非對稱私密金鑰,則akey_password可以具有NULL值。 akey_password 具有 nvarchar 資料類型。

ciphertext

使用金鑰加密的資料。 ciphertext 具有 varbinary 資料類型。

@ciphertext

varbinary 類型的變數,其中包含以對稱金鑰加密的資料。

add_authenticator

指出原始加密程序是否隨純文字一同包含及加密驗證器。 必須符合資料加密程序期間傳遞給 ENCRYPTBYKEY 的值。 如果加密程序使用驗證器,則 add_authenticator 的值為 1。 add_authenticator 具有 int 資料類型。

@add_authenticator

指出原始加密程序是否隨純文字一同包含及加密驗證器的變數。 必須符合資料加密程序期間傳遞給 ENCRYPTBYKEY 的值。 @add_authenticator 具有 int 資料類型。

authenticator

作為驗證器產生基礎使用的資料。 必須符合提供給 ENCRYPTBYKEY 的值。 authenticator 具有 sysname 資料類型。

@authenticator

含有驗證器從中產生之資料的變數。 必須符合提供給 ENCRYPTBYKEY 的值。 @authenticator 具有 sysname 資料類型。

@add_authenticator

指出原始加密程序是否隨純文字一同包含及加密驗證器的變數。 必須符合資料加密程序期間傳遞給 ENCRYPTBYKEY 的值。 @add_authenticator 具有 int 資料類型。

authenticator

作為驗證器產生基礎使用的資料。 必須符合提供給 ENCRYPTBYKEY 的值。 authenticator 具有 sysname 資料類型。

@authenticator

含有驗證器從中產生之資料的變數。 必須符合提供給 ENCRYPTBYKEY 的值。 @authenticator 具有 sysname 資料類型。

傳回類型

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

備註

DECRYPTBYKEYAUTOASYMKEY 會結合 OPEN SYMMETRIC KEYDECRYPTBYKEY 的功能。 在單一作業中,它會先將對稱金鑰解密,再使用該金鑰將加密文字解密。

權限

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

範例

此範例示範 DECRYPTBYKEYAUTOASYMKEY 如何簡化解密程式碼。 此程式碼應該在尚未有 DMK 的資料庫上 AdventureWorks2025 執行。 以強密碼取代 <password>

--Create the keys and certificate.
USE AdventureWorks2022;

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<password>';
OPEN MASTER KEY DECRYPTION BY PASSWORD = '<password>';

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