The Wayback Machine - http://web.archive.org/web/20140105222829/http://msdn.microsoft.com:80/zh-tw/library/ms182559(v=sql.90).aspx
匯出 (0) 列印
全部展開
Expand Minimize
本主題尚未接受評分 - 為這個主題評分

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 的相同值。如果使用驗證器,則為 1。 int。

@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;
本文對您有任何幫助嗎?
(剩餘 1500 個字元)

社群新增項目

新增
顯示:
© 2014 Microsoft. 著作權所有,並保留一切權利。