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;
}
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;
}
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);
}
}
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 "";
}
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);
}
}
Aggregations