Amazon S3 客户端加密配合客户端主密钥 - Amazon SDK for Java 1.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

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

Amazon S3 客户端加密配合客户端主密钥

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

注意

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

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

必需的导入

为这些示例导入以下类。

导入

import com.amazonaws.ClientConfiguration; import com.amazonaws.regions.Regions; 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.StaticEncryptionMaterialsProvider;

经严格身份验证加密

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

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

注意

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

代码

AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.StrictAuthenticatedEncryption))) .withEncryptionMaterialsProvider(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(secretKey))) .build(); s3Encryption.putObject(bucket_name, ENCRYPTED_KEY2, "This is the 2nd content to encrypt");

经身份验证加密模式

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

注意

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

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

代码

AmazonS3EncryptionV2 s3EncryptionClientV2 = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.DEFAULT_REGION) .withClientConfiguration(new ClientConfiguration()) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode(CryptoMode.AuthenticatedEncryption)) .withEncryptionMaterialsProvider(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(secretKey))) .build(); s3EncryptionClientV2.putObject(bucket_name, ENCRYPTED_KEY1, "This is the 1st content to encrypt");