AWS Doc SDK Examples
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
または ReEncrypt
で を使用する AWS SDK CLI
以下のコード例は、ReEncrypt
の使用方法を示しています。
- CLI
-
- AWS CLI
-
例 1: 暗号化されたメッセージを別の対称KMSキー (Linux および macOS) で再暗号化するには。
次の
re-encrypt
コマンド例は、 を使用してデータを再暗号化するための推奨方法を示しています AWS CLI。file.In の暗号文に
--ciphertext-blob
パラメータの値を指定し、fileb://
プレフィックスを使用します。プレフィックスは、バイナリファイルからデータを読み取るCLIように に指示します。ファイルが現在のディレクトリにない場合は、ファイルへのフルパスを入力します。ファイルからのパラメータ値の読み取り AWS CLIの詳細については、AWS 「 コマンドラインインターフェイスユーザーガイド」の「ファイル <https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-file.html> からのパラメータのロード AWS CLI」およびAWS 「 コマンドラインツールブログ」のhttps://aws.amazon.com/blogs/best-practices-for-local「ローカルファイルパラメータのベストプラクティス」を参照してください。暗号文を復号するソース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
ファイルにリダイレクト (>) します。このコマンドを実行する前に、サンプルキーを AWS アカウントのIDs有効なキー識別子に置き換えます。
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 詳細については、「 for AWS SDK Python (Boto3) APIリファレンスReEncrypt」の「」を参照してください。
-
- Ruby
-
- SDK 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 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 SDKRust APIリファレンスのReEncrypt
「」の「」を参照してください。
-