本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
加密和解密資料金鑰
本主題中的範例使用 AWS KMS API 中的「加密」、「解密」和「ReEncrypt作業」。
這些操作旨在加密和解密資料金鑰。它們在加密操作中使用 AWS KMS keys,且無法接受超過 4 KB (4096 個位元組) 的資料。雖然您可以使用它們來加密少量資料,例如密碼或 RSA 金鑰,但它們不是為加密應用程式資料而設計的。
若要加密應用程式資料,請使用 AWS 服務的伺服器端加密功能或用戶端加密程式庫,例如 AWS Encryption SDK 或 Amazon S3 加密用戶端。
加密資料金鑰
加密操作旨在加密資料金鑰,但不常使用。GenerateDataKey和作GenerateDataKeyWithoutPlaintext業會傳回加密的資料金鑰。當您要將加密的資料移到不同區域或是想使用新區域的 KMS 金鑰加密其資料金鑰,可以使用此方法。
在需要用戶端物件的語言中,這些範例會使用您在 建立用戶端 中建立的 AWS KMS 用戶端物件。
- Java
-
如需詳細資訊,請參閱《AWS SDK for Java API 參考》中的 encrypt 方法。
// 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#
-
如需詳細資訊,請參閱 AWS SDK for .NET 中的 Encrypt 方法。
// 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
-
如需詳細資訊,請參閱 AWS SDK for Python (Boto3) 中的 encrypt 方法
。 # 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
-
如需詳細資訊,請參閱 AWS SDK for Ruby 中的 encrypt 執行個體方法。
# 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
-
如需詳細資訊,請參閱 AWS SDK for PHP 中的 Encrypt 方法。
// 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
如需詳細資訊,請參閱 Node.js 中的 AWS SDK JavaScript 中的加密屬性。
// 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
若要加密 KMS 金鑰下的資料金鑰,請使用 Invoke-KMSEncrypt cmdlet。它返回密文作為一個
MemoryStream
(系統。IO。 MemoryStream) 物件。您可以使用 MemoryStream
物件做為 Invoke-KMSDecrypt Cmdlet 的輸入。AWS KMS 也傳回資料金鑰作為
MemoryStream
物件。在這個例子中,為了模擬純文字資料金鑰,我們建立一個位元組陣列,並將其寫入MemoryStream
物件。請注意,
Invoke-KMSEncrypt
的Plaintext
參數採用位元組陣列 (byte[]
);此參數不需要MemoryStream
物件。從 4.0 AWSPowerShell 版開始,採用字節數組和MemoryStream
對象的所有 AWSPowerShell 模塊中的參數都接受字節數組,MemoryStream
對象,字符串,字符串數組和FileInfo
(System.io。 FileInfo) 物件。您可以將這些類型中的任何一項傳遞給 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
若要使用AWS KMS PowerShell 指令程式,請安裝 AWS.Tools。 KeyManagementService
模塊。如需詳細資訊,請參閱《 使用者指南》AWS Tools for Windows PowerShell。
解密資料金鑰
若要解密資料金鑰,請使用 Decrypt 操作。
您指定的必須ciphertextBlob
是GenerateDataKey、GenerateDataKeyWithoutPlaintext或「加密」回應中的CiphertextBlob
欄位值,或來自或回GenerateDataKeyPairWithoutPlaintext應的PrivateKeyCiphertextBlob
GenerateDataKeyPair欄位值。您也可以使用此 Decrypt
操作來解密非對稱 KMS 金鑰中 AWS KMS 以外由公有金鑰加密的資料。
使用對稱加密 KMS 金鑰進行解密時,不需要 KeyId
參數。AWS KMS 可以從加密文字 Blob 的中繼資料取得用來加密資料的 KMS 金鑰。但是指定您正在使用的 KMS 金鑰永遠是最佳實務。此實務可確保您使用預定的 KMS 金鑰,並防止您意外使用您不信任的 KMS 金鑰來解密加密文字。
在需要用戶端物件的語言中,這些範例會使用您在 建立用戶端 中建立的 AWS KMS 用戶端物件。
- Java
-
如需詳細資訊,請參閱《AWS SDK for Java API 參考》中的 decrypt 方法。
// 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#
-
如需詳細資訊,請參閱 AWS SDK for .NET 中的 Decrypt 方法。
// 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
-
如需詳細資訊,請參閱 AWS SDK for Python (Boto3) 中的 decrypt 方法
。 # 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
-
如需詳細資訊,請參閱 AWS SDK for Ruby 中的 decrypt 執行個體方法。
# 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
-
如需詳細資訊,請參閱 AWS SDK for PHP 中的 Decrypt 方法。
// 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
如需詳細資訊,請參閱 Node.js 中的 AWS SDK 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
若要解密資料金鑰,請使用 Invoke-KMSEncrypt Cmdlet。
此指令程式會傳回純文字為 (System.io.
MemoryStream
MemoryStream) 物件。若要將其轉換為位元組陣列,請使用 Cmdlet 或將 MemoryStream
物件轉換為位元組陣列的函數,例如 Convert模組中的函數。 因為此範例會使用 AWS KMS 加密 cmdlet 傳回的加密文字,所以會使用
MemoryStream
物件做為CiphertextBlob
參數的值。但是,Invoke-KMSDecrypt
的CiphertextBlob
參數會採用位元組陣列 (byte[]
);此參數不需要MemoryStream
物件。從 4.0 AWSPowerShell 版開始,採用字節數組和MemoryStream
對象的所有 AWSPowerShell 模塊中的參數都接受字節數組,MemoryStream
對象,字符串,字符串數組和FileInfo
(System.io。 FileInfo) 物件。您可以將這些類型中的任何一項傳遞給 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
若要使用AWS KMS PowerShell 指令程式,請安裝 AWS.Tools。 KeyManagementService
模塊。如需詳細資訊,請參閱《 使用者指南》AWS Tools for Windows PowerShell。
以不同的 AWS KMS key 重新加密資料金鑰
若要解密加密的資料金鑰,然後立即在其他金鑰下重新加密資料金鑰AWS KMS key,請使用此ReEncrypt作業。此操作完全在伺服器端的 AWS KMS 中執行,所以絕不會在 AWS KMS 外部公開您的純文字。
您指定的必須ciphertextBlob
是GenerateDataKey、GenerateDataKeyWithoutPlaintext或「加密」回應中的CiphertextBlob
欄位值,或來自或回GenerateDataKeyPairWithoutPlaintext應的PrivateKeyCiphertextBlob
GenerateDataKeyPair欄位值。您也可以使用此 ReEncrypt
操作來重新加密非對稱 KMS 金鑰中 AWS KMS 以外由公有金鑰加密的資料。
使用對稱加密 KMS 金鑰進行重新加密時,不需要 SourceKeyId
參數。AWS KMS 可以從加密文字 Blob 的中繼資料取得用來加密資料的 KMS 金鑰。但是指定您正在使用的 KMS 金鑰永遠是最佳實務。此實務可確保您使用預定的 KMS 金鑰,並防止您意外使用您不信任的 KMS 金鑰來解密加密文字。
在需要用戶端物件的語言中,這些範例會使用您在 建立用戶端 中建立的 AWS KMS 用戶端物件。
- Java
-
如需詳細資訊,請參閱《AWS SDK for Java API 參考》中的 reEncrypt 方法。
// 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#
-
如需詳細資訊,請參閱 AWS SDK for .NET 中的 ReEncrypt 方法。
// 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
-
如需詳細資訊,請參閱 AWS SDK for Python (Boto3) 中的
re_encrypt
方法。 # 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
-
如需詳細資訊,請參閱 AWS SDK for Ruby 中的
re_encrypt
執行個體方法。# 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
-
如需詳細資訊,請參閱 AWS SDK for PHP 中的 ReEncrypt 方法。
// 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
如需詳細資訊,請參閱 Node.js 中 AWS SDK 中的 reEncrypt 屬性。 JavaScript
// 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
若要重新加密相同或不同 KMS 金鑰下的密文,請使用叫用 KMS 指令程式。ReEncrypt
因為此範例會使用 AWS KMS 加密 cmdlet 傳回的加密文字,所以會使用
MemoryStream
物件做為CiphertextBlob
參數的值。但是,Invoke-KMSReEncrypt
的CiphertextBlob
參數會採用位元組陣列 (byte[]
);此參數不需要MemoryStream
物件。從 4.0 AWSPowerShell 版開始,採用字節數組和MemoryStream
對象的所有 AWSPowerShell 模塊中的參數都接受字節數組,MemoryStream
對象,字符串,字符串數組和FileInfo
(System.io。 FileInfo) 物件。您可以將這些類型中的任何一項傳遞給 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
若要使用AWS KMS PowerShell 指令程式,請安裝 AWS.Tools。 KeyManagementService
模塊。如需詳細資訊,請參閱《AWS Tools for Windows PowerShell 使用者指南》。