Whether you’re studying for the AWS Certified Security Specialty exam, or whether you need a way of encrypting and decrypting data to keep it safe, AWS KMS is a critical service to understand. It’s also not the easiest to wrap your head around if encryption isn’t your strong suit.

In a prior cheat sheet, we explored how to get started with the Key Management Service, but in this one let’s learn how to encrypt and decrypt data using symmetric encryption and the KMS

What is Symmetric Encryption?

Symmetric encryption uses the same cryptographic keys for both the encryption of plaintext and the decryption of ciphertext.

KMS keys

By default, when you create a KMS key, you get a symmetric key that you can use to both encrypt and decrypt data. These KMS keys are great for handling small amounts of data (less than 4096 bytes in size), and they never leave the KMS.

Once created, you can issue a command like this to encrypt plaintext:

aws kms encrypt --key-id alias/lab-key --plaintext fileb://plaintext_favorite_dog.txt --output text --query CiphertextBlob | base64 --decode > encrypted_favorite_dog.txt

…and like this to decrypt ciphertext:

aws kms decrypt --key-id alias/lab-key --ciphertext-blob fileb://encrypted_favorite_dog.txt --output text --query Plaintext | base64 --decode > decrypted_favorite_dog.txt

KMS data keys

However, KMS keys can’t be used for larger data. In that case, you can create a random KMS data key that gets enveloped by a KMS key before it leaves KMS. You can then use the plaintext data key locally to encrypt plaintext data, and later on, you can use it to decrypt its ciphertext.

The default AWS CLI can’t work with data keys directly, so you can either use something like OpenSSL or the AWS Encryption SDK. Reference the cheat sheet for more info and examples!

Want to apply this hands-on?

I created this cheat sheet to celebrate the launch of our latest Cybr 🧪 Hands-On Lab: “Encrypt and Decrypt Data with KMS and Data Keys” where you can apply and practice everything in this cheat sheet in a real AWS environment.

Cybr Premium members can access the lab here.

More information about our Hands-On Labs.

KMS Cheat Sheet

Encrypt and Decrypt Data with KMS and Data Keys

Learn how to encrypt and decrypt data with KMS keys and with Data Keys

AWS KMS

Symmetric Encryption/Decryption

Encrypting Plaintext

You can take plaintext (regular data like what you’re reading right now) and pass it through an encryption algorithm with a KMS Key in order to get ciphertext (encrypted data) back, all without leaving the Key Management Service (KMS) when dealing with smaller data.

Encrypting Plaintext
Encrypting PlaintextYour application sends up to 4,096 bytes of plaintext to the KMS Encrypt API over TLS. KMS encrypts it with the symmetric KMS key. The key material is not returned to your application. KMS returns ciphertext. To recover the plaintext, send this ciphertext to the KMS Decrypt API with permission to use the same key.YOUR APPLICATIONAWS KMSEncrypt requestUses keyCiphertext response01 / INPUTPlaintext DataKMSKMS Key02 / ENCRYPTEncryption Algorithm03 / OUTPUTCiphertext Data
Encrypting Plaintext

Drag the background to pan. Use the controls to zoom or fit the full diagram. Press Escape to close.

By default, when you create a new AWS KMS key, you receive a symmetric encryption KMS key. Symmetric encryption uses the same key to both encrypt and decrypt data.

KMS symmetric keys allow for data encryption up to 4096 bytes in size. The data is sent to KMS, it gets encrypted, and then it gets sent back to the sender which could be a service, application, or yourself. In this case, the encryption key never leaves KMS. If you need to encrypt larger data, see →

Encrypt plaintext data using a KMS key:
aws kms encrypt --key-id alias/lab-key --profile cybrkmslab \
  --plaintext fileb://plaintext_favorite_dog.txt \
  --output text --query CiphertextBlob | \
  base64 --decode > encrypted_favorite_dog.txt
Decrypt ciphertext that was encrypted by a KMS key:
aws kms decrypt --key-id alias/lab-key --profile cybrkmslab \
  --ciphertext-blob fileb://encrypted_favorite_dog.txt \
  --output text --query Plaintext | \
  base64 --decode > decrypted_favorite_dog.txt

Encrypting data greater than 4096 bytes in size

When data larger than 4096 bytes needs to be encrypted, KMS can generate a data key. This data key leaves KMS and is sent back to the sender. The sender is then responsible for managing the security of the key.

The data key that you receive is encrypted with a KMS key and you use the decrypted (plaintext) data key to encrypt data. You can then immediately discard it after use since you can retrieve it at a later time for decryption purposes.

Envelope encryption with a data key
Envelope encryption with a data keyCall GenerateDataKey. KMS returns two copies of the same data key: a plaintext copy and a copy encrypted under your KMS key. Use the plaintext data key in your application to encrypt the file locally. The file itself is not sent to KMS. Discard the plaintext data key after use. Store the encrypted data key with the ciphertext and the required encryption metadata. To decrypt later, send the encrypted data key to KMS Decrypt. Use the returned plaintext data key to decrypt locally, then discard it again.AWS CloudYOUR APPLICATIONPlaintext copyEncrypted copyUse in memoryFileCiphertextKeep with file01 / GENERATE DATA KEYKMS Key02 / USE TEMPORARILYPlaintext Data Key02 / KEEPEncrypted Data KeyINPUTPlaintext Data03 / ENCRYPTEncryption Algorithm04 / STORE TOGETHERCiphertext DataEncrypted Data Key
Envelope encryption with a data key

Drag the background to pan. Use the controls to zoom or fit the full diagram. Press Escape to close.

Generate data key:
aws kms generate-data-key \
  --key-id alias/lab-key \
  --key-spec AES_256 --profile kmslab
Outputs:
• CiphertextBlob → encrypted_text
• Plaintext → plaintext_key
Encrypt file:
KEY_HEX=$(xxd -p -c 256 plaintext_key)
IV=$(openssl rand -hex 16) && echo $IV > iv.txt
Encrypt file:
openssl enc -aes-256-cbc \
  -K $KEY_HEX -iv $IV \
  -in file.txt -out encrypted_file.txt

rm plaintext_key

- or -

Using the AWS Encryption SDK

Encrypt using the AWS Encryption SDK CLI:
aws-encryption-cli --encrypt \
  --input plaintext_sdk_example.txt \
  --output encrypted_sdk_example.txt \
  --wrapping-keys key=key-arn-value-here profile=cybrkmslab \
  --commitment-policy require-encrypt-require-decrypt \
  --metadata-output metadata
Decrypt using the AWS Encryption SDK CLI:
aws-encryption-cli --decrypt \
  --input encrypted_sdk_example.txt \
  --output decrypted_sdk_example.txt \
  --wrapping-keys key=key-arn-value-here profile=kmslab \
  --metadata-output metadata --max-encrypted-data-keys 1 \
  --buffer --commitment-policy require-encrypt-require-decrypt

🧪 Learn KMS By Doing

Use this cheat sheet with AI

Copy a ready-to-use prompt into your preferred AI assistant.

Preview prompt

> More AWS Security cheat sheets <

> KMS Cheat Sheet #1 <