use of software.amazon.awssdk.services.kms.model.EncryptResponse in project aws-doc-sdk-examples by awsdocs.
the class KMSEncryptionExample method encryptData.
// Encrypt the data passed as a byte array
private static byte[] encryptData(String keyId, byte[] data) {
try {
KmsClient kmsClient = getKMSClient();
SdkBytes myBytes = SdkBytes.fromByteArray(data);
EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
EncryptResponse response = kmsClient.encrypt(encryptRequest);
String algorithm = response.encryptionAlgorithm().toString();
System.out.println("The encryption algorithm is " + algorithm);
// Return the encrypted data
SdkBytes encryptedData = response.ciphertextBlob();
return encryptedData.asByteArray();
} catch (KmsException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return null;
}
use of software.amazon.awssdk.services.kms.model.EncryptResponse in project aws-doc-sdk-examples by awsdocs.
the class PutItemEncrypt method putItemInTable.
// snippet-start:[dynamodb.java2.put_item_enc.main]
public static void putItemInTable(DynamoDbClient ddb, KmsClient kmsClient, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal, String keyId) {
HashMap<String, AttributeValue> itemValues = new HashMap<String, AttributeValue>();
// Encrypt the albumTitleValue before writing it to the table.
SdkBytes myBytes = SdkBytes.fromUtf8String(albumTitleValue);
EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
EncryptResponse response = kmsClient.encrypt(encryptRequest);
// Get the encrypted data.
SdkBytes encryptedData = response.ciphertextBlob();
// Add content to the table.
itemValues.put(key, AttributeValue.builder().s(keyVal).build());
itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
itemValues.put(albumTitle, AttributeValue.builder().bs(encryptedData).build());
itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
PutItemRequest request = PutItemRequest.builder().tableName(tableName).item(itemValues).build();
try {
ddb.putItem(request);
System.out.println(tableName + " was successfully updated");
} catch (ResourceNotFoundException e) {
System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
System.err.println("Be sure that it exists and that you've typed its name correctly!");
System.exit(1);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.kms.model.EncryptResponse in project aws-doc-sdk-examples by awsdocs.
the class EncryptDataKey method encryptData.
// snippet-start:[kms.java2_encrypt_data.main]
public static SdkBytes encryptData(KmsClient kmsClient, String keyId) {
try {
SdkBytes myBytes = SdkBytes.fromByteArray(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
EncryptResponse response = kmsClient.encrypt(encryptRequest);
String algorithm = response.encryptionAlgorithm().toString();
System.out.println("The encryption algorithm is " + algorithm);
// Get the encrypted data.
SdkBytes encryptedData = response.ciphertextBlob();
return encryptedData;
} catch (KmsException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return null;
}
Aggregations