Condividi tramite


DECRYPTBYKEYAUTOCERT (Transact-SQL)

Si applica a:SQL ServerIstanza gestita di SQL di Azure

Questa funzione decrittografa i dati con una chiave simmetrica. Tale chiave simmetrica esegue automaticamente la decrittografia con un certificato.

Convenzioni relative alla sintassi Transact-SQL

Sintassi

DECRYPTBYKEYAUTOCERT ( cert_ID , cert_password
    , { 'ciphertext' | @ciphertext }
  [ , { add_authenticator | @add_authenticator }
  [ , { authenticator | @authenticator } ] ] )

Argomenti

cert_ID

ID del certificato usato per proteggere la chiave simmetrica. cert_ID ha un tipo di dati int.

cert_password

Password usata per crittografare la chiave privata del certificato. Può avere un NULL valore se la chiave master del database protegge la chiave privata. cert_password ha un tipo di dati nvarchar.

'ciphertext'

Stringa di dati crittografata con la chiave. ciphertext ha un tipo dati varbinary.

@ciphertext

Variabile di tipo varbinary contenente dati crittografati con la chiave.

add_authenticator

Indica se il processo di crittografia originale includeva e crittografava un autenticatore insieme al testo non crittografato. Deve corrispondere al valore passato a ENCRYPTBYKEY durante il processo di crittografia dei dati. add_authenticator ha un valore pari a 1 se il processo di crittografia ha usato un autenticatore. add_authenticator ha un tipo di dati int.

@add_authenticator

Variabile che indica se il processo di crittografia originale includeva e crittografava un autenticatore insieme al testo non crittografato. Deve corrispondere al valore passato a ENCRYPTBYKEY durante il processo di crittografia dei dati. @add_authenticator ha un tipo di dati int.

authenticator

Dati usati come base per la generazione dell'autenticatore. Deve corrispondere al valore fornito a ENCRYPTBYKEY. authenticator ha un tipo di dati sysname.

@authenticator

Variabile contenente i dati dai quali derivare un autenticatore. Deve corrispondere al valore fornito a ENCRYPTBYKEY. @authenticator ha un tipo di dati sysname.

Tipi restituiti

varbinary con un valore massimo di 8.000 byte.

Osservazioni:

DECRYPTBYKEYAUTOCERT consente di combinare le funzionalità di OPEN SYMMETRIC KEY e DECRYPTBYKEY. In un'unica operazione consente prima di decrittografare una chiave simmetrica e quindi di usarla per la decrittografia del testo crittografato.

Autorizzazioni

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

Esempi

In questo esempio viene illustrato come DECRYPTBYKEYAUTOCERT consente di semplificare il codice di decrittografia. Questo codice deve essere eseguito in un AdventureWorks2025 database che non dispone già di una DMK. Sostituire <password> con una password complessa.

--Create the keys and certificate.
USE AdventureWorks2022;

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

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

CREATE CERTIFICATE HumanResources037
    WITH SUBJECT = 'Sammamish HR', EXPIRY_DATE = '10/31/2035';

CREATE SYMMETRIC KEY SSN_Key_01
    WITH ALGORITHM = AES_256
    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;