

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 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)할 수 있습니다.

다음 예제에서는 다음과 같은 작업을 하는 방법을 보여줍니다.
+ [암호화](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-kms-2014-11-01.html#encrypt)를 사용하여 데이터 키를 암호화합니다.
+ [암호화 해제](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 키로 데이터 키를 다시 암호화합니다.

에 대한 모든 예제 코드는 GitHub에서 확인할 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>

[암호화](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) 또는 [암호화](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) 또는 [암호화](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";
}
```