KEY_NAME (Transact-SQL)

Si applica a:SQL Server database SQL di Azure Istanza gestita di SQL di Azure

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

Convenzioni di sintassi Transact-SQL

Sintassi

KEY_NAME ( ciphertext | key_guid )   

Nota

Per visualizzare la sintassi Transact-SQL per SQL Server 2014 (12.x) e versioni precedenti, vedere la documentazione delle versioni precedenti.

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 (9.x) la visibilità dei metadati è limitata alle entità a sicurezza diretta di cui l'utente è proprietario o per le quali dispone di autorizzazioni. Per altre informazioni, vedere Metadata Visibility Configuration.

Esempi

R. 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];  

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] ;  

Vedi anche

sys.symmetric_keys (Transact-SQL)
ENCRYPTBYKEY (Transact-SQL)
DECRYPTBYKEYAUTOASYMKEY (Transact-SQL)