使用shell_exec的aws-encryption-cli

I am using aws-encryption-cli for my data encryption. It works perfectly. I can encrypt and decrypt the data coming from client inputs and save it to the database.

If I fetch all the records, the system takes too much time to load the data. And the page shows an ERROR 504.

Here's my code:

Encryption process:

$ encryptedData = shell_exec('echo "'. $data .'" | aws-encryption-cli --encrypt -S -i - -m key='. AWSKEY .' -o - --encode');

Decryption process

$ decyptedData = shell_exec('echo "'. $data .'" | aws-encryption-cli --decrypt -S -i - -o - --decode');

Is there a way to make this program faster?

Thanks in advance.