Search in sources :

Example 1 with CryptoConfigurationV2

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

the class S3EncryptV2 method putEncryptedData3_Kms.

public static void putEncryptedData3_Kms() {
    // snippet-start:[s3.java.s3_cse-v2.kms]
    AWSKMS kmsClient = AWSKMSClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    // create CMK for for testing this example
    CreateKeyRequest createKeyRequest = new CreateKeyRequest();
    CreateKeyResult createKeyResult = kmsClient.createKey(createKeyRequest);
    // specify an Amazon KMS customer master key (CMK) ID
    String keyId = createKeyResult.getKeyMetadata().getKeyId();
    String s3ObjectKey = "EncryptedContent3.txt";
    String s3ObjectContent = "This is the 3rd content to encrypt";
    AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard().withRegion(Regions.US_WEST_2).withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode(CryptoMode.StrictAuthenticatedEncryption)).withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)).build();
    s3Encryption.putObject(bucketName, s3ObjectKey, s3ObjectContent);
    System.out.println(s3Encryption.getObjectAsString(bucketName, s3ObjectKey));
    // schedule deletion of CMK generated for testing
    ScheduleKeyDeletionRequest scheduleKeyDeletionRequest = new ScheduleKeyDeletionRequest().withKeyId(keyId).withPendingWindowInDays(7);
    kmsClient.scheduleKeyDeletion(scheduleKeyDeletionRequest);
    s3Encryption.shutdown();
    kmsClient.shutdown();
// snippet-end:[s3.java.s3_cse-v2.kms]
}
Also used : AmazonS3EncryptionV2(com.amazonaws.services.s3.AmazonS3EncryptionV2) ScheduleKeyDeletionRequest(com.amazonaws.services.kms.model.ScheduleKeyDeletionRequest) CreateKeyResult(com.amazonaws.services.kms.model.CreateKeyResult) KMSEncryptionMaterialsProvider(com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider) CreateKeyRequest(com.amazonaws.services.kms.model.CreateKeyRequest) CryptoConfigurationV2(com.amazonaws.services.s3.model.CryptoConfigurationV2) AWSKMS(com.amazonaws.services.kms.AWSKMS)

Example 2 with CryptoConfigurationV2

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

the class S3EncryptV2 method putEncryptedData2.

public static void putEncryptedData2() throws NoSuchAlgorithmException {
    // snippet-start:[s3.java.s3_cse_v2.asymmetric]
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(2048);
    // generate an asymmetric key pair for testing
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // snippet-start:[s3.java.s3_cse_v2.auth]
    String s3ObjectKey = "EncryptedContent2.txt";
    String s3ObjectContent = "This is the 2nd content to encrypt";
    AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard().withRegion(Regions.US_WEST_2).withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode(CryptoMode.StrictAuthenticatedEncryption)).withEncryptionMaterialsProvider(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(keyPair))).build();
    s3Encryption.putObject(bucketName, s3ObjectKey, s3ObjectContent);
    // snippet-end:[s3.java.s3_cse_v2.auth]
    System.out.println(s3Encryption.getObjectAsString(bucketName, s3ObjectKey));
    s3Encryption.shutdown();
// snippet-end:[s3.java.s3_cse_v2.asymmetric]
}
Also used : AmazonS3EncryptionV2(com.amazonaws.services.s3.AmazonS3EncryptionV2) KeyPair(java.security.KeyPair) EncryptionMaterials(com.amazonaws.services.s3.model.EncryptionMaterials) CryptoConfigurationV2(com.amazonaws.services.s3.model.CryptoConfigurationV2) KeyPairGenerator(java.security.KeyPairGenerator) StaticEncryptionMaterialsProvider(com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider)

Example 3 with CryptoConfigurationV2

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

the class S3EncryptV2 method putEncryptedData1.

public static void putEncryptedData1() throws NoSuchAlgorithmException {
    // snippet-start:[s3.java.s3_cse_v2.symmetric]
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(256);
    // generate a symmetric encryption key for testing
    SecretKey secretKey = keyGenerator.generateKey();
    // snippet-start:[s3.java.s3_cse_v2.strictauth]
    String s3ObjectKey = "EncryptedContent1.txt";
    String s3ObjectContent = "This is the 1st content to encrypt";
    AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard().withRegion(Regions.DEFAULT_REGION).withClientConfiguration(new ClientConfiguration()).withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode(CryptoMode.StrictAuthenticatedEncryption)).withEncryptionMaterialsProvider(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(secretKey))).build();
    s3Encryption.putObject(bucketName, s3ObjectKey, s3ObjectContent);
    // snippet-end:[s3.java.s3_cse_v2.strictauth]
    System.out.println(s3Encryption.getObjectAsString(bucketName, s3ObjectKey));
    s3Encryption.shutdown();
// snippet-end:[s3.java.s3_cse_v2.symmetric]
}
Also used : AmazonS3EncryptionV2(com.amazonaws.services.s3.AmazonS3EncryptionV2) SecretKey(javax.crypto.SecretKey) EncryptionMaterials(com.amazonaws.services.s3.model.EncryptionMaterials) CryptoConfigurationV2(com.amazonaws.services.s3.model.CryptoConfigurationV2) StaticEncryptionMaterialsProvider(com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider) KeyGenerator(javax.crypto.KeyGenerator) ClientConfiguration(com.amazonaws.ClientConfiguration)

Aggregations

AmazonS3EncryptionV2 (com.amazonaws.services.s3.AmazonS3EncryptionV2)3 CryptoConfigurationV2 (com.amazonaws.services.s3.model.CryptoConfigurationV2)3 EncryptionMaterials (com.amazonaws.services.s3.model.EncryptionMaterials)2 StaticEncryptionMaterialsProvider (com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSKMS (com.amazonaws.services.kms.AWSKMS)1 CreateKeyRequest (com.amazonaws.services.kms.model.CreateKeyRequest)1 CreateKeyResult (com.amazonaws.services.kms.model.CreateKeyResult)1 ScheduleKeyDeletionRequest (com.amazonaws.services.kms.model.ScheduleKeyDeletionRequest)1 KMSEncryptionMaterialsProvider (com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 KeyGenerator (javax.crypto.KeyGenerator)1 SecretKey (javax.crypto.SecretKey)1