Search in sources :

Example 1 with DecryptRequest

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

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

the class EncryptDataKey method decryptData.

// snippet-end:[kms.java2_encrypt_data.main]
// snippet-start:[kms.java2_decrypt_data.main]
public static void decryptData(KmsClient kmsClient, SdkBytes encryptedData, String keyId) {
    try {
        DecryptRequest decryptRequest = DecryptRequest.builder().ciphertextBlob(encryptedData).keyId(keyId).build();
        DecryptResponse decryptResponse = kmsClient.decrypt(decryptRequest);
        decryptResponse.plaintext();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DecryptResponse(software.amazon.awssdk.services.kms.model.DecryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) DecryptRequest(software.amazon.awssdk.services.kms.model.DecryptRequest)

Aggregations

DecryptRequest (software.amazon.awssdk.services.kms.model.DecryptRequest)2 DecryptResponse (software.amazon.awssdk.services.kms.model.DecryptResponse)2 KmsException (software.amazon.awssdk.services.kms.model.KmsException)2 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 KmsClient (software.amazon.awssdk.services.kms.KmsClient)1