Skip to main content
This section shows how to test encryption and decryption by using the XKS key stored securely on the CryptoHub.

Install AWS CLI

Refer to the Amazon documentation ( docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) for installing AWS CLI on your local machine.

Test encryption and decryption

You can use the following Bash script to test encryption and decryption with the XKS key. The only value you must update in the script is theARN/Key ID. The last output of the script indicates whether the test passes or fails.
To use this example, you must trim the ARN string and get only the Key ID, which is bolded in the following example string:arn:aws:kms:us-east-2:993246061881:key/``d74123b9-0743-46d9-a22a-761239f139
Bash
#!/usr/bin/env bash

echo "Configure your AWS with your credentials"

# If your system already has this configured, you can comment this line below
aws configure

# Configure the plain text that you want to cipher and the KeyID that you want
plaintext="Hello, this is a secret message!"
keyID="87057259-0b6e-46a3-9a6e-381513c7e2ad"

# AWS CLI wants to work with base64 format data
bintext=$(echo -n "$plaintext" | base64)

echo "This is the plain text"
echo $plaintext
echo "This is the bin of plain text that will be used in test"
echo $bintext
echo "This is the KeyId"
echo $keyID

# AWS CLI wants to work with base64 format data
ciphertext=$(aws kms encrypt --key-id $keyID --plaintext $bintext --output text --query CiphertextBlob)

echo "This is the cypher text (in base64 format)"
echo "$ciphertext" > ciphertext.txt
echo $(cat ciphertext.txt)

decrypted=$(aws kms decrypt --ciphertext-blob $ciphertext --output text --query Plaintext)

# Get back data to text format
echo "$decrypted" | base64 --decode > decryptResponse.txt

echo "This is the result of encrypting and decrypting"
result=$(cat decryptResponse.txt)
echo $result

if [[ "$plaintext" == "$result" ]]; then
  echo "Encrypt and Decrypt was Succeed"
else
  echo "Failed to validate Encrypt and Decrypt"
fi
Perform the following steps:
1
Save the preceding Bash script to a file with the .sh extension (such as EncryptDecryptTest.sh).
2
Make the script executable with thechmod command in Linux or macOS:
Shell
chmod +x EncryptDecryptTest.sh
3
And run the script with the following command:
Shell
./EncryptDecryptTest.sh