Search in sources :

Example 11 with StaticEncryptionMaterialsProvider

use of com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider in project aws-doc-sdk-examples by awsdocs.

the class S3Encrypt method authenticatedEncryption_CustomerManagedAsymmetricKey.

/**
 * Same as {@link #authenticatedEncryption_CustomerManagedKey()} except uses an asymmetric key pair and
 * RSA/ECB/OAEPWithSHA-256AndMGF1Padding as the key wrapping algorithm.
 */
public void authenticatedEncryption_CustomerManagedAsymmetricKey() throws NoSuchAlgorithmException {
    KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder.standard().withRegion(Regions.US_WEST_2).withCryptoConfiguration(new CryptoConfiguration(CryptoMode.AuthenticatedEncryption)).withEncryptionMaterials(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(keyPair))).build();
    AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
    s3NonEncrypt.putObject(BUCKET_NAME, NON_ENCRYPTED_KEY, "some other contents");
    System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, ENCRYPTED_KEY));
    System.out.println(s3Encryption.getObjectAsString(BUCKET_NAME, NON_ENCRYPTED_KEY));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) KeyPair(java.security.KeyPair) AmazonS3Encryption(com.amazonaws.services.s3.AmazonS3Encryption)

Example 12 with StaticEncryptionMaterialsProvider

use of com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider in project aws-doc-sdk-examples by awsdocs.

the class S3Encrypt method encryptionOnly_RangeGet_CustomerManagedKey.

/**
 * Non-authenticated encryption schemes can do range GETs without an issue.
 */
// snippet-start:[s3.java1.s3_encrypt.encryption_only]
public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException {
    SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
    AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder.standard().withRegion(Regions.US_WEST_2).withCryptoConfiguration(new CryptoConfiguration(CryptoMode.EncryptionOnly)).withEncryptionMaterials(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(secretKey))).build();
    s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents");
    System.out.println(s3Encryption.getObject(new GetObjectRequest(BUCKET_NAME, ENCRYPTED_KEY).withRange(0, 2)));
}
Also used : SecretKey(javax.crypto.SecretKey) AmazonS3Encryption(com.amazonaws.services.s3.AmazonS3Encryption)

Aggregations

AmazonS3 (com.amazonaws.services.s3.AmazonS3)8 AmazonS3Encryption (com.amazonaws.services.s3.AmazonS3Encryption)8 SecretKey (javax.crypto.SecretKey)8 KeyPair (java.security.KeyPair)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 SdkClientException (com.amazonaws.SdkClientException)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 Regions (com.amazonaws.regions.Regions)2 AmazonS3EncryptionV2 (com.amazonaws.services.s3.AmazonS3EncryptionV2)2 CryptoConfigurationV2 (com.amazonaws.services.s3.model.CryptoConfigurationV2)2 EncryptionMaterials (com.amazonaws.services.s3.model.EncryptionMaterials)2 StaticEncryptionMaterialsProvider (com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider)2 KeyGenerator (javax.crypto.KeyGenerator)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 KeyPairGenerator (java.security.KeyPairGenerator)1