

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS SDK for PHP バージョン 3 を使用した AWS KMS データキーの暗号化と復号
<a name="kms-example-encrypt"></a>

データキーは、大量のデータや他のデータ暗号化キーといったデータを暗号化するための暗号化キーです。

 AWS Key Management Serviceの (AWS KMS) を使用して、データキー[AWS KMS key](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#kms_keys)を生成、暗号化、復号できます。

以下の例では、次の方法を示しています。
+ [Encrypt](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#encrypt) を使用してデータキーを暗号化する。
+ [Decrypt](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#decrypt) を使用してデータキーを復号化する。
+ [ReEncrypt](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#reencrypt) を使用して新しい KMS キーでデータキーを再暗号化します。

のすべてのサンプルコード AWS SDK for PHP は[GitHub で入手できます](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)。

## 認証情報
<a name="examplecredentials"></a>

サンプルコードを実行する前に、「」の説明に従って AWS 認証情報を設定します[AWS SDK for PHP バージョン 3 AWS を使用した での認証](credentials.md)。次に AWS SDK for PHP、「」の説明に従って をインポートします[AWS SDK for PHP バージョン 3 のインストール](getting-started_installation.md)。

 AWS Key Management Service (AWS KMS) の使用の詳細については、「 [AWS KMS デベロッパーガイド](https://docs.aws.amazon.com/kms/latest/developerguide/)」を参照してください。

## 暗号化
<a name="encrypt"></a>

[Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html) オペレーションは、データキーを暗号化するように設計されていますが、頻繁には使用されていません。[GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html) および [GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html) オペレーションは、暗号化されたデータキーを返します。暗号化されたデータを新しい AWS リージョンに移動し、その新しいリージョンで KMS キーを使用してデータキーを暗号化するときに `Encypt` メソッドを使用できます。

 **インポート** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **サンプルコード** 

```
$KmsClient = new Aws\Kms\KmsClient([
    'profile' => 'default',
    'version' => '2014-11-01',
    'region' => 'us-east-2'
]);

$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);

try {
    $result = $KmsClient->encrypt([
        'KeyId' => $keyId,
        'Plaintext' => $message,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## Decrypt
<a name="decrypt"></a>

データキーを復号するには、[Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) オペレーションを使用します。

指定する `ciphertextBlob` は、[GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html)、[GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html)、または [Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html) レスポンスからの `CiphertextBlob` フィールド値である必要があります。

 **インポート** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **サンプルコード** 

```
$KmsClient = new Aws\Kms\KmsClient([
    'profile' => 'default',
    'version' => '2014-11-01',
    'region' => 'us-east-2'
]);

$ciphertext = 'Place your cipher text blob here';

try {
    $result = $KmsClient->decrypt([
        'CiphertextBlob' => $ciphertext,
    ]);
    $plaintext = $result['Plaintext'];
    var_dump($plaintext);
} catch (AwsException $e) {
    // Output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 再暗号化
<a name="reencrypt"></a>

暗号化されたデータキーを復号し、続いてすぐに異なる KMS キーでデータキーを再暗号化するには、[ReEncrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html) オペレーションを使用します。オペレーションは内部のサーバー側で完全に実行されるため AWS KMS、プレーンテキストが外部に公開されることはありません AWS KMS。

指定する `ciphertextBlob` は、[GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html)、[GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html)、または [Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html) レスポンスからの `CiphertextBlob` フィールド値である必要があります。

 **インポート** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **サンプルコード** 

```
$KmsClient = new Aws\Kms\KmsClient([
    'profile' => 'default',
    'version' => '2014-11-01',
    'region' => 'us-east-2'
]);

$keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab';
$ciphertextBlob = 'Place your cipher text blob here';

try {
    $result = $KmsClient->reEncrypt([
        'CiphertextBlob' => $ciphertextBlob,
        'DestinationKeyId' => $keyId,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```