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