选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

ReEncrypt与 AWS SDK 或 CLI 配合使用

聚焦模式
ReEncrypt与 AWS SDK 或 CLI 配合使用 - AWS Key Management Service

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

以下代码示例演示如何使用 ReEncrypt

CLI
AWS CLI

示例 1:使用不同的对称 KMS 密钥重新加密加密消息(Linux 和 macOS)。

以下re-encrypt命令示例演示了使用 AWS CLI 重新加密数据的推荐方法。

在文件中提供加密文字。在 --ciphertext-blob 参数的值中,使用 fileb:// 前缀,它将指示 CLI 从二进制文件中读取数据。如果文件不在当前目录中,请键入文件的完整路径。有关从文件读取 AWS CLI 参数值的更多信息,请参阅AWS 命令行界面用户指南中的从文件中加载 AWS CLI 参数 < https://docs.aws.amazon.com/cli/ latest/userguide/cli-usage-parameters-file .html> 和AWS 命令行工具博客中的本地文件参数最佳实践 < dev https://aws.amazon.com/blogs/ eloper/ best-practices-for-local-file-parameters/>。指定源 KMS 密钥,用于解密密文。使用对称加密 KMS 密钥解密时不需要参数。--source-key-id AWS KMS 可以从密文 blob 中的元数据中获取用于加密数据的 KMS 密钥。但是,指定您正在使用的 KMS 密钥始终是最佳实践。此做法可确保您使用预期的 KMS 密钥,并防止您意外使用不信任的 KMS 密钥解密加密文字。指定目标 KMS 密钥来重新加密数据。--destination-key-id 参数始终为必需项。此示例使用密钥 ARN,但您可以使用任何有效的密钥标识符。将明文输出请求为文本值。--query 参数指示 CLI 仅从输出中获取 Plaintext 字段的值。--output 参数以 text.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 解码并保存在文件中。

有关更多信息,请参阅《AWS 密钥管理服务 API 参考》中的 ReEncrypt < https://docs.aws.amazon.com/kms/ latest/APIReference/API _ ReEncrypt .html。

示例 2:使用不同的对称 KMS 密钥重新加密加密消息(Windows 命令提示符)。

以下 re-encrypt 命令示例与前一个示例相同,不同之处在于,它使用 certutil 实用工具对明文数据进行 Base64 解码。此过程需要两个命令,如以下示例所示。

在运行此命令之前,请将示例密钥 ID 替换为 AWS 账户中的有效密钥 ID。

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.

有关更多信息,请参阅《AWS 密钥管理服务 API 参考》中的 ReEncrypt < https://docs.aws.amazon.com/kms/ latest/APIReference/API _ ReEncrypt .html。

  • 有关 API 的详细信息,请参阅AWS CLI 命令参考ReEncrypt中的。

Python
适用于 Python 的 SDK(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 的详细信息,请参阅适用ReEncryptPython 的AWS SDK (Boto3) API 参考

Ruby
适用于 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 的详细信息,请参阅 AWS SDK for Ruby API 参考ReEncrypt中的。

Rust
适用于 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 的详细信息,请参阅适用ReEncryptRust 的AWS SDK API 参考

AWS CLI

示例 1:使用不同的对称 KMS 密钥重新加密加密消息(Linux 和 macOS)。

以下re-encrypt命令示例演示了使用 AWS CLI 重新加密数据的推荐方法。

在文件中提供加密文字。在 --ciphertext-blob 参数的值中,使用 fileb:// 前缀,它将指示 CLI 从二进制文件中读取数据。如果文件不在当前目录中,请键入文件的完整路径。有关从文件读取 AWS CLI 参数值的更多信息,请参阅AWS 命令行界面用户指南中的从文件中加载 AWS CLI 参数 < https://docs.aws.amazon.com/cli/ latest/userguide/cli-usage-parameters-file .html> 和AWS 命令行工具博客中的本地文件参数最佳实践 < dev https://aws.amazon.com/blogs/ eloper/ best-practices-for-local-file-parameters/>。指定源 KMS 密钥,用于解密密文。使用对称加密 KMS 密钥解密时不需要参数。--source-key-id AWS KMS 可以从密文 blob 中的元数据中获取用于加密数据的 KMS 密钥。但是,指定您正在使用的 KMS 密钥始终是最佳实践。此做法可确保您使用预期的 KMS 密钥,并防止您意外使用不信任的 KMS 密钥解密加密文字。指定目标 KMS 密钥来重新加密数据。--destination-key-id 参数始终为必需项。此示例使用密钥 ARN,但您可以使用任何有效的密钥标识符。将明文输出请求为文本值。--query 参数指示 CLI 仅从输出中获取 Plaintext 字段的值。--output 参数以 text.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 解码并保存在文件中。

有关更多信息,请参阅《AWS 密钥管理服务 API 参考》中的 ReEncrypt < https://docs.aws.amazon.com/kms/ latest/APIReference/API _ ReEncrypt .html。

示例 2:使用不同的对称 KMS 密钥重新加密加密消息(Windows 命令提示符)。

以下 re-encrypt 命令示例与前一个示例相同,不同之处在于,它使用 certutil 实用工具对明文数据进行 Base64 解码。此过程需要两个命令,如以下示例所示。

在运行此命令之前,请将示例密钥 ID 替换为 AWS 账户中的有效密钥 ID。

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.

有关更多信息,请参阅《AWS 密钥管理服务 API 参考》中的 ReEncrypt < https://docs.aws.amazon.com/kms/ latest/APIReference/API _ ReEncrypt .html。

  • 有关 API 的详细信息,请参阅AWS CLI 命令参考ReEncrypt中的。

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将此服务与 AWS SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。

下一主题:

RetireGrant

上一主题:

PutKeyPolicy
隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。