'AWS KMS error Member must have length less than or equal to 4096

I am facing issue while encrypting some text via KMS and that is :

software.amazon.awssdk.services.kms.model.KmsException: 1 validation error detected: Value at 'plaintext' failed to satisfy constraint: Member must have length less than or equal to 4096 (Service: Kms, Status Code: 400, Request ID: c0a79bc3-52a0-42c3-95d5-a5f77217962e, Extended Request ID: null)

I read in AWS documentation that there is a limitation of KMS encrypt data to 4Kb only with master key. But if data key can be generated through master key then that data key doesn't have any limitation of data encryption length :

Below is my code :

 public Uni<String> encrypt(String data) {
    
    System.out.println("data to encrypt is ::"+data);
    GenerateDataKeyRequest dataKeyReq = GenerateDataKeyRequest.builder().keyId(keyArn).keySpec(DataKeySpec.AES_256).build();
    return Uni.createFrom().completionStage(kms.generateDataKey(dataKeyReq).thenComposeAsync(res->kms.encrypt(req -> req.keyId(res.keyId()).plaintext(SdkBytes.fromUtf8String(data)))))
            .onItem().transform(EncryptResponse::ciphertextBlob)
            .onItem().transform(blob -> Base64.encodeBase64String(blob.asByteArray()));
}

Still after generating data key I am facing same issue. Is there anything which I am missing in above code or is there any other way of generating data key ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source