DECRYPTBYKEYAUTOASYMKEY (Transact-SQL)

Esegue la decrittografia tramite una chiave simmetrica decrittografata automaticamente utilizzando una chiave asimmetrica.

Icona di collegamento a un argomentoConvenzioni della sintassi Transact-SQL

Sintassi

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

Argomenti

  • akey_ID
    ID della chiave asimmetrica utilizzata per proteggere la chiave simmetrica. akey_ID è di tipo int.

  • akey_password
    Password che protegge la chiave privata della chiave asimmetrica. Può essere NULL se la chiave privata è protetta dalla chiave master del database. akey_password è di tipo varchar.

  • 'ciphertext'
    Dati crittografati con la chiave. ciphertext è di tipo varbinary.

  • @ciphertext
    Variabile di tipo varbinary contenente dati crittografati con la chiave.

  • add_authenticator
    Indica se un autenticatore è stato crittografato insieme al testo normale. Deve corrispondere al valore passato a EncryptByKey durante la crittografia dei dati. Ha valore 1 se è stato utilizzato un autenticatore. add_authenticator è di tipo int.

  • @add_authenticator
    Indica se un autenticatore è stato crittografato insieme al testo normale. Deve corrispondere al valore passato a EncryptByKey durante la crittografia dei dati.

  • authenticator
    Dati da cui generare un autenticatore. Deve corrispondere al valore specificato per EncryptByKey. authenticator è di tipo sysname.

  • @authenticator
    Variabile contenente i dati da cui generare un autenticatore. Deve corrispondere al valore specificato per EncryptByKey.

Tipi restituiti

varbinary con dimensioni massime pari a 8.000 byte.

Osservazioni

DecryptByKeyAutoAsymKey include le funzionalità di OPEN SYMMETRIC KEY e DecryptByKey. In un'unica operazione consente di decrittografare una chiave simmetrica e di utilizzarla per la decrittografia del testo.

Autorizzazioni

È richiesta l'autorizzazione VIEW DEFINITION per la chiave simmetrica e l'autorizzazione CONTROL per la chiave asimmetrica.

Esempi

Nell'esempio seguente viene illustrato come utilizzare DecryptByKeyAutoAsymKey per semplificare il codice che esegue la decrittografia. È consigliabile eseguire questo codice in una copia appena installata del database 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