翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
データキーの暗号化と復号
このトピックの例では、 AWS KMS API の Encrypt 、DecryptReEncrypt 、および オペレーションを使用します。
これらのオペレーションは、データキーを暗号化および復号するように設計されています。暗号化オペレーションでは AWS KMS keys を使用しますが、4 KB (4,096 バイト) を超えるデータを受け付けることはできません。パスワードや RSA キーなどの少量データを暗号化するためにこれを使用できますが、アプリケーションデータを暗号化するために設計されていません。
アプリケーションデータを暗号化するには、AWS サービスのサーバー側の暗号化機能、またはクライアント側の暗号化ライブラリを使用します (AWS Encryption SDK や Amazon S3 暗号化クライアントなど)。
データキーの暗号化
Encrypt オペレーションは、データキーを暗号化するように設計されていますが、頻繁に使用されていません。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
詳細については、 AWS SDK for in JavaScript Node.js の encrypt プロパティを参照してください。
// 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 コマンドレットを使用します。暗号文を
MemoryStream
(System.IO) オブジェクトとして返します。MemoryStreamこの MemoryStream
オブジェクトは Invoke-KMSDecrypt コマンドレットへの入力として使用できます。AWS KMS もまた、データキーを
MemoryStream
オブジェクトとして返します。この例では、プレーンテキストのデータキーをシミュレートするために、バイト配列を作成してMemoryStream
オブジェクトに書き込みます。Invoke-KMSEncrypt
のPlaintext
パラメータは、バイト配列 (byte[]
) を受けとります。MemoryStream
オブジェクトは必要ありません。 AWSPowerShell バージョン 4.0 以降、バイト配列と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または Encrypt レスポンスの CiphertextBlob
フィールドの値、または GenerateDataKeyPairまたは GenerateDataKeyPairWithoutPlaintextレスポンスの PrivateKeyCiphertextBlob
フィールドである必要があります。また、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
詳細については、 AWS SDK for in JavaScript Node.js の decrypt プロパティを参照してください。
// 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 コマンドレットを使用します。
このコマンドレットは、プレーンテキストを
MemoryStream
(System.IO) オブジェクトとして返します。MemoryStreamプレーンテキストをバイト配列に変換するには、コマンドレットまたはバイト配列を MemoryStream
オブジェクトに変換する関数 (変換モジュールの関数など) を使用します。 この例では、AWS KMS 暗号化コマンドレットが返す暗号テキストを使用するため、
CiphertextBlob
パラメータの値にはMemoryStream
オブジェクトを使用します。ただし、Invoke-KMSDecrypt
のCiphertextBlob
パラメータはバイト配列 (byte[]
) を受けとります。MemoryStream
オブジェクトは必要ありません。 AWSPowerShell バージョン 4.0 以降、バイト配列と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または Encrypt レスポンスの CiphertextBlob
フィールドの値、または GenerateDataKeyPairまたは GenerateDataKeyPairWithoutPlaintextレスポンスの PrivateKeyCiphertextBlob
フィールドである必要があります。また、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
詳細については、 AWS SDK for JavaScript in Node.js の reEncrypt プロパティを参照してください。
// 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 キーで暗号文を再暗号化するには、Invoke-KMSReEncrypt コマンドレットを使用します。
この例では、AWS KMS 暗号化コマンドレットが返す暗号テキストを使用するため、
CiphertextBlob
パラメータの値にはMemoryStream
オブジェクトを使用します。ただし、Invoke-KMSReEncrypt
のCiphertextBlob
パラメータはバイト配列 (byte[]
) を受けとります。MemoryStream
オブジェクトは必要ありません。 AWSPowerShell バージョン 4.0 以降、バイト配列と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 ユーザーガイド」を参照してください。