Utilizzo delle chiavi - AWS Key Management Service

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzo delle chiavi

Gli esempi in questo argomento utilizzano l'API AWS KMS per creare, visualizzare, abilitare e disabilitare AWS KMS AWS KMS keys e per generare chiavi dei dati.

Creazione di una chiave KMS

Per creare una AWS KMS key(chiave KMS), utilizzare l'CreateKeyoperazione. Gli esempi in questa sezione creano una chiave KMS di crittografia simmetrica. Il parametro Description utilizzato in questi esempi è opzionale.

Nelle lingue che richiedono un oggetto client, questi esempi utilizzano l'oggetto client AWS KMS creato in Creazione di un client.

Per informazioni sulla creazione di chiavi KMS nella console AWS KMS, consulta Creazione di chiavi.

Java

Per ulteriori informazioni, consulta il metodo createKey nella Documentazione di riferimento dell'API AWS SDK for Java.

// Create a KMS key // String desc = "Key for protecting critical data"; CreateKeyRequest req = new CreateKeyRequest().withDescription(desc); CreateKeyResult result = kmsClient.createKey(req);
C#

Per maggiori dettagli, consultare il metodo CreateKey in AWS SDK for .NET.

// Create a KMS key // String desc = "Key for protecting critical data"; CreateKeyRequest req = new CreateKeyRequest() { Description = desc }; CreateKeyResponse response = kmsClient.CreateKey(req);
Python

Per maggiori dettagli, consultare il metodo create_key in AWS SDK for Python (Boto3).

# Create a KMS key desc = 'Key for protecting critical data' response = kms_client.create_key( Description=desc )
Ruby

Per maggiori dettagli, consultare il metodo dell'istanza create_key in AWS SDK for Ruby.

# Create a KMS key desc = 'Key for protecting critical data' response = kmsClient.create_key({ description: desc })
PHP

Per maggiori dettagli, consultare il metodo CreateKey in AWS SDK for PHP.

// Create a KMS key // $desc = "Key for protecting critical data"; $result = $KmsClient->createKey([ 'Description' => $desc ]);
Node.js

Per i dettagli, vedete la proprietà CreateKey nell'SDK JavaScript per AWS Node.js.

// Create a KMS key // const Description = 'Key for protecting critical data'; kmsClient.createKey({ Description }, (err, data) => { ... });
PowerShell

Per creare una chiave KMS PowerShell, utilizzare il cmdlet New-. KmsKey

# Create a KMS key $desc = 'Key for protecting critical data' New-KmsKey -Description $desc

Per utilizzare i AWS KMS PowerShell cmdlet, installa AWS.Tools. KeyManagementServicemodulo. Per ulteriori informazioni, consulta la AWS Tools for Windows PowerShellGuida per l'utente.

Generazione di una chiave di dati

Per generare una chiave dati simmetrica, usa l'GenerateDataKeyoperazione. Questa operazione restituisce una chiave di dati di testo normale e una copia di tale chiave di dati crittografata sotto la chiave KMS di crittografia simmetrica specificata. È necessario specificare KeySpec o NumberOfBytes (ma non entrambi) in ogni comando.

Per informazioni sull'utilizzo della chiave di dati per crittografare i dati, consultare AWS Encryption SDK. Puoi utilizzare la chiave di dati anche nelle operazioni HMAC.

Nelle lingue che richiedono un oggetto client, questi esempi utilizzano l'oggetto client AWS KMS creato in Creazione di un client.

Java

Per i dettagli, consulta il generateDataKey metodo nell'AWS SDK for JavaAPI Reference.

// Generate a data key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; GenerateDataKeyRequest dataKeyRequest = new GenerateDataKeyRequest(); dataKeyRequest.setKeyId(keyId); dataKeyRequest.setKeySpec("AES_256"); GenerateDataKeyResult dataKeyResult = kmsClient.generateDataKey(dataKeyRequest); ByteBuffer plaintextKey = dataKeyResult.getPlaintext(); ByteBuffer encryptedKey = dataKeyResult.getCiphertextBlob();
C#

Per maggiori dettagli, consultare il metodo GenerateDataKey in AWS SDK for .NET.

// Generate a data key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; GenerateDataKeyRequest dataKeyRequest = new GenerateDataKeyRequest() { KeyId = keyId, KeySpec = DataKeySpec.AES_256 }; GenerateDataKeyResponse dataKeyResponse = kmsClient.GenerateDataKey(dataKeyRequest); MemoryStream plaintextKey = dataKeyResponse.Plaintext; MemoryStream encryptedKey = dataKeyResponse.CiphertextBlob;
Python

Per maggiori dettagli, consultare il metodo generate_data_key in AWS SDK for Python (Boto3).

# Generate a data key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kms_client.generate_data_key( KeyId=key_id, KeySpec='AES_256' ) plaintext_key = response['Plaintext'] encrypted_key = response['CiphertextBlob']
Ruby

Per maggiori dettagli, consultare il metodo dell'istanza generate_data_key in AWS SDK for Ruby.

# Generate a data key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kmsClient.generate_data_key({ key_id: key_id, key_spec: 'AES_256' }) plaintext_key = response.plaintext encrypted_key = response.ciphertext_blob
PHP

Per maggiori dettagli, consultare il metodo GenerateDataKey in AWS SDK for PHP.

// Generate a data key // // Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $keySpec = 'AES_256'; $result = $KmsClient->generateDataKey([ 'KeyId' => $keyId, 'KeySpec' => $keySpec, ]); $plaintextKey = $result['Plaintext']; $encryptedKey = $result['CiphertextBlob'];
Node.js

Per i dettagli, consultate la generateDataKey proprietà nell'AWSSDK per JavaScript Node.js.

// Generate a data key // // Replace the following example key ARN with any valid key identfier const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const KeySpec = 'AES_256'; kmsClient.generateDataKey({ KeyId, KeySpec }, (err, data) => { if (err) console.log(err, err.stack); else { const { CiphertextBlob, Plaintext } = data; ... } });
PowerShell

Per generare una chiave dati simmetrica, utilizzare il cmdlet New-KMS. DataKey

Nell'output, la chiave in chiaro (nella Plaintext proprietà) e la chiave crittografata (nella proprietà) sono oggetti. CiphertextBlob MemoryStream Per convertirli in stringhe, utilizzate i metodi della MemoryStream classe oppure un cmdlet o una funzione che converte MemoryStream gli oggetti in stringhe, ad esempio le funzioni ConvertFrom- MemoryStream e ConvertFrom -Base64 nel modulo Convert.

# Generate a data key # Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $keySpec = 'AES_256' $response = New-KmsDataKey -KeyId $keyId -KeySpec $keySpec $plaintextKey = $response.Plaintext $encryptedKey = $response.CiphertextBlob

Per utilizzare i cmdlet, installa AWS.Tools. AWS KMS PowerShell KeyManagementServicemodulo. Per ulteriori informazioni, consulta la AWS Tools for Windows PowerShellGuida per l'utente.

Visualizzazione di un AWS KMS key

Per ottenere informazioni dettagliate su un ARNAWS KMS key, tra cui l'ARN della chiave KMS e lo stato della chiave, utilizzare l'DescribeKeyoperazione.

DescribeKey non ottiene alias. Per ottenere gli alias, usa l'operazione. ListAliases Per alcuni esempi, consulta Utilizzo degli alias.

Nelle lingue che richiedono un oggetto client, questi esempi utilizzano l'oggetto client AWS KMS creato in Creazione di un client.

Per informazioni sulla visualizzazione delle chiavi KMS nella console AWS KMS, consulta Visualizzazione di chiavi.

Java

Per ulteriori informazioni, consulta il metodo describeKey nella Documentazione di riferimento dell'API AWS SDK for Java.

// Describe a KMS key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DescribeKeyRequest req = new DescribeKeyRequest().withKeyId(keyId); DescribeKeyResult result = kmsClient.describeKey(req);
C#

Per maggiori dettagli, consultare il metodo DescribeKey in AWS SDK for .NET.

// Describe a KMS key // // Replace the following example key ARN with any valid key identfier String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DescribeKeyRequest describeKeyRequest = new DescribeKeyRequest() { KeyId = keyId }; DescribeKeyResponse describeKeyResponse = kmsClient.DescribeKey(describeKeyRequest);
Python

Per maggiori dettagli, consultare il metodo describe_key in AWS SDK for Python (Boto3).

# Describe a KMS key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kms_client.describe_key( KeyId=key_id )
Ruby

Per maggiori dettagli, consultare il metodo dell'istanza describe_key in AWS SDK for Ruby.

# Describe a KMS key # Replace the following example key ARN with any valid key identfier key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kmsClient.describe_key({ key_id: key_id })
PHP

Per maggiori dettagli, consultare il metodo DescribeKey in AWS SDK for PHP.

// Describe a KMS key // // Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $result = $KmsClient->describeKey([ 'KeyId' => $keyId, ]);
Node.js

Per i dettagli, vedete la proprietà DescribeKey nell'SDK JavaScript per in AWS Node.js.

// Describe a KMS key // // Replace the following example key ARN with any valid key identfier const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; kmsClient.describeKey({ KeyId }, (err, data) => { ... });
PowerShell

Per ottenere informazioni dettagliate su una chiave KMS, utilizzare il cmdlet Get-. KmsKey

# Describe a KMS key # Replace the following example key ARN with any valid key identfier $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' Get-KmsKey -KeyId $keyId

Per utilizzare i AWS KMS PowerShell cmdlet, installa AWS.Tools. KeyManagementServicemodulo. Per ulteriori informazioni, consulta la AWS Tools for Windows PowerShellGuida per l'utente.

Ottenimento degli ID e degli ARN delle chiavi KMS

Per ottenere gli ID delle chiavi e gli ARN delle chiavi diAWS KMS keys, utilizzare l'ListKeysoperazione. Questi esempi utilizzano il parametro Limit opzionale, che imposta il numero massimo di chiavi KMS restituite in ogni chiamata. Per informazioni sull'identificazione di una chiave KMS in un'operazione AWS KMS, consulta Identificatori chiave () KeyId.

Nelle lingue che richiedono un oggetto client, questi esempi utilizzano l'oggetto client AWS KMS creato in Creazione di un client.

Per informazioni sulla ricerca di ID chiave e ARN della chiave nella console AWS KMS, consulta Individuazione dell'ID e dell'ARN della chiave.

Java

Per ulteriori informazioni, consulta il metodo listKeys nella Documentazione di riferimento dell'API AWS SDK for Java.

// List KMS keys in this account // Integer limit = 10; ListKeysRequest req = new ListKeysRequest().withLimit(limit); ListKeysResult result = kmsClient.listKeys(req);
C#

Per maggiori dettagli, consultare il metodo ListKeys in AWS SDK for .NET.

// List KMS keys in this account // int limit = 10; ListKeysRequest listKeysRequest = new ListKeysRequest() { Limit = limit }; ListKeysResponse listKeysResponse = kmsClient.ListKeys(listKeysRequest);
Python

Per maggiori dettagli, consultare il metodo list_keys in AWS SDK for Python (Boto3).

# List KMS keys in this account response = kms_client.list_keys( Limit=10 )
Ruby

Per maggiori dettagli, consultare il metodo dell'istanza list_keys in AWS SDK for Ruby.

# List KMS keys in this account response = kmsClient.list_keys({ limit: 10 })
PHP

Per maggiori dettagli, consultare il metodo ListKeys in AWS SDK for PHP.

// List KMS keys in this account // $limit = 10; $result = $KmsClient->listKeys([ 'Limit' => $limit, ]);
Node.js

Per i dettagli, vedete la proprietà ListKeys nell'AWSSDK per JavaScript in Node.js.

// List KMS keys in this account // const Limit = 10; kmsClient.listKeys({ Limit }, (err, data) => { ... });
PowerShell

Per ottenere l'ID della chiave e l'ARN della chiave di tutte le chiavi KMS nell'account e nella regione, utilizzare il cmdlet Get -. KmsKeyList

Per limitare il numero di oggetti di output, in questo esempio viene utilizzato il cmdlet Select-Object anziché il parametro Limit, che viene considerato obsoleto nei cmdlet list. Per informazioni sull'impaginazione dell'output in AWS Tools for PowerShell, consulta Paginazione output con AWS Tools for PowerShell.

# List KMS keys in this account $limit = 10 Get-KmsKeyList | Select-Object -First $limit

Per utilizzare i AWS KMS PowerShell cmdlet, installa AWS.Tools. KeyManagementServicemodulo. Per ulteriori informazioni, consulta la AWS Tools for Windows PowerShellGuida per l'utente.

Abilitazione di AWS KMS keys

Per abilitare un disabileAWS KMS key, usa l'EnableKeyoperazione.

Nelle lingue che richiedono un oggetto client, questi esempi utilizzano l'oggetto client AWS KMS creato in Creazione di un client.

Per informazioni su come abilitare e disabilitare le chiavi KMS nella console AWS KMS, consulta Abilitazione e disabilitazione delle chiavi.

Java

Per ulteriori informazioni sull'implementazione Java, consulta il metodo enableKey nella Documentazione di riferimento dell'API AWS SDK for Java.

// Enable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; EnableKeyRequest req = new EnableKeyRequest().withKeyId(keyId); kmsClient.enableKey(req);
C#

Per maggiori dettagli, consultare il metodo EnableKey in AWS SDK for .NET.

// Enable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; EnableKeyRequest enableKeyRequest = new EnableKeyRequest() { KeyId = keyId }; kmsClient.EnableKey(enableKeyRequest);
Python

Per maggiori dettagli, consultare il metodo enable_key in AWS SDK for Python (Boto3).

# Enable a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kms_client.enable_key( KeyId=key_id )
Ruby

Per maggiori dettagli, consultare il metodo dell'istanza enable_key in AWS SDK for Ruby.

# Enable a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kmsClient.enable_key({ key_id: key_id })
PHP

Per maggiori dettagli, consultare il metodo EnableKey in AWS SDK for PHP.

// Enable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $result = $KmsClient->enableKey([ 'KeyId' => $keyId, ]);
Node.js

Per i dettagli, vedete la proprietà EnableKey nell'SDK JavaScript per in AWS Node.js.

// Enable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; kmsClient.enableKey({ KeyId }, (err, data) => { ... });
PowerShell

Per abilitare una chiave KMS, utilizzare il cmdlet Enable-. KmsKey

# Enable a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' Enable-KmsKey -KeyId $keyId

Per utilizzare i AWS KMS PowerShell cmdlet, installa AWS.Tools. KeyManagementServicemodulo. Per ulteriori informazioni, consulta la AWS Tools for Windows PowerShellGuida per l'utente.

Disabilitazione di AWS KMS key

Per disabilitare una chiave KMS, usa l'DisableKeyoperazione. La disabilitazione di una chiave KMS impedisce che venga utilizzata nelle operazioni di crittografia.

Nelle lingue che richiedono un oggetto client, questi esempi utilizzano l'oggetto client AWS KMS creato in Creazione di un client.

Per informazioni su come abilitare e disabilitare le chiavi KMS nella console AWS KMS, consulta Abilitazione e disabilitazione delle chiavi.

Java

Per ulteriori informazioni, consulta il metodo disableKey nella Documentazione di riferimento dell'API AWS SDK for Java.

// Disable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DisableKeyRequest req = new DisableKeyRequest().withKeyId(keyId); kmsClient.disableKey(req);
C#

Per maggiori dettagli, consultare il metodo DisableKey in AWS SDK for .NET.

// Disable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; DisableKeyRequest disableKeyRequest = new DisableKeyRequest() { KeyId = keyId }; kmsClient.DisableKey(disableKeyRequest);
Python

Per maggiori dettagli, consultare il metodo disable_key in AWS SDK for Python (Boto3).

# Disable a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kms_client.disable_key( KeyId=key_id )
Ruby

Per maggiori dettagli, consultare il metodo dell'istanza disable_key in AWS SDK for Ruby.

# Disable a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kmsClient.disable_key({ key_id: key_id })
PHP

Per maggiori dettagli, consultare il metodo DisableKey in AWS SDK for PHP.

// Disable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $result = $KmsClient->disableKey([ 'KeyId' => $keyId, ]);
Node.js

Per i dettagli, vedete la proprietà DisableKey nell'SDK JavaScript per in AWS Node.js.

// Disable a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; kmsClient.disableKey({ KeyId }, (err, data) => { ... });
PowerShell

Per disabilitare una chiave KMS, utilizzare il cmdlet Disable-. KmsKey

# Disable a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' Disable-KmsKey -KeyId $keyId

Per utilizzare i AWS KMS PowerShell cmdlet, installa AWS.Tools. KeyManagementServicemodulo. Per ulteriori informazioni, consulta la Guida per l'utente AWS Tools for Windows PowerShell.