DECRYPTBYKEYAUTOCERT (Transact-SQL)
Esegue la decrittografia tramite una chiave simmetrica decrittografata automaticamente con un certificato.
Si applica a: SQL Server (da SQL Server 2008 a versione corrente). |
Convenzioni della sintassi Transact-SQL
Sintassi
DecryptByKeyAutoCert ( cert_ID , cert_password
, { 'ciphertext' | @ciphertext }
[ , { add_authenticator | @add_authenticator }
[ , { authenticator | @authenticator } ] ] )
Argomenti
cert_ID
ID del certificato utilizzato per proteggere la chiave simmetrica. cert_ID è di tipo int.cert_password
Password che protegge la chiave privata del certificato. Può essere NULL se la chiave privata è protetta dalla chiave master del database. cert_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.è 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
DecryptByKeyAutoCert 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 il certificato.
Esempi
Nell'esempio seguente viene illustrato come utilizzare DecryptByKeyAutoCert per semplificare il codice che esegue la decrittografia. Questo codice deve essere eseguito in un database AdventureWorks2012 per cui non è presente già una chiave master.
--Create the keys and certificate.
USE AdventureWorks2012;
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;
Vedere anche
Riferimento
OPEN SYMMETRIC KEY (Transact-SQL)