Search in sources :

Example 1 with KmsException

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

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

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

the class CreateAlias method createCustomAlias.

// snippet-start:[kms.java2._create_alias.main]
public static void createCustomAlias(KmsClient kmsClient, String targetKeyId, String aliasName) {
    try {
        CreateAliasRequest aliasRequest = CreateAliasRequest.builder().aliasName(aliasName).targetKeyId(targetKeyId).build();
        kmsClient.createAlias(aliasRequest);
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : KmsException(software.amazon.awssdk.services.kms.model.KmsException) CreateAliasRequest(software.amazon.awssdk.services.kms.model.CreateAliasRequest)

Example 4 with KmsException

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

the class CreateGrant method createGrant.

// snippet-start:[kms.java2_create_grant.main]
public static String createGrant(KmsClient kmsClient, String keyId, String granteePrincipal, String operation) {
    try {
        CreateGrantRequest grantRequest = CreateGrantRequest.builder().keyId(keyId).granteePrincipal(granteePrincipal).operationsWithStrings(operation).build();
        CreateGrantResponse response = kmsClient.createGrant(grantRequest);
        return response.grantId();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : CreateGrantResponse(software.amazon.awssdk.services.kms.model.CreateGrantResponse) CreateGrantRequest(software.amazon.awssdk.services.kms.model.CreateGrantRequest) KmsException(software.amazon.awssdk.services.kms.model.KmsException)

Example 5 with KmsException

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

the class DescribeKey method describeSpecifcKey.

// snippet-start:[kms.java2_describe_key.main]
public static void describeSpecifcKey(KmsClient kmsClient, String keyId) {
    try {
        DescribeKeyRequest keyRequest = DescribeKeyRequest.builder().keyId(keyId).build();
        DescribeKeyResponse response = kmsClient.describeKey(keyRequest);
        System.out.println("The key description is " + response.keyMetadata().description());
        System.out.println("The key ARN is " + response.keyMetadata().arn());
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DescribeKeyResponse(software.amazon.awssdk.services.kms.model.DescribeKeyResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) DescribeKeyRequest(software.amazon.awssdk.services.kms.model.DescribeKeyRequest)

Aggregations

KmsException (software.amazon.awssdk.services.kms.model.KmsException)16 SdkBytes (software.amazon.awssdk.core.SdkBytes)3 KmsClient (software.amazon.awssdk.services.kms.KmsClient)2 DecryptRequest (software.amazon.awssdk.services.kms.model.DecryptRequest)2 DecryptResponse (software.amazon.awssdk.services.kms.model.DecryptResponse)2 EncryptRequest (software.amazon.awssdk.services.kms.model.EncryptRequest)2 EncryptResponse (software.amazon.awssdk.services.kms.model.EncryptResponse)2 AliasListEntry (software.amazon.awssdk.services.kms.model.AliasListEntry)1 CreateAliasRequest (software.amazon.awssdk.services.kms.model.CreateAliasRequest)1 CreateGrantRequest (software.amazon.awssdk.services.kms.model.CreateGrantRequest)1 CreateGrantResponse (software.amazon.awssdk.services.kms.model.CreateGrantResponse)1 CreateKeyRequest (software.amazon.awssdk.services.kms.model.CreateKeyRequest)1 CreateKeyResponse (software.amazon.awssdk.services.kms.model.CreateKeyResponse)1 DeleteAliasRequest (software.amazon.awssdk.services.kms.model.DeleteAliasRequest)1 DescribeKeyRequest (software.amazon.awssdk.services.kms.model.DescribeKeyRequest)1 DescribeKeyResponse (software.amazon.awssdk.services.kms.model.DescribeKeyResponse)1 DisableKeyRequest (software.amazon.awssdk.services.kms.model.DisableKeyRequest)1 EnableKeyRequest (software.amazon.awssdk.services.kms.model.EnableKeyRequest)1 GrantListEntry (software.amazon.awssdk.services.kms.model.GrantListEntry)1 KeyListEntry (software.amazon.awssdk.services.kms.model.KeyListEntry)1