KEY_NAME (Transact-SQL)

Restituisce il nome della chiave simmetrica dal GUID di una chiave simmetrica o da un testo crittografato.

Icona di collegamento a un argomentoConvenzioni della sintassi Transact-SQL

Sintassi

KEY_NAME ( ciphertext | key_guid ) 

Argomenti

  • ciphertext
    Testo crittografato dalla chiave simmetrica. cyphertext è di tipo varbinary(8000).

  • key_guid
    GUID della chiave simmetrica. key_guid è di tipo uniqueidentifier.

Tipi restituiti

varchar(128)

Autorizzazioni

A partire da SQL Server 2005 la visibilità dei metadati è limitata alle entità a protezione diretta di cui l'utente è proprietario o per le quali dispone di autorizzazioni. Per ulteriori informazioni, vedere Configurazione della visibilità dei metadati.

Esempi

A. Visualizzazione del nome di una chiave simmetrica mediante key_guid

Il database master contiene una chiave simmetrica denominata ##MS_ServiceMasterKey##. Nell'esempio seguente il GUID di tale chiave viene recuperato dalla vista a gestione dinamica sys.symmetric_keys e viene assegnato a una variabile che viene quindi passata alla funzione KEY_NAME per illustrare come restituire il nome corrispondente al GUID.

USE master
GO
DECLARE @guid uniqueidentifier ;
SELECT @guid = key_guid FROM sys.symmetric_keys
WHERE name = '##MS_ServiceMasterKey##' ;
-- Demonstration of passing a GUID to KEY_NAME to receive a name
SELECT KEY_NAME(@guid) AS [Name of Key];
PASSWORD = 'decryption_password'

B. Visualizzazione del nome di una chiave simmetrica mediante il testo crittografato

Nell'esempio seguente viene illustrato l'intero processo di creazione di una chiave simmetrica e popolamento di una tabella con i dati. Nell'esempio viene quindi mostrata la restituzione del nome della chiave da KEY_NAME quando viene passato il testo crittografato.

-- Create a symmetric key
CREATE SYMMETRIC KEY TestSymKey 
   WITH ALGORITHM = AES_128,
   KEY_SOURCE = 'The square of the hypotenuse is equal to the sum of the squares of the sides',
   IDENTITY_VALUE = 'Pythagoras'
   ENCRYPTION BY PASSWORD = 'pGFD4bb925DGvbd2439587y' ;
GO
-- Create a table for the demonstration
CREATE TABLE DemoKey
(IDCol int IDENTITY PRIMARY KEY,
SecretCol varbinary(256) NOT NULL)
GO
-- Open the symmetric key if not already open
OPEN SYMMETRIC KEY TestSymKey 
    DECRYPTION BY PASSWORD = 'pGFD4bb925DGvbd2439587y';
GO
-- Insert a row into the DemoKey table
DECLARE @key_GUID uniqueidentifier
SELECT @key_GUID = key_guid FROM sys.symmetric_keys
WHERE name LIKE 'TestSymKey' ;
INSERT INTO DemoKey(SecretCol)
VALUES ( ENCRYPTBYKEY (@key_GUID, 'EncryptedText'))
GO
-- Verify the DemoKey data
SELECT * FROM DemoKey
GO
-- Decrypt the data
DECLARE @ciphertext varbinary(256)
SELECT @ciphertext = SecretCol
FROM DemoKey WHERE IDCol = 1 ;
SELECT CAST (
DECRYPTBYKEY( @ciphertext)
AS varchar(100) ) AS SecretText ;
-- Use KEY_NAME to view the name of the key
SELECT KEY_NAME(@ciphertext) AS [Name of Key] ;