I want to take encryption keys as input from the user
I does not try your above need but I have generated 256bit Secret key to encrypt and decrypt my data in S3.
// Code To Generate Secret Key.
KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
symKeyGenerator.init(256);
SecretKey symKey = symKeyGenerator.generateKey();
System.out.println(new String(Base64.encodeBase64(symKey.getEncoded())));
And I used like this to Download and upload objects in S3
// Code To Make Objects Encrypt while uploading and Decrypt while Downloading.
public static void s3WithEncryption(AWSCredentials credentials) {
String myKeyPair = "KEY_GENERATED_USING_ABOVE_CODE";
SecretKey mySymmetricKey = new SecretKeySpec(Base64.decodeBase64(myKeyPair.getBytes()), "AES");
EncryptionMaterials materials = new EncryptionMaterials(mySymmetricKey);
AmazonS3Client encryptedS3 = new AmazonS3EncryptionClient(credentials, materials);
try {
File file = new File("D:/dummy.txt");
SSECustomerKey sseKey = new SSECustomerKey(myKeyPair);
PutObjectRequest objectRequest = new PutObjectRequest(bucketName, "withEncrypt/dummy.txt", file);
encryptedS3.putObject(objectRequest.withSSECustomerKey(sseKey));
System.out.println("s3WithEncryption: Object uploaded!!!");
S3Object downloadedObject = encryptedS3.getObject(new GetObjectRequest(bucketName, "withEncrypt/" + file.getName()).withSSECustomerKey(sseKey));
downloadFile("D:/withEncrption", downloadedObject.getObjectContent(), "Steps to configure unifiedUI.txt");
System.out.println("s3WithEncryption: Object Downloaded!!!");
} catch (Exception e) {
e.printStackTrace();
} finally {
encryptedS3.shutdown();
}
}