Search in sources :

Example 1 with EncryptResponse

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;
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) KmsClient(software.amazon.awssdk.services.kms.KmsClient) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Example 2 with EncryptResponse

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);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Example 3 with EncryptResponse

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;
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Aggregations

SdkBytes (software.amazon.awssdk.core.SdkBytes)3 EncryptRequest (software.amazon.awssdk.services.kms.model.EncryptRequest)3 EncryptResponse (software.amazon.awssdk.services.kms.model.EncryptResponse)3 KmsException (software.amazon.awssdk.services.kms.model.KmsException)2 HashMap (java.util.HashMap)1 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)1 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)1 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)1 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)1 KmsClient (software.amazon.awssdk.services.kms.KmsClient)1