다음과 ReEncrypt 함께 사용하십시오. AWS SDK또는 CLI - AWS Key Management Service

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

다음과 ReEncrypt 함께 사용하십시오. AWS SDK또는 CLI

다음 코드 예제는 ReEncrypt의 사용 방법을 보여 줍니다.

작업 예제는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.

CLI
AWS CLI

예 1: 암호화된 메시지를 다른 대칭 KMS 키 (Linux 및 macOS) 로 다시 암호화하는 방법.

다음 re-encrypt 명령 예제는 다음을 사용하여 데이터를 다시 암호화하는 권장 방법을 보여줍니다. AWS CLI.

파일에 암호문을 입력합니다. --ciphertext-blob 매개 변수 값에는 이진 파일에서 데이터를 읽도록 지시하는 fileb:// 접두사를 사용합니다. CLI 파일이 현재 디렉터리에 없는 경우 파일의 전체 경로를 입력합니다. 읽기에 대한 자세한 내용은 AWS CLI파일의 매개변수 값은 로드를 참조하십시오. AWS CLI파일의 매개 변수는 < https://docs.aws.amazon.com/cli/ 최신/사용 cli-usage-parameters-file 설명서/ .html>에 있습니다. AWS 명령줄 인터페이스 사용 설명서 및 로컬 파일 매개 변수에 대한 모범 사례< 개발자/ -file-parameters/> https://aws.amazon.com/blogs/ best-practices-for-local AWS 명령줄 도구 블로그. 암호문을 해독하는 소스 KMS 키를 지정합니다. 대칭 암호화 키를 사용하여 암호를 해독할 때는 이 매개 변수가 필요하지 않습니다. --source-key-id KMS AWS KMS암호문 Blob의 메타데이터에서 KMS 데이터를 암호화하는 데 사용된 키를 가져올 수 있습니다. 하지만 항상 사용 중인 키를 지정하는 것이 가장 좋습니다. KMS 이렇게 하면 의도한 KMS 키를 사용하고 신뢰할 수 없는 키를 사용하여 실수로 암호문을 해독하는 것을 방지할 수 있습니다.데이터를 다시 암호화하는 대상 KMS 키를 지정하십시오.매개 변수는 항상 필요합니다. KMS --destination-key-id 이 예제에서는 키를 ARN 사용하지만 모든 유효한 키 식별자를 사용할 수 있습니다.일반 텍스트 출력을 텍스트 값으로 요청합니다. --query 매개변수는 출력에서 Plaintext 필드 값만 CLI 가져오도록 지시합니다. --output 파라미터는 출력을 텍스트로 반환합니다. 일반 텍스트를 Base64로 디코딩하여 파일에 저장합니다. 다음 예시에서는 Plaintext 파라미터 값을 Base64 유틸리티에 파이프(|)로 구분하며 유틸리티가 이를 디코딩합니다. 그런 다음 디코딩된 출력을 ExamplePlaintext 파일로 리디렉션(>)합니다.

이 명령을 실행하기 전에 예제 키를 사용 중인 유효한 키 식별자로 바꾸십시오. IDs AWS 계정.

aws kms re-encrypt \ --ciphertext-blob fileb://ExampleEncryptedFile \ --source-key-id 1234abcd-12ab-34cd-56ef-1234567890ab \ --destination-key-id 0987dcba-09fe-87dc-65ba-ab0987654321 \ --query CiphertextBlob \ --output text | base64 --decode > ExampleReEncryptedFile

이 명령은 출력을 생성하지 않습니다. re-encrypt 명령의 출력은 base64로 디코딩되어 파일에 저장됩니다.

자세한 내용은 의 ReEncrypt < https://docs.aws.amazon.com/kms/ APIReference 최신/ API_R eEncrypt /.html을 참조하십시오. AWS 키 관리 서비스 레퍼런스. API

예 2: 다른 대칭 KMS 키로 암호화된 메시지를 다시 암호화하는 방법 (Windows 명령 프롬프트).

다음 re-encrypt 명령 예시는 certutil 유틸리티를 사용하여 일반 텍스트 데이터를 base64로 디코딩한다는 점을 제외하면 이전 예시와 동일합니다. 이 프로시저에는 다음 예시와 같이 두 개의 명령이 필요합니다.

이 명령을 실행하기 전에 예제 키 ID를 사용자의 유효한 키 ID로 바꾸십시오. AWS 계정.

aws kms re-encrypt ^ --ciphertext-blob fileb://ExampleEncryptedFile ^ --source-key-id 1234abcd-12ab-34cd-56ef-1234567890ab ^ --destination-key-id 0987dcba-09fe-87dc-65ba-ab0987654321 ^ --query CiphertextBlob ^ --output text > ExampleReEncryptedFile.base64

그런 다음 certutil 유틸리티를 사용하세요.

certutil -decode ExamplePlaintextFile.base64 ExamplePlaintextFile

출력:

Input Length = 18 Output Length = 12 CertUtil: -decode command completed successfully.

자세한 내용은 의 ReEncrypt < https://docs.aws.amazon.com/kms/ APIReference 최신/ API_R eEncrypt /.html을 참조하십시오. AWS 키 관리 서비스 레퍼런스. API

  • 자세한 API 내용은 ReEncrypt을 참조하십시오. AWS CLI 명령 참조.

Python
SDK파이썬용 (보토3)
참고

더 많은 정보가 있습니다. GitHub 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

class KeyEncrypt: def __init__(self, kms_client): self.kms_client = kms_client def re_encrypt(self, source_key_id, cipher_text): """ Takes ciphertext previously encrypted with one key and reencrypt it by using another key. :param source_key_id: The ARN or ID of the original key used to encrypt the ciphertext. :param cipher_text: The encrypted ciphertext. :return: The ciphertext encrypted by the second key. """ destination_key_id = input( f"Your ciphertext is currently encrypted with key {source_key_id}. " f"Enter another key ID or ARN to reencrypt it: " ) if destination_key_id != "": try: cipher_text = self.kms_client.re_encrypt( SourceKeyId=source_key_id, DestinationKeyId=destination_key_id, CiphertextBlob=cipher_text, )["CiphertextBlob"] except ClientError as err: logger.error( "Couldn't reencrypt your ciphertext. Here's why: %s", err.response["Error"]["Message"], ) else: print(f"Reencrypted your ciphertext as: {cipher_text}") return cipher_text else: print("Skipping reencryption demo.")
  • 자세한 API 내용은 ReEncrypt을 참조하십시오. AWS SDK파이썬 (Boto3) API 참조용.

Ruby
SDK루비의 경우
참고

더 많은 정보가 있습니다. GitHub 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

require "aws-sdk-kms" # v2: require 'aws-sdk' # Human-readable version of the ciphertext of the data to reencrypt. blob = "01020200785d68faeec386af1057904926253051eb2919d3c16078badf65b808b26dd057c101747cadf3593596e093d4ffbf22434a6d00000068306606092a864886f70d010706a0593057020100305206092a864886f70d010701301e060960864801650304012e3011040c9d629e573683972cdb7d94b30201108025b20b060591b02ca0deb0fbdfc2f86c8bfcb265947739851ad56f3adce91eba87c59691a9a1" sourceCiphertextBlob = [blob].pack("H*") # Replace the fictitious key ARN with a valid key ID destinationKeyId = "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321" client = Aws::KMS::Client.new(region: "us-west-2") resp = client.re_encrypt({ ciphertext_blob: sourceCiphertextBlob, destination_key_id: destinationKeyId }) # Display a readable version of the resulting re-encrypted blob. puts "Blob:" puts resp.ciphertext_blob.unpack("H*")
  • 자세한 API 내용은 ReEncrypt을 참조하십시오. AWS SDK for Ruby API참조.

Rust
SDK러스트의 경우
참고

더 많은 정보가 있습니다 GitHub. 전체 예제를 찾아 설치 및 실행 방법을 알아보십시오. AWS 코드 예제 리포지토리.

async fn reencrypt_string( verbose: bool, client: &Client, input_file: &str, output_file: &str, first_key: &str, new_key: &str, ) -> Result<(), Error> { // Get blob from input file // Open input text file and get contents as a string // input is a base-64 encoded string, so decode it: let data = fs::read_to_string(input_file) .map(|input_file| base64::decode(input_file).expect("invalid base 64")) .map(Blob::new); let resp = client .re_encrypt() .ciphertext_blob(data.unwrap()) .source_key_id(first_key) .destination_key_id(new_key) .send() .await?; // Did we get an encrypted blob? let blob = resp.ciphertext_blob.expect("Could not get encrypted text"); let bytes = blob.as_ref(); let s = base64::encode(bytes); let o = &output_file; let mut ofile = File::create(o).expect("unable to create file"); ofile.write_all(s.as_bytes()).expect("unable to write"); if verbose { println!("Wrote the following to {}:", output_file); println!("{}", s); } else { println!("Wrote base64-encoded output to {}", output_file); } Ok(()) }
  • 자세한 API 내용은 ReEncrypt을 참조하십시오. AWS SDKRust API 레퍼런스에 대해서는.

전체 목록은 다음과 같습니다. AWS SDK개발자 가이드 및 코드 예제는 을 참조하십시오사용 AWS KMS 와 함께 AWS SDK. 이 항목에는 시작에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.