DECRYPTBYKEYAUTOCERT (Transact-SQL)

Descifra utilizando una clave simétrica que se descifra automáticamente con un certificado.

Icono de vínculo a temasConvenciones de sintaxis de Transact-SQL

Sintaxis

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

Argumentos

  • cert_ID
    Es el identificador del certificado que se usa para proteger la clave simétrica. cert_ID es int.

  • cert_password
    Contraseña que protege la clave privada del certificado. Puede ser NULL si la clave privada está protegida por la clave maestra de la base de datos. cert_password es varchar.

  • 'ciphertext'
    Son los datos que se cifraron con la clave. ciphertext es varbinary.

  • @ciphertext
    Es una variable de tipo varbinary que contiene los datos que se han cifrado con la clave.

  • add_authenticator
    Indica si se ha cifrado un autenticador junto con el texto simple. Debe ser el mismo valor pasado a EncryptByKey cuando se cifraron los datos.Es 1 si se ha utilizado un autenticador. add_authenticator es int.

  • @add_authenticator
    Indica si se ha cifrado un autenticador junto con el texto simple. Debe ser el mismo valor pasado a EncryptByKey cuando se cifraron los datos.

  • authenticator
    Son los datos a partir de los cuales se generará un autenticador. Debe coincidir con el valor que se proporcionó a EncryptByKey. authenticator es sysname.

  • @authenticator
    Es una variable que contiene los datos a partir de los cuales se generará un autenticador. Debe coincidir con el valor que se proporcionó para EncryptByKey.

Tipos de valor devueltos

varbinary con un tamaño máximo de 8.000 bytes.

Notas

DecryptByKeyAutoCert combina la funcionalidad de OPEN SYMMETRIC KEY con la de DecryptByKey. En una sola operación descifra una clave simétrica y utiliza esa clave para descifrar texto cifrado.

Permisos

Se requiere el permiso VIEW DEFINITION sobre la clave simétrica y el permiso CONTROL sobre el certificado.

Ejemplos

En el ejemplo siguiente se muestra cómo se puede utilizar DecryptByKeyAutoCert para simplificar código de descifrado. Este código debe ejecutarse en una copia recién instalada de la base de datos 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;