Amazon S3 客户端加密配合 AWS KMS 托管密钥 - AWS SDK for Java 1.x

我们宣布了即将推出 end-of-support 的 AWS SDK for Java (v1)。建议您迁移到 AWS SDK for Java v2。有关日期、其他详细信息以及如何迁移的信息,请参阅链接的公告。

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

Amazon S3 客户端加密配合 AWS KMS 托管密钥

以下示例使用 AmazonS3EncryptionClientV2Builder 类创建启用客户端加密的 Amazon S3 客户端。配置后,您使用此客户端上传到 Amazon S3 的任何对象都将加密。您使用此客户端从 Amazon S3 获取的任何对象都将自动解密。

注意

以下示例演示如何将 Amazon S3 客户端加密和 AWS 托管密钥配合使用。要了解如何将加密与您自己的密钥配合使用,请参阅 Amazon S3 客户端加密配合客户端主密钥

启用客户端 Amazon S3 加密时,您可以从两种加密模式中进行选择:经严格身份验证或经身份验证。以下部分说明了如何启用每种类型。要了解每种模式使用哪种算法,请参阅 CryptoMode 定义。

必需的导入

为这些示例导入以下类。

导入

import com.amazonaws.ClientConfiguration; import com.amazonaws.regions.Regions; import com.amazonaws.services.kms.AWSKMS; import com.amazonaws.services.kms.AWSKMSClientBuilder; import com.amazonaws.services.kms.model.GenerateDataKeyRequest; import com.amazonaws.services.kms.model.GenerateDataKeyResult; import com.amazonaws.services.s3.AmazonS3EncryptionClientV2Builder; import com.amazonaws.services.s3.AmazonS3EncryptionV2; import com.amazonaws.services.s3.model.CryptoConfigurationV2; import com.amazonaws.services.s3.model.CryptoMode; import com.amazonaws.services.s3.model.EncryptionMaterials; import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;

经严格身份验证加密

如果未指定 CryptoMode,则默认模式为经严格身份验证加密。

要显式启用此模式,请在 withCryptoConfiguration 方法中指定 StrictAuthenticatedEncryption 值。

注意

要使用客户端经身份验证加密,您必须将最新的 Bouncy Castle jar 文件加入应用程序的类路径中。

代码

AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.StrictAuthenticatedEncryption))) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build(); s3Encryption.putObject(bucket_name, ENCRYPTED_KEY3, "This is the 3rd content to encrypt with a key created in the {console}"); System.out.println(s3Encryption.getObjectAsString(bucket_name, ENCRYPTED_KEY3));

对 Amazon S3 加密客户端调用 putObject 方法以上传对象。

代码

s3Encryption.putObject(bucket_name, ENCRYPTED_KEY3, "This is the 3rd content to encrypt with a key created in the {console}");

您可以使用同一个客户端检索该对象。此示例调用 getObjectAsString 方法以检索存储的字符串。

代码

System.out.println(s3Encryption.getObjectAsString(bucket_name, ENCRYPTED_KEY3));

经身份验证加密模式

使用 AuthenticatedEncryption 模式时,在加密期间会应用改进的密钥包装算法。在此模式下解密时,该算法会验证已解密对象的完整性,如果检查失败,则引发异常。有关经身份验证加密模式工作原理的更多详细信息,请参阅博客文章 Amazon S3 Client-Side Authenticated Encryption

注意

要使用客户端经身份验证加密,您必须将最新的 Bouncy Castle jar 文件加入应用程序的类路径中。

要启用此模式,请在 withCryptoConfiguration 方法中指定 AuthenticatedEncryption 值。

代码

AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.AuthenticatedEncryption))) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build();

配置 AWS KMS 客户端

除非明确指定了 AWS KMS 客户端,否则默认情况下,Amazon S3 加密客户端会创建一个该客户端。

要为这个自动创建的 AWS KMS 客户端设置区域,请设置 awsKmsRegion

代码

Region kmsRegion = Region.getRegion(Regions.AP_NORTHEAST_1); AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withAwsKmsRegion(kmsRegion)) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build();

或者,您可以使用自己的 AWS KMS 客户端来初始化加密客户端。

代码

AWSKMS kmsClient = AWSKMSClientBuilder.standard() .withRegion(Regions.US_WEST_2); .build(); AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withKmsClient(kmsClient) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.AuthenticatedEncryption))) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build();