DecryptByKey (Transact-SQL)

Decrittografa i dati tramite una chiave simmetrica.

Icona di collegamento a un argomentoConvenzioni della sintassi Transact-SQL

Sintassi

DecryptByKey ( { 'ciphertext' | @ciphertext } 
              [ , add_authenticator 
                                , { authenticator | @authenticator } 
              ]
                          )

Argomenti

  • ciphertext
    Dati crittografati tramite la chiave. varbinary.
  • @ciphertext
    Variabile di tipo varbinary contenente dati crittografati tramite la chiave.
  • add_authenticator
    Indica se un autenticatore è stato crittografato insieme al testo normale. Deve corrispondere allo stesso valore passato a EncryptByKey durante la crittografia dei dati. int.
  • authenticator
    Dati da cui generare un autenticatore. Deve corrispondere al valore specificato per EncryptByKey. 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

DecryptByKey utilizza una chiave simmetrica. Tale chiave deve essere già aperta nel database. È possibile che siano presenti più chiavi aperte contemporaneamente. Non è necessario aprire la chiave prima di decrittografare il testo definito dall'argomento ciphertext.

La crittografia e decrittografia simmetriche sono operazioni relativamente veloci, ideali per operazioni basate su quantità elevate di dati.

Esempi

A. Decrittografia di dati tramite una chiave simmetrica

Nell'esempio seguente il testo definito dall'argomento ciphertext viene decrittografato tramite una chiave simmetrica.

-- First, open the symmetric key with which to decrypt the data.
OPEN SYMMETRIC KEY SSN_Key_01
   DECRYPTION BY CERTIFICATE HumanResources037;
GO

-- Now list the original ID, the encrypted ID, and the 
-- decrypted ciphertext. If the decryption worked, the original
-- and the decrypted ID will match.
SELECT NationalIDNumber, EncryptedNationalID 
    AS 'Encrypted ID Number',
    CONVERT(nvarchar, DecryptByKey(EncryptedNationalID)) 
    AS 'Decrypted ID Number'
    FROM HumanResources.Employee;
GO

B. Decrittografia di dati tramite una chiave simmetrica e un hash di autenticazione

Nell'esempio seguente vengono decrittografati i dati crittografati insieme a un autenticatore.

-- First, open the symmetric key with which to decrypt the data
OPEN SYMMETRIC KEY CreditCards_Key11
   DECRYPTION BY CERTIFICATE Sales09;
GO

-- Now list the original card number, the encrypted card number,
-- and the decrypted ciphertext. If the decryption worked, 
-- the original number will match the decrypted number.
SELECT CardNumber, CardNumber_Encrypted 
    AS 'Encrypted card number', CONVERT(nvarchar,
    DecryptByKey(CardNumber_Encrypted, 1 , 
    HashBytes('SHA1', CONVERT(varbinary, CreditCardID)))) 
    AS 'Decrypted card number' FROM Sales.CreditCard;
GO

Vedere anche

Riferimento

EncryptByKey (Transact-SQL)
CREATE SYMMETRIC KEY (Transact-SQL)
ALTER SYMMETRIC KEY (Transact-SQL)
DROP SYMMETRIC KEY (Transact-SQL)

Altre risorse

Gerarchia di crittografia
Scelta di un algoritmo di crittografia

Guida in linea e informazioni

Assistenza su SQL Server 2005