Sub topic 5
Encrypting and decrypting small payloads directly
This chapter assumes you can use the AWS CLI, understand plaintext versus ciphertext, and have a customer managed symmetric KMS key with the alias alias/tutorial-key. Your caller needs permission to use that key.
What a direct call does
The KMS Encrypt API sends your plaintext to KMS. KMS encrypts it under a KMS key inside its hardware security modules and returns the ciphertext. The key material never leaves KMS. Decrypt reverses the process.
Direct calls are limited to 4,096 bytes of plaintext. That fits passwords, API tokens, and small configuration values. Larger data needs envelope encryption, which a later chapter covers.
Encrypt from the CLI
Write a small file, then encrypt it:
echo -n "my secret" > secret.txt
aws kms encrypt \
--key-id alias/tutorial-key \
--plaintext fileb://secret.txt \
--encryption-context purpose=demo \
--query CiphertextBlob \
--output text | base64 --decode > secret.encThree details matter here:
fileb://reads the file as raw bytes. Plainfile://treats it as text and can corrupt the input.- The CLI prints
CiphertextBlobas Base64 text. Thebase64 --decodestep turns it back into binary sosecret.encholds the real ciphertext. --encryption-context purpose=demoattaches extra key-value data. Decryption must supply the same data. The next chapter covers it in detail.
Decrypt from the CLI
aws kms decrypt \
--ciphertext-blob fileb://secret.enc \
--encryption-context purpose=demo \
--query Plaintext \
--output text | base64 --decodeThis prints my secret. You did not pass a key ID. For a symmetric key, the ciphertext blob records which key encrypted it, so KMS finds the key itself. The caller still needs kms:Decrypt permission on that key.
If you want to be strict, pass --key-id to decrypt. KMS then fails the call if the ciphertext came from a different key. This guards against decrypting data that was not meant for you.
Common errors
| Error | Likely cause |
|---|---|
| `InvalidCiphertextException` | The ciphertext is corrupted, was left Base64-encoded in the file, or the encryption context does not match. |
| `AccessDeniedException` | The caller lacks `kms:Encrypt` or `kms:Decrypt` in the key policy or IAM policy. |
| `ValidationException` about length | The plaintext exceeds 4,096 bytes. |
| `DisabledException` | The key is disabled or pending deletion. |
| `NotFoundException` | The alias or key ID does not exist in the Region you are calling. |
KMS keys belong to one Region. Encrypting in us-east-1 and decrypting in eu-west-1 fails unless you use a Multi-Region key.
When to use direct calls
Direct calls suit small secrets that you store rarely and read occasionally. Every call goes over the network to KMS and counts against request quotas. For large files or high-volume workloads, generate a data key and encrypt locally instead.
Exercise
You run aws kms decrypt on a ciphertext that was encrypted with --encryption-context purpose=demo, but you omit the --encryption-context option. What happens, and why do you not need to pass --key-id?
Show answer
The call fails with InvalidCiphertextException, because the encryption context must match the one used at encryption. You do not need --key-id because a symmetric ciphertext blob records which KMS key encrypted it, so KMS selects that key automatically.