Search in sources :

Example 1 with KmsClient

use of software.amazon.awssdk.services.kms.KmsClient 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 KmsClient

use of software.amazon.awssdk.services.kms.KmsClient in project aws-doc-sdk-examples by awsdocs.

the class KMSEncryptionExample method decryptData.

// Decrypt the data passed as a byte array
private static byte[] decryptData(byte[] data, String keyId) {
    try {
        KmsClient kmsClient = getKMSClient();
        SdkBytes encryptedData = SdkBytes.fromByteArray(data);
        DecryptRequest decryptRequest = DecryptRequest.builder().ciphertextBlob(encryptedData).keyId(keyId).build();
        DecryptResponse decryptResponse = kmsClient.decrypt(decryptRequest);
        SdkBytes plainText = decryptResponse.plaintext();
        return plainText.asByteArray();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return null;
}
Also used : SdkBytes(software.amazon.awssdk.core.SdkBytes) DecryptResponse(software.amazon.awssdk.services.kms.model.DecryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) KmsClient(software.amazon.awssdk.services.kms.KmsClient) DecryptRequest(software.amazon.awssdk.services.kms.model.DecryptRequest)

Example 3 with KmsClient

use of software.amazon.awssdk.services.kms.KmsClient in project aws-doc-sdk-examples by awsdocs.

the class PutItemEncrypt method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    PutItem <tableName> <key> <keyVal> <albumtitle> <albumtitleval> <awards> <awardsval> <Songtitle> <songtitleval>\n\n" + "Where:\n" + "    tableName - the Amazon DynamoDB table in which an item is placed (for example, Music3).\n" + "    key - the key used in the Amazon DynamoDB table (for example, Artist).\n" + "    keyval - the key value that represents the item to get (for example, Famous Band).\n" + "    albumTitle - album title (for example, AlbumTitle).\n" + "    AlbumTitleValue - the name of the album (for example, Songs About Life ).\n" + "    Awards - the awards column (for example, Awards).\n" + "    AwardVal - the value of the awards (for example, 10).\n" + "    SongTitle - the song title (for example, SongTitle).\n" + "    SongTitleVal - the value of the song title (for example, Happy Day).\n" + "    keyId - a KMS key id value to use to encrypt/decrypt the data (for example, xxxxxbcd-12ab-34cd-56ef-1234567890ab).";
    if (args.length != 10) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String tableName = args[0];
    String key = args[1];
    String keyVal = args[2];
    String albumTitle = args[3];
    String albumTitleValue = args[4];
    String awards = args[5];
    String awardVal = args[6];
    String songTitle = args[7];
    String songTitleVal = args[8];
    String keyId = args[9];
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a KmsClient object to use to encrpt data
    KmsClient kmsClient = KmsClient.builder().region(region).build();
    putItemInTable(ddb, kmsClient, tableName, key, keyVal, albumTitle, albumTitleValue, awards, awardVal, songTitle, songTitleVal, keyId);
    System.out.println("Done!");
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region) KmsClient(software.amazon.awssdk.services.kms.KmsClient)

Example 4 with KmsClient

use of software.amazon.awssdk.services.kms.KmsClient in project aws-doc-sdk-examples by awsdocs.

the class CreateAlias method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <targetKeyId> <aliasName> \n\n" + "Where:\n" + "    targetKeyId - the key ID or the Amazon Resource Name (ARN) of the customer master key (CMK). \n\n" + "    aliasName - an alias name (for example, alias/myAlias). \n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String targetKeyId = args[0];
    String aliasName = args[1];
    Region region = Region.US_WEST_2;
    KmsClient kmsClient = KmsClient.builder().region(region).build();
    createCustomAlias(kmsClient, targetKeyId, aliasName);
    kmsClient.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) KmsClient(software.amazon.awssdk.services.kms.KmsClient)

Example 5 with KmsClient

use of software.amazon.awssdk.services.kms.KmsClient in project aws-doc-sdk-examples by awsdocs.

the class CreateGrant method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <keyId> <granteePrincipal> <operation> \n\n" + "Where:\n" + "    keyId - the unique identifier for the customer master key (CMK) that the grant applies to. \n\n" + "    granteePrincipal - the principal that is given permission to perform the operations that the grant permits. \n\n" + "    operation - an operation (for example, Encrypt). \n\n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String keyId = args[0];
    String granteePrincipal = args[1];
    String operation = args[2];
    Region region = Region.US_WEST_2;
    KmsClient kmsClient = KmsClient.builder().region(region).build();
    String grantId = createGrant(kmsClient, keyId, granteePrincipal, operation);
    System.out.printf("Successfully created a grant with ID %s%n", grantId);
    kmsClient.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) KmsClient(software.amazon.awssdk.services.kms.KmsClient)

Aggregations

KmsClient (software.amazon.awssdk.services.kms.KmsClient)17 Region (software.amazon.awssdk.regions.Region)15 SdkBytes (software.amazon.awssdk.core.SdkBytes)3 KmsException (software.amazon.awssdk.services.kms.model.KmsException)2 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)1 DecryptRequest (software.amazon.awssdk.services.kms.model.DecryptRequest)1 DecryptResponse (software.amazon.awssdk.services.kms.model.DecryptResponse)1 EncryptRequest (software.amazon.awssdk.services.kms.model.EncryptRequest)1 EncryptResponse (software.amazon.awssdk.services.kms.model.EncryptResponse)1