本文件已封存並已停止維護。

DecryptByKeyAutoAsymKey (Transact-SQL)

新增: 2006 年 4 月 14 日

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

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


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

akey_ID

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

akey_password

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

'ciphertext'

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

@ciphertext

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

add_authenticator

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

@add_authenticator

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

authenticator

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

@authenticator

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

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

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

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

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