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 bashecho "Configure your AWS with your credentials"# If your system already has this configured, you can comment this line belowaws configure# Configure the plain text that you want to cipher and the KeyID that you wantplaintext="Hello, this is a secret message!"keyID="87057259-0b6e-46a3-9a6e-381513c7e2ad"# AWS CLI wants to work with base64 format databintext=$(echo -n "$plaintext" | base64)echo "This is the plain text"echo $plaintextecho "This is the bin of plain text that will be used in test"echo $bintextecho "This is the KeyId"echo $keyID# AWS CLI wants to work with base64 format dataciphertext=$(aws kms encrypt --key-id $keyID --plaintext $bintext --output text --query CiphertextBlob)echo "This is the cypher text (in base64 format)"echo "$ciphertext" > ciphertext.txtecho $(cat ciphertext.txt)decrypted=$(aws kms decrypt --ciphertext-blob $ciphertext --output text --query Plaintext)# Get back data to text formatecho "$decrypted" | base64 --decode > decryptResponse.txtecho "This is the result of encrypting and decrypting"result=$(cat decryptResponse.txt)echo $resultif [[ "$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
Was this page helpful?
⌘I
Assistant
Responses are generated using AI and may contain mistakes.