데이터 키 암호화 및 해독 - AWS Key Management Service

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

데이터 키 암호화 및 해독

이 항목의 예제에서는 API의 암호화, 암호 해독ReEncrypt작업을 사용합니다. AWS KMS

이 작업은 데이터 키를 암호화하고 해독하도록 설계되었습니다. 이러한 작업은 암호화 작업에 AWS KMS keys를 사용하고 4KB(4096바이트)를 초과하는 데이터를 수락할 수 없습니다. 암호나 RSA 키 등 소량의 데이터를 암호화하는 데 사용할 수 있지만 애플리케이션 데이터를 암호화하도록 설계되지 않았습니다.

애플리케이션 데이터를 암호화하려면 AWS 서비스의 서버 측 암호화 기능이나 클라이언트 측 암호화 라이브러리(예: AWS Encryption SDK 또는 Amazon S3 암호화 클라이언트)를 사용합니다.

데이터 키 암호화

암호화 작업은 데이터 키를 암호화하도록 설계되었지만 자주 사용되지 않습니다. GenerateDataKeyGenerateDataKeyWithoutPlaintext연산은 암호화된 데이터 키를 반환합니다. 암호화된 데이터를 다른 리전으로 이동하고 새 리전의 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 .NETEncrypt 메서드를 참조하십시오.

// 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 Rubyencrypt 메서드를 참조하십시오.

# 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 PHPEncrypt 메서드를 참조하십시오.

// 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를 사용합니다. 암호문을 a (System.io) 로 반환합니다. MemoryStream MemoryStream) 객체입니다. MemoryStream 객체를 Invoke-KMSDecrypt cmdlet에 대한 입력으로 사용할 수 있습니다.

또한 AWS KMS는 데이터 키를 MemoryStream 객체로 반환합니다. 이 예제에서는 일반 텍스트 데이터 키를 시뮬레이션하기 위해 바이트 배열을 만들어 MemoryStream 객체에 씁니다.

Invoke-KMSEncryptPlaintext 파라미터는 바이트 배열(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 cmdlet을 사용하려면 AWS.tools를 설치하십시오. KeyManagementService모듈. 자세한 내용은 AWS Tools for Windows PowerShell 사용 설명서를 참조하세요.

데이터 키 해독

데이터 키를 해독하려면 Decrypt 작업을 사용합니다.

ciphertextBlob지정하는 값은 a GenerateDataKeyGenerateDataKeyWithoutPlaintext, 또는 Encrypt 응답의 CiphertextBlob 필드 값이거나 또는 응답의 PrivateKeyCiphertextBlob 필드 값이어야 합니다. GenerateDataKeyPairGenerateDataKeyPairWithoutPlaintext 또한 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 .NETDecrypt 메서드를 참조하세요.

// 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 Rubydecrypt 인스턴스 메소드를 참조하세요.

# 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 PHPDecrypt 메서드를 참조하세요.

// 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 용 AWSSDK의 암호 해독 속성을 참조하십시오. 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를 사용합니다.

이 cmdlet은 일반 텍스트를 a (System.io) 로 반환합니다. MemoryStream MemoryStream) 객체입니다. 이를 바이트 배열로 변환하려면 MemoryStream 객체를 바이트 배열로 변환하는 cmdlet 또는 함수(예: Convert 모듈의 함수)를 사용합니다.

이 예제에서는 AWS KMS 암호화 cmdlet가 반환한 암호화 텍스트를 사용하기 때문에 CiphertextBlob 파라미터 값에 MemoryStream 객체를 사용합니다. 그러나 Invoke-KMSDecryptCiphertextBlob 파라미터는 바이트 배열(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 cmdlet을 사용하려면 AWS.tools를 설치하십시오. KeyManagementService모듈. 자세한 정보는 AWS Tools for Windows PowerShell 사용 설명서를 참조하세요.

다른 AWS KMS key 아래의 의 데이터 키 재암호화

암호화된 데이터 키를 해독한 다음 다른 AWS KMS key 키로 데이터 키를 즉시 다시 암호화하려면 작업을 사용하십시오. ReEncrypt 이러한 작업은 모두 AWS KMS 내부의 서버 측에서 수행되므로 AWS KMS 외부에 일반 텍스트를 노출해서는 안 됩니다.

ciphertextBlob지정하는 값은 GenerateDataKeyGenerateDataKeyWithoutPlaintext, 또는 암호화 응답의 CiphertextBlob 필드 값이거나 또는 응답의 PrivateKeyCiphertextBlob 필드 값이어야 합니다. GenerateDataKeyPairGenerateDataKeyPairWithoutPlaintext 또한 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 .NETReEncrypt 메서드를 참조하세요.

// 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 Rubyre_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 PHPReEncrypt 메서드를 참조하세요.

// 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 JavaScript SDK의 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 키로 암호문을 다시 암호화하려면 Invoke-KMS cmdlet을 사용하십시오. ReEncrypt

이 예제에서는 AWS KMS 암호화 cmdlet가 반환한 암호화 텍스트를 사용하기 때문에 CiphertextBlob 파라미터 값에 MemoryStream 객체를 사용합니다. 그러나 Invoke-KMSReEncryptCiphertextBlob 파라미터는 바이트 배열(byte[])을 사용하며 MemoryStream 객체가 필요하지 않습니다. AWSPowerShell 버전 4.0부터 바이트 배열 및 객체를 사용하는 모든 AWSPowerShell 모듈의 매개 변수는 바이트 배열, MemoryStream 객체, 문자열, 문자열 배열 및 (System.io) 를 허용합니다. MemoryStreamFileInfo 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 cmdlet을 사용하려면 AWS.tools를 설치하십시오. KeyManagementService모듈. 자세한 내용은 AWS Tools for Windows PowerShell 사용 설명서를 참조하세요.