use of com.amazonaws.services.s3.AmazonS3EncryptionV2 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]
}
use of com.amazonaws.services.s3.AmazonS3EncryptionV2 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]
}
use of com.amazonaws.services.s3.AmazonS3EncryptionV2 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]
}
Aggregations