Re-encrypting data

This topic shows how to re-encrypt data. If you suspect unauthorized use of a key, you should re-encrypt the data protected by that key and then disable or schedule destruction of the prior CryptoKeyVersion.

Before you begin

This scenario requires the following conditions.

  • You have already encrypted data using Cloud KMS.

  • The CryptoKeyVersion used for the encryption is not disabled, scheduled for destruction, or destroyed. You use this CryptoKeyVersion to decrypt the encrypted data.

  • You have already rotated keys. A key rotation creates a new primary CryptoKeyversion. You use the new primary CryptoKeyVersion to re-encrypt the data.

Re-encrypting data workflow

Use the following steps to re-encrypt data and disable or schedule destruction of the CryptoKeyVersion used for the original encryption.

  1. Decrypt the data using the prior CryptoKeyVersion

  2. Re-encrypt the data using the new primary CryptoKeyVersion

  3. Disable or schedule destruction of the prior CryptoKeyVersion

Decrypt the data using the prior CryptoKeyVersion

Cloud KMS automatically uses the correct CryptoKeyVersion to decrypt data, as long as the CryptoKeyVersion is not disabled, scheduled for destruction, or destroyed. The following examples show how to decrypt the data. This is the same decryption code used in Encrypting and Decrypting.

Command-line

To decrypt data, provide the appropriate key information, specify the name of the encrypted file (ciphertext file) to decrypt, and specify the name of the file that will contain the decrypted content.

gcloud kms decrypt \
    --location=global \
    --keyring=my-key-ring \
    --key=my-key \
    --ciphertext-file=YOUR_FILEPATH_AND_FILENAME_TO_DECRYPT \
    --plaintext-file=YOUR_FILEPATH_AND_FILENAME_TO_DECRYPT.dec

The decrypt command supports an optional --additional-authenticated-data- file flag to specify a file that contains additional authenticated data. The additional authenticated data file must not be larger than 64 KiB.

If --ciphertext-file or --additional-authenticated-data-file is set to -, that file is read from stdin. Similarly, if --plaintext-file is set to -, the decrypted plaintext is written to stdout.

The following decrypt example shows how to specify additional authenticated data.

gcloud kms decrypt \
    --location=global \
    --keyring=my-key-ring \
    --key=my-key \
    --additional-authenticated-data-file=YOUR_ADDITIONAL_AUTHENTICATED_DATA_FILEPATH_AND_FILENAME \
    --ciphertext-file=YOUR_FILEPATH_AND_FILENAME_TO_DECRYPT \
    --plaintext-file=YOUR_FILEPATH_AND_FILENAME_TO_DECRYPT.dec

Protocol

Decrypted text that is returned in the JSON from Cloud KMS is base64-encoded. For information on encoding and decoding using base64, see Base64 Encoding.

To decrypt encrypted data, make a POST request and provide the appropriate project and key information and specify the encrypted (cipher) text to be decrypted in the ciphertext field of the request body. Replace YOUR_API_KEY with a valid API key. For information on generating an API key, see [Accessing the API][accessing-the-api].

POST https://cloudkms.googleapis.com/v1/projects/project_id/locations/global/keyRings/keyring_name/cryptoKeys/cryptoKey_name:decrypt&key=YOUR_API_KEY
{
  "ciphertext": "CiQAhMwwBo61cHas7dDgifrUFs5zNzBJ2uZtVFq4ZPEl6fUVT4kSmQ...",
}

Here is an example using curl:

curl -s -X POST "https://cloudkms.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/keyRings/YOUR_KEYRING_NAME/cryptoKeys/YOUR_CRYPTOKEY_NAME:decrypt" \
  -d "{\"ciphertext\":\"ENCRYPTED_CONTENT\"}" \
  -H "Authorization:Bearer YOUR_API_KEY"\
  -H "Content-Type:application/json"

C#

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

    public static object Decrypt(string projectId, string location, string keyRing, string cryptoKeyName,
string fileToDecrypt, string fileToOutput)
    {
        var cloudKms = CreateAuthorizedClient();
        // Generate the full path of the crypto key to use for encryption.
        var cryptoKey = $"projects/{projectId}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKeyName}";
        DecryptRequest dataToDecrypt = new DecryptRequest();
        byte[] bytes = File.ReadAllBytes(fileToDecrypt);
        string contents = Convert.ToBase64String(bytes);
        dataToDecrypt.Ciphertext = contents;
        Console.WriteLine($"dataToDecrypt.Ciphertext: {dataToDecrypt.Ciphertext}");
        var result = cloudKms.Projects.Locations.KeyRings.CryptoKeys.Decrypt(dataToDecrypt, cryptoKey).Execute();
        // Output decypted data to a file
        File.WriteAllBytes(fileToOutput, Convert.FromBase64String(result.Plaintext));
        Console.Write($"Encypted file created: {fileToOutput}");
        return 0;
    }

Go

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

func decrypt(projectID, keyRing, cryptoKey string, ciphertext []byte) ([]byte, error) {
	ctx := context.Background()
	client, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope)
	if err != nil {
		log.Fatal(err)
	}

	cloudkmsService, err := cloudkms.New(client)
	if err != nil {
		log.Fatal(err)
	}

	parentName := fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
		projectID, "global", keyRing, cryptoKey)

	resp, err := cloudkmsService.Projects.Locations.KeyRings.CryptoKeys.
		Decrypt(parentName, &cloudkms.DecryptRequest{
			Ciphertext: base64.StdEncoding.EncodeToString(ciphertext),
		}).Do()
	if err != nil {
		return nil, err
	}
	return base64.StdEncoding.DecodeString(resp.Plaintext)
}

Java

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Decrypts the given encrypted bytes, using the specified crypto key.
 */
public static byte[] decrypt(String projectId, String ringId, String keyId, byte[] encrypted)
    throws IOException {
  String location = "global";
  // Create the Cloud KMS client.
  CloudKMS kms = createAuthorizedClient();

  // The resource name of the cryptoKey
  String cryptoKeyName = String.format(
      "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
      projectId, location, ringId, keyId);

  DecryptRequest request = new DecryptRequest().encodeCiphertext(encrypted);
  DecryptResponse response = kms.projects().locations().keyRings().cryptoKeys()
          .decrypt(cryptoKeyName, request)
          .execute();

  return response.decodePlaintext();
}

Node.js

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

const fs = require('fs');

// Your Google Cloud Platform project ID
// const projectId = 'YOUR_PROJECT_ID';

// The location of the crypto key's key ring, e.g. "global"
// const location = 'global';

// The name of the crypto key's key ring, e.g. "my-key-ring"
// const keyRingName = 'my-key-ring';

// The name of the crypto key, e.g. "my-key"
// const keyName = 'my-key';

// The path to the file to decrypt, e.g. "./path/to/plaintext.txt.encrypted"
// const infile = './path/to/plaintext.txt.encrypted';

// The path where the decrypted file should be written, e.g. "./path/to/plaintext.txt.decrypted"
// const outfile = './path/to/plaintext.txt.decrypted';

// Builds and authorizes a Cloud KMS client
buildAndAuthorizeService((err, cloudkms) => {
  if (err) {
    console.log(err);
    return;
  }

  // Reads the file to be decrypted
  fs.readFile(infile, 'utf8', (err, contentsBuffer) => {
    if (err) {
      console.log(err);
      return;
    }

    const request = {
      // This will be a path parameter in the request URL
      name: `projects/${projectId}/locations/${location}/keyRings/${keyRingName}/cryptoKeys/${keyName}`,
      // This will be the request body
      resource: {
        ciphertext: contentsBuffer
      }
    };

    // Dencrypts the file using the specified crypto key
    cloudkms.projects.locations.keyRings.cryptoKeys.decrypt(request, (err, result) => {
      if (err) {
        console.log(err);
        return;
      }

      // Writes the dencrypted file to disk
      fs.writeFile(outfile, Buffer.from(result.plaintext, 'base64'), (err) => {
        if (err) {
          console.log(err);
          return;
        }

        console.log(`Decrypted ${infile}, result saved to ${outfile}.`);
      });
    });
  });
});

function buildAndAuthorizeService (callback) {
  // Imports the Google APIs client library
  const google = require('googleapis');

  // Acquires credentials
  google.auth.getApplicationDefault((err, authClient) => {
    if (err) {
      callback(err);
      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/cloud-platform'
      ]);
    }

    // Instantiates an authorized client
    const cloudkms = google.cloudkms({
      version: 'v1',
      auth: authClient
    });

    callback(null, cloudkms);
  });
}

PHP

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Decrypt a text file.
 *
 * @param string $projectId
 * @param string $ring
 * @param string $key
 * @param string $infile The path to an encrypted file.
 * @param string $outfile The path to write the decrypted file.
 * @param string $location [optional]
 * @return null
 */
function decrypt($projectId, $ring, $key, $infile, $outfile, $location = 'global')
{
    // Instantiate the client, authenticate, and add scopes.
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    // Create the Cloud KMS client.
    $kms = new Google_Service_CloudKMS($client);

    // The resource name of the cryptokey.
    $name = sprintf('projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s',
        $projectId,
        $location,
        $ring,
        $key
    );

    // Use the KMS API to decrypt the text.
    $ciphertext = file_get_contents($infile);
    $request = new Google_Service_CloudKMS_DecryptRequest();
    $request->setCiphertext($ciphertext);
    $response = $kms->projects_locations_keyRings_cryptoKeys->decrypt(
        $name,
        $request
    );

    // Write the decrypted text to a file.
    $plaintext = base64_decode($response['plaintext']);
    file_put_contents($outfile, $plaintext);
    printf('Saved decrypted text to %s' . PHP_EOL, $outfile);
}

Python

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

def decrypt(project_id, location, keyring, cryptokey, encrypted_file_name,
            decrypted_file_name):
    """Decrypts data from encrypted_file_name that was previously encrypted
    using the CryptoKey with a call to encrypt. Outputs decrypted data to
    decrpyted_file_name."""

    # Creates an API client for the KMS API.
    kms_client = googleapiclient.discovery.build('cloudkms', 'v1')

    # The resource name of the CryptoKey.
    name = 'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(
        project_id, location, keyring, cryptokey)

    # Read cipher text from the input file.
    with io.open(encrypted_file_name, 'rb') as encrypted_file:
        ciphertext = encrypted_file.read()

    # Use the KMS API to decrypt the text.
    cryptokeys = kms_client.projects().locations().keyRings().cryptoKeys()
    request = cryptokeys.decrypt(
        name=name,
        body={'ciphertext': base64.b64encode(ciphertext).decode('ascii')})
    response = request.execute()
    plaintext = base64.b64decode(response['plaintext'].encode('ascii'))

    # Write the plain text to a file.
    with io.open(decrypted_file_name, 'wb') as decrypted_file:
        decrypted_file.write(plaintext)

    print('Saved decrypted text to {}.'.format(decrypted_file_name))

Ruby

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

# project_id  = "Your Google Cloud project ID"
# key_ring_id = "The ID of the key ring"
# crypto_key  = "Name of the crypto key"
# location    = "The location of the key ring"
# input_file  = "The path to an encrypted file"
# output_file = "The path to write the decrypted file"

require "google/apis/cloudkms_v1"

# Initialize the client and authenticate with the specified scope
Cloudkms = Google::Apis::CloudkmsV1
kms_client = Cloudkms::CloudKMSService.new
kms_client.authorization = Google::Auth.get_application_default(
  "https://www.googleapis.com/auth/cloud-platform"
)

# The resource name of the location associated with the key ring crypto key
resource = "projects/#{project_id}/locations/#{location}/" +
           "keyRings/#{key_ring_id}/cryptoKeys/#{crypto_key}"

# Use the KMS API to decrypt the text
encrypted_text = File.read input_file

request = Cloudkms::DecryptRequest.new ciphertext: encrypted_text

response = kms_client.decrypt_crypto_key resource, request

# Write the decrypted text to a file
File.write output_file, response.plaintext

puts "Saved decrypted #{input_file} as #{output_file}"

Re-encrypt the data using the new primary CryptoKeyVersion

Cloud KMS automatically uses the new primary CryptoKeyVersion to encrypt data. The following examples show how to encrypt the data. This is the same encryption code used in Encrypting and Decrypting.

Command-line

To encrypt data, provide the appropriate key information, specify the name of the plaintext file to encrypt, and specify the name of the file that will contain the encrypted content.

gcloud kms encrypt \
    --location=global  \
    --keyring=my-key-ring \
    --key=my-key \
    --plaintext-file=YOUR_FILEPATH_AND_FILENAME_TO_ENCRYPT.dec \
    --ciphertext-file=YOUR_FILEPATH_AND_FILENAME.enc

The plaintext file must not be larger than 64 KiB.

The encrypt command supports an optional --additional-authenticated-data-file flag to specify a file that contains additional authenticated data. The additional authenticated data file must not be larger than 64 KiB.

If --plaintext-file or --additional-authenticated-data-file is set to -, that file is read from stdin. Similarly, if --ciphertext-file is set to -, the ciphertext is written to stdout.

The encrypt command supports an optional --version flag to indicate the version of the key to use for encryption. By default, the primary version is used.

The following encrypt example shows how to specify a version key and additional authenticated data.

gcloud kms encrypt \
    --location=global  \
    --keyring=my-key-ring \
    --key=my-key \
    --version=my-key-version \
    --additional-authenticated-data-file=YOUR_ADDITIONAL_AUTHENTICATED_DATA_FILEPATH_AND_FILENAME \
    --plaintext-file=YOUR_FILEPATH_AND_FILENAME_TO_ENCRYPT.dec \
    --ciphertext-file=YOUR_FILEPATH_AND_FILENAME.enc

Protocol

Content to be encrypted that is sent to Cloud KMS in JSON must be base64-encoded. For information on encoding and decoding using base64, see Base64 Encoding.

To encrypt data, make a POST request and provide the appropriate project and key information and specify the base64-encoded text to be encrypted in the plaintext field of the request body. Replace YOUR_API_KEY with a valid API key. For information on generating an API key, see [Accessing the API][accessing-the-api].

POST https://cloudkms.googleapis.com/v1/projects/project_id/locations/global/keyRings/keyring_name/cryptoKeys/cryptoKey_name:encrypt&key=YOUR_API_KEY
{
  "plaintext": "BASE64_ENCODED_INPUT",
}

Here is an example using curl:

curl -s -X POST "https://cloudkms.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/keyRings/YOUR_KEYRING_NAME/cryptoKeys/YOUR_CRYPTOKEY_NAME:encrypt" \
  -d "{\"plaintext\":\"BASE64_ENCODED_INPUT\"}" \
  -H "Authorization:Bearer YOUR_API_KEY"\
  -H "Content-Type:application/json"

C#

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

        public static object Encrypt(string projectId, string location, string keyRing, string cryptoKeyName,
string fileToEncrypt, string fileToOutput)
        {
            var cloudKms = CreateAuthorizedClient();
            // Generate the full path of the crypto key to use for encryption.
            var cryptoKey = $"projects/{projectId}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKeyName}";
            EncryptRequest dataToEncrypt = new EncryptRequest();
            byte[] bytes = File.ReadAllBytes(fileToEncrypt);
            string contents = Convert.ToBase64String(bytes);
            dataToEncrypt.Plaintext = contents;
            Console.WriteLine($"dataToEncrypt.Plaintext: {dataToEncrypt.Plaintext}");
            var result = cloudKms.Projects.Locations.KeyRings.CryptoKeys.Encrypt(body: dataToEncrypt, name: cryptoKey).Execute();
            // Output encypted data to a file.
            File.WriteAllBytes(fileToOutput, Convert.FromBase64String(result.Ciphertext));
            Console.Write($"Encypted file created: {fileToOutput}");
            return 0;
        }

Go

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

func encrypt(projectID, keyRing, cryptoKey string, plaintext []byte) ([]byte, error) {
	ctx := context.Background()
	client, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope)
	if err != nil {
		log.Fatal(err)
	}

	cloudkmsService, err := cloudkms.New(client)
	if err != nil {
		log.Fatal(err)
	}

	parentName := fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
		projectID, "global", keyRing, cryptoKey)

	resp, err := cloudkmsService.Projects.Locations.KeyRings.CryptoKeys.
		Encrypt(parentName, &cloudkms.EncryptRequest{
			Plaintext: base64.StdEncoding.EncodeToString(plaintext),
		}).Do()
	if err != nil {
		return nil, err
	}

	return base64.StdEncoding.DecodeString(resp.Ciphertext)
}

Java

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Encrypts the given bytes, using the specified crypto key version.
 */
public static byte[] encrypt(
    String projectId, String ringId, String keyId, String version, byte[] plaintext)
    throws IOException {
  String location = "global";
  // The resource name of the cryptoKey
  String cryptoKeyName = String.format(
      "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
      projectId, location, ringId, keyId);
  if (null != version) {
    cryptoKeyName += "/cryptoKeyVersions/" + version;
  }
  // Create the Cloud KMS client.
  CloudKMS kms = createAuthorizedClient();

  EncryptRequest request = new EncryptRequest().encodePlaintext(plaintext);
  EncryptResponse response = kms.projects().locations().keyRings().cryptoKeys()
          .encrypt(cryptoKeyName, request)
          .execute();

  return response.decodeCiphertext();
}

Node.js

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

const fs = require('fs');

// Your Google Cloud Platform project ID
// const projectId = 'YOUR_PROJECT_ID';

// The location of the crypto key's key ring, e.g. "global"
// const location = 'global';

// The name of the crypto key's key ring, e.g. "my-key-ring"
// const keyRingName = 'my-key-ring';

// The name of the crypto key, e.g. "my-key"
// const keyName = 'my-key';

// The path to the file to encrypt, e.g. "./path/to/plaintext.txt"
// const infile = './path/to/plaintext.txt';

// The path where the encrypted file should be written, e.g. "./path/to/plaintext.txt.encrypted"
// const outfile = './path/to/plaintext.txt.encrypted';

// Builds and authorizes a Cloud KMS client
buildAndAuthorizeService((err, cloudkms) => {
  if (err) {
    console.log(err);
    return;
  }

  // Reads the file to be encrypted
  fs.readFile(infile, (err, contentsBuffer) => {
    if (err) {
      console.log(err);
      return;
    }

    const request = {
      // This will be a path parameter in the request URL
      name: `projects/${projectId}/locations/${location}/keyRings/${keyRingName}/cryptoKeys/${keyName}`,
      // This will be the request body
      resource: {
        plaintext: contentsBuffer.toString('base64')
      }
    };

    // Encrypts the file using the specified crypto key
    cloudkms.projects.locations.keyRings.cryptoKeys.encrypt(request, (err, result) => {
      if (err) {
        console.log(err);
        return;
      }

      // Writes the encrypted file to disk
      fs.writeFile(outfile, Buffer.from(result.ciphertext), (err) => {
        if (err) {
          console.log(err);
          return;
        }

        console.log(`Encrypted ${infile} using ${result.name}.`);
        console.log(`Result saved to ${outfile}.`);
      });
    });
  });
});

function buildAndAuthorizeService (callback) {
  // Imports the Google APIs client library
  const google = require('googleapis');

  // Acquires credentials
  google.auth.getApplicationDefault((err, authClient) => {
    if (err) {
      callback(err);
      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/cloud-platform'
      ]);
    }

    // Instantiates an authorized client
    const cloudkms = google.cloudkms({
      version: 'v1',
      auth: authClient
    });

    callback(null, cloudkms);
  });
}

PHP

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Encrypt a text file.
 *
 * @param string $projectId
 * @param string $ring
 * @param string $key
 * @param string $infile The path to a text file.
 * @param string $outfile The path to write the encrypted file.
 * @param string $location [optional]
 * @return null
 */
function encrypt($projectId, $ring, $key, $infile, $outfile, $location = 'global')
{
    // Instantiate the client, authenticate, and add scopes.
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    // Create the Cloud KMS client.
    $kms = new Google_Service_CloudKMS($client);

    // The resource name of the cryptokey.
    $name = sprintf('projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s',
        $projectId,
        $location,
        $ring,
        $key
    );

    // Use the KMS API to encrypt the text.
    $encoded = base64_encode(file_get_contents($infile));
    $request = new Google_Service_CloudKMS_EncryptRequest();
    $request->setPlaintext($encoded);
    $response = $kms->projects_locations_keyRings_cryptoKeys->encrypt(
        $name,
        $request
    );

    // Write the encrypted text to a file.
    file_put_contents($outfile, $response['ciphertext']);
    printf('Saved encrypted text to %s' . PHP_EOL, $outfile);
}

Python

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

def encrypt(project_id, location, keyring, cryptokey, plaintext_file_name,
            encrypted_file_name):
    """Encrypts data from a plaintext_file_name using the provided CryptoKey
    and saves it to an encrypted_file_name so it can only be recovered with a
    call to decrypt."""

    # Creates an API client for the KMS API.
    kms_client = googleapiclient.discovery.build('cloudkms', 'v1')

    # The resource name of the CryptoKey.
    name = 'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(
        project_id, location, keyring, cryptokey)

    # Read text from the input file.
    with io.open(plaintext_file_name, 'rb') as plaintext_file:
        plaintext = plaintext_file.read()

    # Use the KMS API to encrypt the text.
    cryptokeys = kms_client.projects().locations().keyRings().cryptoKeys()
    request = cryptokeys.encrypt(
        name=name,
        body={'plaintext': base64.b64encode(plaintext).decode('ascii')})
    response = request.execute()
    ciphertext = base64.b64decode(response['ciphertext'].encode('ascii'))

    # Write the encrypted text to a file.
    with io.open(encrypted_file_name, 'wb') as encrypted_file:
        encrypted_file.write(ciphertext)

    print('Saved encrypted text to {}.'.format(encrypted_file_name))

Ruby

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

# project_id  = "Your Google Cloud project ID"
# key_ring_id = "The ID of the key ring"
# crypto_key  = "Name of the crypto key"
# location    = "The location of the key ring"
# input_file  = "File to encrypt"
# output_file = "File name to use for encrypted input file"

require "google/apis/cloudkms_v1"

# Initialize the client and authenticate with the specified scope
Cloudkms = Google::Apis::CloudkmsV1
kms_client = Cloudkms::CloudKMSService.new
kms_client.authorization = Google::Auth.get_application_default(
  "https://www.googleapis.com/auth/cloud-platform"
)

# The resource name of the location associated with the key ring crypto key
resource = "projects/#{project_id}/locations/#{location}/" +
           "keyRings/#{key_ring_id}/cryptoKeys/#{crypto_key}"

# Use the KMS API to encrypt the text
plain_text = File.read input_file

request = Cloudkms::EncryptRequest.new plaintext: plain_text

response = kms_client.encrypt_crypto_key resource, request

# Write the encrypted text to a file
File.write output_file, response.ciphertext

puts "Saved encrypted #{input_file} as #{output_file}"

Disable or schedule destruction of the prior CryptoKeyVersion

If you rotated your CryptoKey in response to a suspected incident, after you have re-encrypted the data, disable or schedule destruction of the prior CryptoKeyVersion.

Disable an enabled CryptoKeyVersion

Only a CryptoKeyVersion which is Enabled can be Disabled. This is done with the method UpdateCryptoKeyVersion.

Command-line

To disable version "42" of CryptoKey "answer" in KeyRing "answers"

gcloud kms keys versions disable 42 --location global --keyring answers --key answer

C#

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

      public static object DisableCryptoKeyVersion(string projectId, string location, string keyRing, string cryptoKey, string version)
      {
          var cloudKms = CreateAuthorizedClient();
          // Generate the full path of the parent to use for disabling the crypto key Version.
          var parent = $"projects/{projectId}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}/cryptoKeyVersions/{version}";
          // Get crypto key version.
          var request = new ProjectsResource.LocationsResource.KeyRingsResource.CryptoKeysResource
              .CryptoKeyVersionsResource.GetRequest(cloudKms, parent);
          var result = request.Execute();
          result.State = "DISABLED";
          var patchRequest = new ProjectsResource.LocationsResource.KeyRingsResource.CryptoKeysResource
              .CryptoKeyVersionsResource.PatchRequest(cloudKms, result, parent);
          patchRequest.UpdateMask = "state";
          var patchResult = patchRequest.Execute();
          Console.Write($"Disabled Crypto Key Version: {patchResult.Name}");
          return 0;
      }

Go

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

func disableCryptoKeyVersion(project, ring, key, version string) error {
	ctx := context.Background()
	authedClient, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope)
	if err != nil {
		return err
	}
	client, err := cloudkms.New(authedClient)
	if err != nil {
		return err
	}
	location := "global"
	parent := fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeyVersions/%s",
		project, location, ring, version)

	_, err = client.Projects.Locations.KeyRings.CryptoKeys.CryptoKeyVersions.Patch(
		parent, &cloudkms.CryptoKeyVersion{
			State: "DISABLED",
		}).UpdateMask("state").Do()
	if err != nil {
		return err
	}
	log.Print("Disabled crypto key version.")

	return nil
}

Java

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Disables the given version of the crypto key.
 */
public static CryptoKeyVersion disableCryptoKeyVersion(
    String projectId, String ringId, String keyId, String version)
    throws IOException {
  String location = "global";
  // Create the Cloud KMS client.
  CloudKMS kms = createAuthorizedClient();

  // The resource name of the cryptoKey version
  String cryptoKeyVersion = String.format(
      "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s",
      projectId, location, ringId, keyId, version);

  CryptoKeyVersion newVersionState = new CryptoKeyVersion()
      .setState("DISABLED");

  CryptoKeyVersion response = kms.projects().locations().keyRings().cryptoKeys()
      .cryptoKeyVersions()
      .patch(cryptoKeyVersion, newVersionState)
      .setUpdateMask("state")
      .execute();

  System.out.println(response);
  return response;
}

Node.js

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

// Your Google Cloud Platform project ID
// const projectId = 'YOUR_PROJECT_ID';

// The location of the crypto key versions's key ring, e.g. "global"
// const location = 'global';

// The name of the crypto key version's key ring, e.g. "my-key-ring"
// const keyRingName = 'my-key-ring';

// The name of the version's crypto key, e.g. "my-key"
// const keyName = 'my-key';

// The version's id, e.g. 123
// const version = 123;

// Builds and authorizes a Cloud KMS client
buildAndAuthorizeService((err, cloudkms) => {
  if (err) {
    console.log(err);
    return;
  }

  let request = {
    // This will be a path parameter in the request URL
    name: `projects/${projectId}/locations/${location}/keyRings/${keyRingName}/cryptoKeys/${keyName}/cryptoKeyVersions/${version}`
  };

  // Gets a crypto key version
  cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.get(request, (err, cryptoKeyVersion) => {
    if (err) {
      console.log(err);
      return;
    }

    cryptoKeyVersion.state = 'DISABLED';

    request = {
      // This will be a path parameter in the request URL
      name: request.name,
      // This will be a query parameter in the request URL
      updateMask: 'state',
      // This will be the request body
      resource: cryptoKeyVersion
    };

    // Disables a crypto key version
    cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.patch(request, (err, cryptoKeyVersion) => {
      if (err) {
        console.log(err);
        return;
      }

      console.log(`Crypto key version ${cryptoKeyVersion.name} disabled.`);
    });
  });
});

function buildAndAuthorizeService (callback) {
  // Imports the Google APIs client library
  const google = require('googleapis');

  // Acquires credentials
  google.auth.getApplicationDefault((err, authClient) => {
    if (err) {
      callback(err);
      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/cloud-platform'
      ]);
    }

    // Instantiates an authorized client
    const cloudkms = google.cloudkms({
      version: 'v1',
      auth: authClient
    });

    callback(null, cloudkms);
  });
}

PHP

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Disable a CryptoKey version.
 *
 * @param string $projectId
 * @param string $ring
 * @param string $key
 * @param int $version
 * @param string $location [optional]
 * @return null
 */
function disable_cryptokey_version($projectId, $ring, $key, $version, $location = 'global')
{
    // Instantiate the client, authenticate, and add scopes.
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    // Create the Cloud KMS client.
    $kms = new Google_Service_CloudKMS($client);

    // The resource name of the KeyRing associated with the CryptoKey.
    $parent = sprintf('projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s',
        $projectId,
        $location,
        $ring,
        $key,
        $version
    );

    // Disable the CryptoKey version.
    $cryptoKeyVersion = $kms->projects_locations_keyRings_cryptoKeys_cryptoKeyVersions
        ->get($parent);
    $cryptoKeyVersion->setState('DISABLED');

    $kms->projects_locations_keyRings_cryptoKeys_cryptoKeyVersions->patch(
        $parent,
        $cryptoKeyVersion,
        ['updateMask' => 'state']
    );

    printf('Disabled version %s for key %s in keyring %s' . PHP_EOL, $version, $key, $ring);
}

Python

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

def disable_cryptokey_version(project_id, location, keyring, cryptokey,
                              version):
    """Disables a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing."""

    # Creates an API client for the KMS API.
    kms_client = googleapiclient.discovery.build('cloudkms', 'v1')

    # Construct the resource name of the CryptoKeyVersion.
    name = (
        'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}/'
        'cryptoKeyVersions/{}'
        .format(project_id, location, keyring, cryptokey, version))

    # Use the KMS API to disable the CryptoKeyVersion.
    cryptokeys = kms_client.projects().locations().keyRings().cryptoKeys()
    request = cryptokeys.cryptoKeyVersions().patch(
        name=name, body={'state': 'DISABLED'}, updateMask='state')
    response = request.execute()

    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response['state']))

Ruby

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

# project_id  = "Your Google Cloud project ID"
# key_ring_id = "The ID of the key ring"
# crypto_key  = "Name of the crypto key"
# version     = "Version of the crypto key"
# location    = "The location of the key ring"

require "google/apis/cloudkms_v1"

# Initialize the client and authenticate with the specified scope
Cloudkms = Google::Apis::CloudkmsV1
kms_client = Cloudkms::CloudKMSService.new
kms_client.authorization = Google::Auth.get_application_default(
  "https://www.googleapis.com/auth/cloud-platform"
)

# The resource name of the location associated with the key ring crypto key version
resource = "projects/#{project_id}/locations/#{location}/" +
           "keyRings/#{key_ring_id}/cryptoKeys/#{crypto_key}/" +
           "cryptoKeyVersions/#{version}"

# Get a version of the crypto key
crypto_key_version = kms_client.get_project_location_key_ring_crypto_key_crypto_key_version resource

# Set the primary version state as disabled for update
crypto_key_version.state = "DISABLED"

# Disable the crypto key version
kms_client.patch_project_location_key_ring_crypto_key_crypto_key_version(
  resource,
  crypto_key_version, update_mask: "state"
)

puts "Disabled version #{version} of #{crypto_key}"

Schedule a CryptoKeyVersion for destruction

Only CryptoKeyVersions which are Enabled or Disabled can be Scheduled for destruction. This is done with the method DestroyCryptoKeyVersion.

To prevent accidents, and damage from malicious individuals, when DestroyCryptoKeyVersion is used, the key material is NOT immediately Destroyed. Rather, the CryptoKeyVersion moves to Scheduled for destruction for 24 hours, after which it is automatically destroyed. There is no way to override this safety fallback. If you decide within 24 hours of scheduling the destruction that you do not want the destruction to occur, you can restore the CryptoKeyVersion.

Destruction is removal of the key material, but a record of the version still exists (e.g., the version number cannot be reused). This is NOT reversible - any data encrypted with this version will not be recoverable.

Command-line

Destroy version "42" of CryptoKey "answer" in KeyRing "answers"

gcloud kms keys versions destroy 42 --location global --keyring answers --key answer

C#

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

      public static object DestroyCryptoKeyVersion(string projectId, string location, string keyRing, string cryptoKey, string version)
      {
          var cloudKms = CreateAuthorizedClient();
          // Generate the full path of the parent to use for destroying the crypto key Version.
          var parent = $"projects/{projectId}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}/cryptoKeyVersions/{version}";
          DestroyCryptoKeyVersionRequest destroyRequest = new DestroyCryptoKeyVersionRequest();
          // Destroy crypto key version.
          var request = new ProjectsResource.LocationsResource.KeyRingsResource.CryptoKeysResource
              .CryptoKeyVersionsResource.DestroyRequest(cloudKms, destroyRequest, parent);
          var result = request.Execute();
          Console.Write($"Destroyed Crypto Key Version: {result.Name}");
          return 0;
      }

Go

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

func destroyCryptoKeyVersion(project, ring, key, version string) error {
	ctx := context.Background()
	authedClient, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope)
	if err != nil {
		return err
	}
	client, err := cloudkms.New(authedClient)
	if err != nil {
		return err
	}
	location := "global"
	parent := fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeyVersions/%s",
		project, location, ring, version)

	_, err = client.Projects.Locations.KeyRings.CryptoKeys.CryptoKeyVersions.Destroy(
		parent, &cloudkms.DestroyCryptoKeyVersionRequest{}).Do()
	if err != nil {
		return err
	}
	log.Print("Destroyed crypto key version.")

	return nil
}

Java

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Marks the given version of a crypto key to be destroyed at a scheduled future point.
 */
public static CryptoKeyVersion destroyCryptoKeyVersion(
    String projectId, String ringId, String keyId, String version)
    throws IOException {
  String location = "global";
  // Create the Cloud KMS client.
  CloudKMS kms = createAuthorizedClient();

  // The resource name of the cryptoKey version
  String cryptoKeyVersion = String.format(
      "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s",
      projectId, location, ringId, keyId, version);

  DestroyCryptoKeyVersionRequest destroyRequest = new DestroyCryptoKeyVersionRequest();

  CryptoKeyVersion destroyed = kms.projects().locations().keyRings().cryptoKeys()
      .cryptoKeyVersions()
      .destroy(cryptoKeyVersion, destroyRequest)
      .execute();

  System.out.println(destroyed);
  return destroyed;
}

Node.js

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

// Your Google Cloud Platform project ID
// const projectId = 'YOUR_PROJECT_ID';

// The location of the crypto key versions's key ring, e.g. "global"
// const location = 'global';

// The name of the crypto key version's key ring, e.g. "my-key-ring"
// const keyRingName = 'my-key-ring';

// The name of the version's crypto key, e.g. "my-key"
// const keyName = 'my-key';

// The version's id, e.g. 123
// const version = 123;

// Builds and authorizes a Cloud KMS client
buildAndAuthorizeService((err, cloudkms) => {
  if (err) {
    console.log(err);
    return;
  }

  const request = {
    // This will be a path parameter in the request URL
    name: `projects/${projectId}/locations/${location}/keyRings/${keyRingName}/cryptoKeys/${keyName}/cryptoKeyVersions/${version}`
  };

  // Destroys a crypto key version
  cloudkms.projects.locations.keyRings.cryptoKeys.cryptoKeyVersions.destroy(request, (err, cryptoKeyVersion) => {
    if (err) {
      console.log(err);
      return;
    }

    console.log(`Crypto key version ${cryptoKeyVersion.name} destroyed.`);
  });
});

function buildAndAuthorizeService (callback) {
  // Imports the Google APIs client library
  const google = require('googleapis');

  // Acquires credentials
  google.auth.getApplicationDefault((err, authClient) => {
    if (err) {
      callback(err);
      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/cloud-platform'
      ]);
    }

    // Instantiates an authorized client
    const cloudkms = google.cloudkms({
      version: 'v1',
      auth: authClient
    });

    callback(null, cloudkms);
  });
}

PHP

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

/**
 * Destroy a CryptoKey version.
 *
 * @param string $projectId
 * @param string $ring
 * @param string $key
 * @param string $version
 * @param string $location [optional]
 * @return Google_Service_CloudKMS_CryptoKeyVersion
 */
function destroy_cryptokey_version($projectId, $ring, $key, $version, $location = 'global')
{
    // Instantiate the client, authenticate, and add scopes.
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope('https://www.googleapis.com/auth/cloud-platform');

    // Create the Cloud KMS client.
    $kms = new Google_Service_CloudKMS($client);

    // The resource name of the CryptoKey version.
    $parent = sprintf('projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s',
        $projectId,
        $location,
        $ring,
        $key,
        $version
    );

    // Destroy the CryptoKey version.
    $request = new Google_Service_CloudKMS_DestroyCryptoKeyVersionRequest();
    $kms->projects_locations_keyRings_cryptoKeys_cryptoKeyVersions->destroy(
        $parent,
        $request
    );

    printf('Destroyed version %s for key %s in keyring %s' . PHP_EOL, $version, $key, $ring);
}

Python

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

def destroy_cryptokey_version(
        project_id, location, keyring, cryptokey, version):
    """Schedules a CryptoKeyVersion associated with a given CryptoKey and
    KeyRing for destruction 24 hours in the future."""

    # Creates an API client for the KMS API.
    kms_client = googleapiclient.discovery.build('cloudkms', 'v1')

    # Construct the resource name of the CryptoKeyVersion.
    name = (
        'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}/'
        'cryptoKeyVersions/{}'
        .format(project_id, location, keyring, cryptokey, version))

    # Use the KMS API to schedule the CryptoKeyVersion for destruction.
    cryptokeys = kms_client.projects().locations().keyRings().cryptoKeys()
    request = cryptokeys.cryptoKeyVersions().destroy(name=name, body={})
    response = request.execute()

    print('CryptoKeyVersion {}\'s state has been set to {}.'.format(
        name, response['state']))

Ruby

For more on installing and creating a Cloud KMS client, refer to Cloud KMS Client Libraries.

# project_id  = "Your Google Cloud project ID"
# key_ring_id = "The ID of the key ring"
# crypto_key  = "Name of the crypto key"
# version     = "Version of the crypto key"
# location    = "The location of the key ring"

require "google/apis/cloudkms_v1"

# Initialize the client and authenticate with the specified scope
Cloudkms = Google::Apis::CloudkmsV1
kms_client = Cloudkms::CloudKMSService.new
kms_client.authorization = Google::Auth.get_application_default(
  "https://www.googleapis.com/auth/cloud-platform"
)

# The resource name of the location associated with the key ring crypto key version
resource = "projects/#{project_id}/locations/#{location}/" +
           "keyRings/#{key_ring_id}/cryptoKeys/#{crypto_key}/" +
           "cryptoKeyVersions/#{version}"

# Destroy specific version of the crypto key
kms_client.destroy_crypto_key_version(
  resource,
  Cloudkms::DestroyCryptoKeyVersionRequest.new
)

puts "Destroyed version #{version} of #{crypto_key}"

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Cloud KMS Documentation