Raw symmetric encryption

This topic shows you how to do the following raw symmetric key operations:

If instead you want to do a regular (non-raw) symmetric key operation, see Encrypting and decrypting data with a symmetric key.

The raw symmetric encryption lets you encrypt and decrypt your data locally on-premises or using Cloud KMS, and move encrypted data between different libraries and service providers without having to decrypt it first. This functionality depends on the ability to access the key at the point of operation. If you want to use the ciphertexts outside of Google Cloud, you must use an imported key because keys generated in Cloud KMS can't be exported. These encryption algorithms generate standard ciphertexts that can be decrypted by any standard decryption service. We support the following raw symmetric encryption algorithms:

Note the following points about these raw encryption algorithms:

Required roles

To get the permissions that you need to use raw encryption, ask your administrator to grant you the following IAM roles on your key:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Additional roles for unauthenticated raw encryption algorithms

Before you begin

Encrypt

gcloud

To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.

gcloud kms raw-encrypt \
    --location LOCATION \
    --keyring KEY_RING \
    --key KEY_NAME \
    --version KEY_VERSION \
    --plaintext-file INPUT_FILE_PATH \
    --ciphertext-file OUTPUT_FILE_PATH

Replace the following:

For information on all flags and possible values, run the command with the --help flag.

API

These examples use curl as an HTTP client to demonstrate using the API. For more information about access control, see Accessing the Cloud KMS API.

When using JSON and the REST API, content must be base64 encoded before it can be encrypted by Cloud KMS.

Use the rawEncrypt method to encrypt a plaintext data:

curl "https://cloudkms.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME/cryptoKeyVersions/KEY_VERSION:rawEncrypt" \
  --request "POST" \
  --header "authorization: Bearer TOKEN" \
  --header "content-type: application/json" \
  --data '{"plaintext": "BASE64_ENCODED_INPUT", "additionalAuthenticatedData": "BASE64_ENCODED_AAD"}'

Replace the following:

The output is a JSON object containing the encrypted ciphertext and the associated initialization vector as base64-encoded strings.

Decrypt

gcloud

To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.

gcloud kms raw-decrypt \
    --location LOCATION \
    --keyring KEY_RING \
    --key KEY_NAME \
    --version KEY_VERSION \
    --ciphertext-file INPUT_FILE_PATH \
    --plaintext-file OUTPUT_FILE_PATH

Replace the following:

For information on all flags and possible values, run the command with the --help flag.

API

These examples use curl as an HTTP client to demonstrate using the API. For more information about access control, see Accessing the Cloud KMS API.

When using the REST API, content must be base64-encoded before it can be decrypted by Cloud KMS.

To decrypt the encrypted data, use the rawDecrypt method:

curl "https://cloudkms.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME/cryptoKeyVersions/KEY_VERSION:rawDecrypt" \
  --request "POST" \
  --header "authorization: Bearer TOKEN" \
  --header "content-type: application/json" \
  --data '{"ciphertext": "BASE64_ENCODED_DATA", "additionalAuthenticatedData": "BASE64_ENCODED_AAD", "initializationVector": "BASE64_ENCODED_IV"}'

Replace the following:

The output is a JSON object containing the decrypted plaintext as a base64-encoded string.

What's next

  • Read more about importing a key version
  • Read more about envelope encryption.
  • Try the Encrypt and decrypt data with Cloud KMS Codelab.