Crittografia e decrittografia delle chiavi di dati - 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à.

Crittografia e decrittografia delle chiavi di dati

Gli esempi in questo argomento utilizzano Encrypt, Decrypt e ReEncryptle operazioni nell'API. AWS KMS

Queste operazioni sono progettate per crittografare e decrittografare le chiavi dei dati. Usano una AWS KMS keys nelle operazioni di crittografia e non possono accettare più di 4 KB (4096 byte) di dati. Anche se è possibile utilizzarli per crittografare piccole quantità di dati, ad esempio una password o la chiave RSA, non sono state progettate per crittografare i dati delle applicazioni.

Per crittografare i dati delle applicazioni, utilizza le funzionalità di crittografia lato server di un servizio AWS o una libreria di crittografia lato client, come il client di crittografia AWS Encryption SDK o quello di Amazon S3.

Crittografia di una chiave di dati

L'operazione Encrypt è progettata per crittografare le chiavi dei dati, ma non viene utilizzata di frequente. Le GenerateDataKeyWithoutPlaintextoperazioni GenerateDataKeyand restituiscono chiavi di dati crittografate. Puoi utilizzare questo metodo quando desideri spostare dati crittografati in una diversa regione e vuoi crittografare i dati con una chiave KMS nella nuova regione.

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

Java

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

// Encrypt 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"; ByteBuffer plaintext = ByteBuffer.wrap(new byte[]{1,2,3,4,5,6,7,8,9,0}); EncryptRequest req = new EncryptRequest().withKeyId(keyId).withPlaintext(plaintext); ByteBuffer ciphertext = kmsClient.encrypt(req).getCiphertextBlob();
C#

Per ulteriori informazioni, consulta il metodo di crittografia nella AWS SDK for .NET.

// Encrypt 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"; MemoryStream plaintext = new MemoryStream(); plaintext.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, 0, 10); EncryptRequest encryptRequest = new EncryptRequest() { KeyId = keyId, Plaintext = plaintext }; MemoryStream ciphertext = kmsClient.Encrypt(encryptRequest).CiphertextBlob;
Python

Per ulteriori informazioni, consulta il metodo di crittografia nella AWS SDK for Python (Boto3).

# Encrypt 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' plaintext = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00' response = kms_client.encrypt( KeyId=key_id, Plaintext=plaintext ) ciphertext = response['CiphertextBlob']
Ruby

Per ulteriori informazioni, consulta il metodo dell'istanza encrypt nella AWS SDK for Ruby.

# Encrypt 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' plaintext = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00" response = kmsClient.encrypt({ key_id: key_id, plaintext: plaintext }) ciphertext = response.ciphertext_blob
PHP

Per ulteriori informazioni, consulta il metodo di crittografia nella AWS SDK for PHP.

// Encrypt 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'; $message = pack('c*',1,2,3,4,5,6,7,8,9,0); $result = $KmsClient->encrypt([ 'KeyId' => $keyId, 'Plaintext' => $message, ]); $ciphertext = $result['CiphertextBlob'];
Node.js

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

// Encrypt 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 Plaintext = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); kmsClient.encrypt({ KeyId, Plaintext }, (err, data) => { if (err) console.log(err, err.stack); // an error occurred else { const { CiphertextBlob } = data; ... } });
PowerShell

Per crittografare una chiave di dati in una chiave KMS, utilizza il cmdlet Invoke-KMSEncrypt. Restituisce il testo cifrato come (System.IO. MemoryStream MemoryStream) oggetto. È possibile utilizzare l'oggetto MemoryStream come input del cmdlet Invoke-KmsDecrypt.

AWS KMS restituisce anche chiavi dati come oggetti MemoryStream. In questo esempio, per simulare una chiave di dati in testo normale, creiamo un array di byte e lo scriviamo su un oggetto MemoryStream.

Si noti che il parametro Plaintextdi Invoke-KMSEncrypt accetta un array di byte (byte[]); non richiede un oggetto MemoryStream. A partire dalla AWSPowerShell versione 4.0, i parametri in tutti i AWSPowerShell moduli che accettano array e MemoryStream oggetti di byte accettano array di byte, oggetti, stringhe, MemoryStream array di stringhe e (System.IO. FileInfo FileInfo) oggetti. È possibile passare uno qualsiasi di questi tipi a Invoke-KMSEncrypt.

# Encrypt 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' # Simulate a data key # Create a byte array [byte[]] $bytes = 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 # Create a MemoryStream $plaintext = [System.IO.MemoryStream]::new() # Add the byte array to the MemoryStream $plaintext.Write($bytes, 0, $bytes.length) # Encrypt the simulated data key $response = Invoke-KMSEncrypt -KeyId $keyId -Plaintext $plaintext # Get the ciphertext from the response $ciphertext = $response.CiphertextBlob

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

Decrittografia di una chiave di dati

Per decrittografare una chiave di dati, utilizza l'operazione Decrypt.

Il ciphertextBlob valore specificato deve essere il valore del CiphertextBlob campo di una risposta GenerateDataKeyGenerateDataKeyWithoutPlaintext, o Encrypt, o il PrivateKeyCiphertextBlob campo di una GenerateDataKeyPairWithoutPlaintextrisposta GenerateDataKeyPairor. Puoi inoltre utilizzare l'operazione Decrypt per decrittare i dati crittografati all'esterno di AWS KMS dalla chiave pubblica in una chiave KMS asimmetrica.

Il parametro KeyId non è richiesto quando per la decrittografia si utilizzano chiavi KMS di crittografia simmetrica. AWS KMS può ottenere la chiave KMS utilizzata per crittografare i dati dai metadati nel blob in testo cifrato. Tuttavia è sempre consigliabile specificare la chiave KMS che stai utilizzando. Questa procedura garantisce di utilizzare la chiave KMS desiderata e impedisce di decrittare inavvertitamente un testo cifrato utilizzando una chiave KMS non attendibile.

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

Java

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

// Decrypt 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"; ByteBuffer ciphertextBlob = Place your ciphertext here; DecryptRequest req = new DecryptRequest().withCiphertextBlob(ciphertextBlob).withKeyId(keyId); ByteBuffer plainText = kmsClient.decrypt(req).getPlaintext();
C#

Per ulteriori informazioni, consulta il metodo di decrittografia nella AWS SDK for .NET.

// Decrypt 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"; MemoryStream ciphertextBlob = new MemoryStream(); // Write ciphertext to memory stream DecryptRequest decryptRequest = new DecryptRequest() { CiphertextBlob = ciphertextBlob, KeyId = keyId }; MemoryStream plainText = kmsClient.Decrypt(decryptRequest).Plaintext;
Python

Per ulteriori informazioni, consulta il metodo di decrittografia nella AWS SDK for Python (Boto3).

# Decrypt 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' ciphertext = 'Place your ciphertext here' response = kms_client.decrypt( CiphertextBlob=ciphertext, KeyId=key_id ) plaintext = response['Plaintext']
Ruby

Per ulteriori informazioni, consulta il metodo dell'istanza decrypt nella AWS SDK for Ruby.

# Decrypt 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' ciphertext = 'Place your ciphertext here' ciphertext_packed = [ciphertext].pack("H*") response = kmsClient.decrypt({ ciphertext_blob: ciphertext_packed, key_id: key_id }) plaintext = response.plaintext
PHP

Per ulteriori informazioni, consulta il metodo di decrittografia nella AWS SDK for PHP.

// Decrypt 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'; $ciphertext = 'Place your cipher text blob here'; $result = $KmsClient->decrypt([ 'CiphertextBlob' => $ciphertext, 'KeyId' => $keyId, ]); $plaintext = $result['Plaintext'];
Node.js

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

// Decrypt 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 CiphertextBlob = 'Place your cipher text blob here'; kmsClient.decrypt({ CiphertextBlob, KeyId }, (err, data) => { if (err) console.log(err, err.stack); // an error occurred else { const { Plaintext } = data; ... } });
PowerShell

Per decrittare una chiave dati, utilizzare il cmdlet Invoke-KMSEncrypt.

Questo cmdlet restituisce il testo semplice come (System.IO). MemoryStream MemoryStream) oggetto. Per convertirlo in un array di byte, utilizzare cmdlet o funzioni che convertono oggetti MemoryStream in array di byte, ad esempio le funzioni del modulo Convert.

Poiché in questo esempio viene utilizzato il testo cifrato restituito da un cmdlet di crittografia AWS KMS, viene utilizzato un oggetto MemoryStream per il valore del parametro CiphertextBlob. Tuttavia, il parametro CiphertextBlob di Invoke-KMSDecrypt accetta un array di byte (byte[]); non richiede un oggetto MemoryStream. A partire dalla AWSPowerShell versione 4.0, i parametri in tutti i AWSPowerShell moduli che accettano array e MemoryStream oggetti di byte accettano array di byte, oggetti, stringhe, MemoryStream array di stringhe e (System.IO. FileInfo FileInfo) oggetti. È possibile passare uno qualsiasi di questi tipi a Invoke-KMSDecrypt.

# Decrypt 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' [System.IO.MemoryStream]$ciphertext = Read-Host 'Place your cipher text blob here' $response = Invoke-KMSDecrypt -CiphertextBlob $ciphertext -KeyId $keyId $plaintext = $response.Plaintext

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

Ricrittografia di una chiave di dati in un'altra AWS KMS key

Per decrittografare una chiave dati crittografata e quindi ricrittografare immediatamente la chiave dati con un'altraAWS KMS key, utilizzare l'operazione. ReEncrypt Le operazioni vengono eseguite interamente su lato server all'interno di AWS KMS, in modo che il testo normale non dovrà mai essere esposto all'esterno di AWS KMS.

ciphertextBlobCiò che specifichi deve essere il valore del CiphertextBlob campo di una GenerateDataKeyrisposta o Encrypt o il PrivateKeyCiphertextBlob campo di una risposta or. GenerateDataKeyWithoutPlaintextGenerateDataKeyPairGenerateDataKeyPairWithoutPlaintext Puoi inoltre utilizzare l'operazione ReEncrypt per crittografare nuovamente i dati crittografati all'esterno di AWS KMS dalla chiave pubblica in una chiave KMS asimmetrica.

Il parametro SourceKeyId non è richiesto quando si crittografa nuovamente con chiavi KMS di crittografia simmetrica. AWS KMS può ottenere la chiave KMS utilizzata per crittografare i dati dai metadati nel blob in testo cifrato. Tuttavia è sempre consigliabile specificare la chiave KMS che stai utilizzando. Questa procedura garantisce di utilizzare la chiave KMS desiderata e impedisce di decrittare inavvertitamente un testo cifrato utilizzando una chiave KMS non attendibile.

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

Java

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

// Re-encrypt a data key ByteBuffer sourceCiphertextBlob = Place your ciphertext here; // Replace the following example key ARNs with valid key identfiers String sourceKeyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String destinationKeyId = "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321"; ReEncryptRequest req = new ReEncryptRequest(); req.setCiphertextBlob(sourceCiphertextBlob); req.setSourceKeyId(sourceKeyId); req.setDestinationKeyId(destinationKeyId); ByteBuffer destinationCipherTextBlob = kmsClient.reEncrypt(req).getCiphertextBlob();
C#

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

// Re-encrypt a data key MemoryStream sourceCiphertextBlob = new MemoryStream(); // Write ciphertext to memory stream // Replace the following example key ARNs with valid key identfiers String sourceKeyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String destinationKeyId = "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321"; ReEncryptRequest reEncryptRequest = new ReEncryptRequest() { CiphertextBlob = sourceCiphertextBlob, SourceKeyId = sourceKeyId, DestinationKeyId = destinationKeyId }; MemoryStream destinationCipherTextBlob = kmsClient.ReEncrypt(reEncryptRequest).CiphertextBlob;
Python

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

# Re-encrypt a data key ciphertext = 'Place your ciphertext here' # Replace the following example key ARNs with valid key identfiers source_key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' destination_key_id = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321' response = kms_client.re_encrypt( CiphertextBlob=ciphertext, SourceKeyId=source_key_id, DestinationKeyId=destination_key_id ) destination_ciphertext_blob = response['CiphertextBlob']
Ruby

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

# Re-encrypt a data key ciphertext = 'Place your ciphertext here' ciphertext_packed = [ciphertext].pack("H*") # Replace the following example key ARNs with valid key identfiers source_key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' destination_key_id = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321' response = kmsClient.re_encrypt({ ciphertext_blob: ciphertext_packed, source_key_id: source_key_id, destination_key_id: destination_key_id }) destination_ciphertext_blob = response.ciphertext_blob.unpack('H*')
PHP

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

// Re-encrypt a data key $ciphertextBlob = 'Place your ciphertext here'; // Replace the following example key ARNs with valid key identfiers $sourceKeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $destinationKeyId = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321'; $result = $KmsClient->reEncrypt([ 'CiphertextBlob' => $ciphertextBlob, 'SourceKeyId' => $sourceKeyId, 'DestinationKeyId' => $destinationKeyId, ]);
Node.js

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

// Re-encrypt a data key const CiphertextBlob = 'Place your cipher text blob here'; // Replace the following example key ARNs with valid key identfiers const SourceKeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const DestinationKeyId = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321'; kmsClient.reEncrypt({ CiphertextBlob, SourceKeyId, DestinationKeyId }, (err, data) => { ... });
PowerShell

Per crittografare nuovamente un testo cifrato con la stessa chiave KMS o con una chiave diversa, utilizzare il cmdlet Invoke-KMS. ReEncrypt

Poiché in questo esempio viene utilizzato il testo cifrato restituito da un cmdlet di crittografia AWS KMS, viene utilizzato un oggetto MemoryStream per il valore del parametro CiphertextBlob. Tuttavia, il parametro CiphertextBlob di Invoke-KMSReEncrypt accetta un array di byte (byte[]); non richiede un oggetto MemoryStream. A partire dalla AWSPowerShell versione 4.0, i parametri in tutti i AWSPowerShell moduli che utilizzano matrici di byte e gli MemoryStream oggetti accettano array di byte, oggetti, stringhe, matrici di stringhe e (System.IO. MemoryStreamFileInfo FileInfo) oggetti. È possibile passare uno qualsiasi di questi tipi a Invoke-KMSReEncrypt.

# Re-encrypt a data key [System.IO.MemoryStream]$ciphertextBlob = Read-Host 'Place your cipher text blob here' # Replace the following example key ARNs with valid key identfiers $sourceKeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $destinationKeyId = 'arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321' $response = Invoke-KMSReEncrypt -Ciphertext $ciphertextBlob -SourceKeyId $sourceKeyId -DestinationKeyId $destinationKeyId $reEncryptedCiphertext = $response.CiphertextBlob

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.