创建密钥

本页面介绍如何在 Cloud KMS 中创建密钥。密钥可以是以下类型:对称或非对称加密密钥、非对称签名密钥或 MAC 签名密钥。

创建密钥时,您将其添加到特定 Cloud KMS 位置中的密钥环。您可以创建新的密钥环,也可以使用现有的密钥环。 在此页面中,您将生成一个新的 Cloud KMS 密钥,并将其添加到现有密钥环中。 如需创建 Cloud EKM 密钥,请参阅创建外部密钥。如需导入 Cloud KMS 或 Cloud HSM 密钥,请参阅导入密钥

准备工作

在完成本页面上的任务之前,您需要具备以下条件:

  1. 用于包含 Cloud KMS 资源的 Google Cloud 项目资源。我们建议您为 Cloud KMS 资源使用单独的项目,该项目不包含任何其他 Google Cloud 资源。
  2. 您要在其中创建密钥的密钥环的名称和位置。 选择一个位置靠近其他资源且支持所选防护等级的密钥环。 如需创建密钥环,请参阅创建密钥环

所需的角色

如需获得创建密钥所需的权限,请让管理员为您授予项目或父级资源的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

这些预定义角色包含创建密钥所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

创建密钥需要以下权限:

  • cloudkms.cryptoKeys.create
  • cloudkms.cryptoKeys.get
  • cloudkms.cryptoKeys.list
  • cloudkms.cryptoKeyVersions.create
  • cloudkms.cryptoKeyVersions.get
  • cloudkms.cryptoKeyVersions.list
  • cloudkms.keyRings.get
  • cloudkms.keyRings.list
  • cloudkms.locations.get
  • cloudkms.locations.list
  • resourcemanager.projects.get
  • 如需检索公钥,请执行以下操作: cloudkms.cryptoKeyVersions.viewPublicKey
  • 如需创建单租户 HSM 密钥,请执行以下操作:
    • cloudkms.singleTenantHsmInstances.get
    • cloudkms.singleTenantHsmInstances.use

您也可以使用自定义角色或其他预定义角色来获取这些权限。

创建对称加密密钥

控制台

  1. 在 Google Cloud 控制台中,前往密钥管理页面。

    前往 Key Management

  2. 点击您要为其创建密钥的密钥环的名称。

  3. 点击创建密钥

  4. 密钥名称部分,输入密钥的名称。

  5. 对于保护级别,请选择软件

  6. 对于密钥材料,请选择生成的密钥

  7. 对于用途,选择 Symmetric encrypt/decrypt

  8. 接受轮替周期开始日期的默认值。

  9. 点击创建

gcloud

如需在命令行上使用 Cloud KMS,请先安装或升级到最新版本的 Google Cloud CLI

如需创建软件密钥,请使用 kms keys create 命令:

gcloud kms keys create KEY_NAME \
    --keyring KEY_RING \
    --location LOCATION \
    --purpose "encryption"

替换以下内容:

  • KEY_NAME:密钥的名称。
  • KEY_RING:包含密钥的密钥环的名称。
  • LOCATION:密钥环的 Cloud KMS 位置。

如需了解所有标志和可能值,请使用 --help 标志运行命令。

C#

要运行此代码,请先设置 C# 开发环境安装 Cloud KMS C# SDK


using Google.Cloud.Kms.V1;

public class CreateKeySymmetricEncryptDecryptSample
{
    public CryptoKey CreateKeySymmetricEncryptDecrypt(
      string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring",
      string id = "my-symmetric-encryption-key")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the parent key ring name.
        KeyRingName keyRingName = new KeyRingName(projectId, locationId, keyRingId);

        // Build the key.
        CryptoKey key = new CryptoKey
        {
            Purpose = CryptoKey.Types.CryptoKeyPurpose.EncryptDecrypt,
            VersionTemplate = new CryptoKeyVersionTemplate
            {
                Algorithm = CryptoKeyVersion.Types.CryptoKeyVersionAlgorithm.GoogleSymmetricEncryption,
            }
        };

        // Call the API.
        CryptoKey result = client.CreateCryptoKey(keyRingName, id, key);

        // Return the result.
        return result;
    }
}

Go

要运行此代码,请先设置 Go 开发环境安装 Cloud KMS Go SDK

import (
	"context"
	"fmt"
	"io"

	kms "cloud.google.com/go/kms/apiv1"
	"cloud.google.com/go/kms/apiv1/kmspb"
)

// createKeySymmetricEncryptDecrypt creates a new symmetric encrypt/decrypt key
// on Cloud KMS.
func createKeySymmetricEncryptDecrypt(w io.Writer, parent, id string) error {
	// parent := "projects/my-project/locations/us-east1/keyRings/my-key-ring"
	// id := "my-symmetric-encryption-key"

	// Create the client.
	ctx := context.Background()
	client, err := kms.NewKeyManagementClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create kms client: %w", err)
	}
	defer client.