This page shows how to create a symmetric key for encryption.
You can also create an asymmetric key, a Cloud HSM key, or a Cloud External Key Manager key.
Overview
When you create a key, you add it to a key ring in a given Google Cloud location. You can create a new key ring or use an existing one. In this topic, you create a new key ring and add a new key to it.
Create a key ring
Follow these steps to create a key ring for your new key. If you want to use an existing key ring instead, you can create a key.
Web UI
Go to the Cryptographic Keys page in the Cloud Console.
Click Create key ring.
In the Key ring name field, enter the desired name for your key ring.
From the Key ring location dropdown, select a location like
"us-east1"
.Click Create.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keyrings create key-ring \ --location location
Replace key-ring with a name for the key ring. Replace location with the Cloud KMS location for the key ring and its keys.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
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.
curl "https://cloudkms.googleapis.com/v1/projects/project-id/locations/location-id/keyRings" \ --request "POST" \ --header "authorization: Bearer token" \ --header "content-type: application/json" \ --header "x-goog-user-project: project-id" \ --data "{\"name\": {\"key-ring-name\": {}}}"
See the KeyRing.create
API documentation
for more information.
Create a key
Follow these steps to create a symmetric key on the specified key ring and location.
Web UI
Go to the Cryptographic Keys page in the Cloud Console.
Click the name of the key ring for which you will create a key.
Click Create key.
In the What type of key do you want to create?, choose Generated key.
In the Key name field, enter the name for your key.
Click the Protection level dropdown and select Software.
Click the Purpose dropdown and select Symmetric encrypt/decrypt.
Accept the default values for Rotation period and Starting on.
Click Create.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys create key \ --keyring key-ring \ --location location \ --purpose "encryption"
Replace key with the name for the key. Replace key-ring with the name of the key ring where the key will be located. Replace location with the Cloud KMS location of the key ring.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Set a key rotation period and starting time
A key can be created with a specified rotation period, which is the time between when new key versions are generated automatically. A key can also be created with a specified next rotation time.
Web UI
When you use the Google Cloud Console to create a key, if you don't specify your own rotation period and next rotation time, Cloud KMS will set the key's rotation period and next rotation time automatically.
To specify a different rotation period and starting time, when you are creating your key, but before you click the Create button:
Click the Rotation period dropdown and then select a value for the rotation period.
Click the date in the Starting on field and then select a date for the next rotation.
Command-line
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Cloud SDK.
gcloud kms keys create key \ --keyring key-ring \ --location location \ --purpose "encryption" \ --rotation-period rotation-period \ --next-rotation-time next-rotation-time
Replace key with a name for the key. Replace key-ring with
the name of the existing key ring where the key will be located. Replace
location with the Cloud KMS location for the key ring.
Replace rotation-period with an interval, such as 30d
to rotate the
key every 30 days. Replace next-rotation-time with a timestamp at
which to begin the first rotation, such as "1970-01-01T01:02:03"
.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
Manually create new key versions
In addition to automatic rotation, you can manually rotate keys. For more details, see Rotating keys.