翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS SDK または CLI ReEncrypt
で を使用する
以下のコード例は、ReEncrypt
の使用方法を示しています。
- CLI
-
- AWS CLI
-
例 1: 異なる対称 KMS キーで暗号化されたメッセージを再暗号化するには (Linux と macOS)
次の
re-encrypt
コマンド例は、 CLI AWS を使用してデータを再暗号化するための推奨方法を示しています。ファイルに暗号文を指定します。
--ciphertext-blob
パラメータの値には、バイナリファイルからデータを読み取るように CLI に指示するfileb://
プレフィックスを使用します。ファイルが現在のディレクトリにない場合は、ファイルへのフルパスを入力します。ファイルから CLI AWS パラメータ値を読み取る方法の詳細については、「」を参照してください。 AWS 「 コマンドラインインターフェイスユーザーガイド」の「ファイル <https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-file.html> からの AWS CLI パラメータのロード」およびAWS 「 コマンドラインツールブログ」の「ローカルファイルパラメータのベストプラクティス<https://aws.amazon.com/blogs/developer/best-practices-for-local-file-parameters/>」を参照してください。ソース KMS キーを指定します。 は暗号文を復号します。対称暗号化 KMS キーで復号する場合、--source-key-id
パラメータは必要ありません。 AWS KMS は、暗号文 BLOB のメタデータからデータを暗号化するために使用された KMS キーを取得できます。ただし、ベストプラクティスは常に、使用している KMS キーを指定することです。この方法により、意図した KMS キーを使用することができ、信頼できない KMS キーを使用して誤って暗号文を復号するのを防ぐことができます。送信先の KMS キーを指定して、データを再暗号化します。--destination-key-id
パラメータは常に必須です。この例ではキー ARN を使用していますが、有効である限り任意のキー識別子を使用できます。プレーンテキストの出力をテキスト値としてリクエストします。--query
パラメータは、出力からPlaintext
フィールドの値のみを取得するよう CLI に命令します。--output
パラメータは出力を text.Base64 でデコードしたプレーンテキストとして返し、ファイルに保存します。次の例では、Plaintext
パラメータの値を Base64 ユーティリティにパイプ (|) して、Base64 ユーティリティでデコードします。次に、デコードされた出力をExamplePlaintext
ファイルにリダイレクト (>) します。このコマンドを実行する前に、サンプルキー IDsを AWS アカウントの有効なキー識別子に置き換えます。
aws kms re-encrypt \ --ciphertext-blob
fileb://ExampleEncryptedFile
\ --source-key-id1234abcd-12ab-34cd-56ef-1234567890ab
\ --destination-key-id0987dcba-09fe-87dc-65ba-ab0987654321
\ --queryCiphertextBlob
\ --outputtext
|
base64
--decode>
ExampleReEncryptedFile
このコマンドでは何も出力されません。
re-encrypt
コマンドからの出力は base64 でデコードされ、ファイルに保存されます。詳細については、「AWS Key Management Service API リファレンス」の「ReEncrypt」<https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html> を参照してください。
例 2: 異なる対称 KMS キーで暗号化されたメッセージを再暗号化するには (Windows コマンドプロンプト)
次の
re-encrypt
の例は、certutil
ユーティリティを使用してプレーンテキストデータを Base64 でデコードする点を除いて、前の例と同じです。この手順には、次の例に示すように 2 つのコマンドが必要です。このコマンドを実行する前に、サンプルキー ID を AWS アカウントの有効なキー ID に置き換えます。
aws kms re-encrypt
^
--ciphertext-blobfileb://ExampleEncryptedFile
^
--source-key-id1234abcd-12ab-34cd-56ef-1234567890ab
^
--destination-key-id0987dcba-09fe-87dc-65ba-ab0987654321
^
--queryCiphertextBlob
^
--outputtext
>
ExampleReEncryptedFile.base64
その後、
certutil
ユーティリティーを使用します。certutil -decode ExamplePlaintextFile.base64 ExamplePlaintextFile
出力:
Input Length = 18 Output Length = 12 CertUtil: -decode command completed successfully.
詳細については、「AWS Key Management Service API リファレンス」の「ReEncrypt」<https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html> を参照してください。
-
API の詳細については、AWS CLI コマンドリファレンスの「ReEncrypt
」を参照してください。
-
- Python
-
- SDK for Python (Boto3)
-
注記
GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 class KeyEncrypt: def __init__(self, kms_client): self.kms_client = kms_client @classmethod def from_client(cls) -> "KeyEncrypt": """ Creates a KeyEncrypt instance with a default KMS client. :return: An instance of KeyEncrypt initialized with the default KMS client. """ kms_client = boto3.client("kms") return cls(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 の詳細については、AWS SDK for Python (Boto3) API リファレンスの「ReEncrypt」を参照してください。
-
- Ruby
-
- SDK for Ruby
-
注記
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 の詳細については、「AWS SDK for Ruby API リファレンス」の「ReEncrypt」を参照してください。
-
- Rust
-
- SDK for Rust
-
注記
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 の詳細については、AWS SDK for Rust API リファレンスの「ReEncrypt
」を参照してください。
-
AWS SDK 開発者ガイドとコード例の完全なリストについては、「」を参照してくださいAWS SDK でこのサービスを使用する。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。