All Products
Search
Document Center

Key Management Service:GenerateAndExportDataKey

Last Updated:Jun 10, 2026

Menghasilkan kunci data acak, mengenkripsinya menggunakan CMK dan kunci publik yang Anda tentukan, lalu mengembalikan kedua ciphertext.

Deskripsi operasi

Notes

  • For more information about the access policy required for a RAM user or RAM role to use this operation, see Resource Access Management.

  • This operation is accessible through a shared gateway or a dedicated gateway. For more information, see Alibaba Cloud SDK.

    • Shared gateway: You can access KMS over the Internet or a VPC. To access KMS over the Internet, you must enable Internet access. For more information, see Access KMS instances over the Internet.

    • Dedicated gateway: You can access KMS using the private endpoint of KMS (<YOUR_KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com).

Description

We recommend that you import the data key to a cryptographic module for data encryption and data decryption as follows:

1. Call the GenerateAndExportDataKey operation to obtain the data key encrypted by a KMS key and a specified public key.

2. Save the ciphertext of the data key that is encrypted by the KMS key to KMS or a storage service, such as ApsaraDB, for key backup and recovery.

3. Import the ciphertext of the data key that is encrypted by the public key to the cryptographic module that contains the corresponding private key. This process distributes the key from KMS to the cryptographic module. You can then use the data key to encrypt and decrypt data.

Catatan

The KMS key that you specify in the request is used only to encrypt the data key and is not used to generate the data key. KMS does not record or store the randomly generated data key. You are responsible for recording the data key or its ciphertext.

Coba sekarang

Coba API ini di OpenAPI Explorer tanpa perlu penandatanganan manual. Panggilan yang berhasil akan secara otomatis menghasilkan contoh kode SDK sesuai dengan parameter Anda. Unduh kode tersebut dengan kredensial bawaan yang aman untuk penggunaan lokal.

Test

RAM authorization

Tidak ada otorisasi untuk operasi ini. Jika Anda mengalami masalah saat menjalankan operasi ini, hubungi dukungan teknis.

Parameter permintaan

Parameter

Type

Required

Description

Example

KeyId

string

Yes

ID kunci. Anda juga dapat menentukan alias atau Nama Sumber Daya Alibaba Cloud (ARN) kunci. Untuk informasi selengkapnya tentang alias, lihat Mengelola alias.

Catatan

Untuk mengakses kunci di akun Alibaba Cloud lain, Anda harus menentukan ARN kunci. ARN kunci berformat acs:kms:${Wilayah}:${account}:key/${keyid}.

1234abcd-12ab-34cd-56ef-12345678****

KeySpec

string

No

Panjang kunci data yang ingin Anda hasilkan. Nilai valid:

  • AES_256: kunci simetris 256-bit.

  • AES_128: kunci simetris 128-bit.

Catatan

Kami menyarankan Anda menggunakan parameter KeySpec atau NumberOfBytes untuk menentukan panjang kunci data. Jika Anda tidak menentukan salah satu parameter tersebut, KMS akan menghasilkan kunci data 256-bit. Jika Anda menentukan kedua parameter, KMS akan mengabaikan parameter KeySpec.

AES_256

NumberOfBytes

integer

No

Panjang kunci data yang ingin Anda hasilkan.

Nilai valid: 1 hingga 1024.

Unit: bytes.

32

EncryptionContext

object

No

String JSON dalam format pasangan kunci-nilai. Jika Anda menentukan parameter ini, Anda harus menentukan parameter yang sama saat memanggil operasi Decrypt atau operasi lain untuk mengenkripsi ulang kunci data. Untuk informasi selengkapnya, lihat EncryptionContext.

{"Example":"Example"}

PublicKeyBlob

string

Yes

Kunci publik yang dikodekan dalam Base64.

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAndKfC2ReLL2+y8a0+ZBBeAft/uBYo86GZiYJuflqgUzKxpyuvlo3uQkBv6b+nx+0tz8g8v7GhpPWMSW5L9mNHYsvYFsa7jTxsYdt17yj6GlUHPuMIs8hr5qbwl38IHU1iIa7nYWwE2fb3ePOvLDACRJVgGpU0yxioW80d2QD+9aU4jF5dlAahcfgsNzo2CXzCUc1+xbmNuq7Rp+H9VJB9dyYOwqnW3RhOLBo21FzpORapf0UiRlrHRpk1V6ez+aE1dofaYh/9bh0m6ioxj7j5hpZbWccuEZTMBKd+cbuBkRhJzc6Tti6qwZbDiu4fUwbZS0Tqpuo1UadiyxMW********

WrappingKeySpec

string

Yes

Tipe kunci yang ditentukan oleh PublicKeyBlob, dalam format berikut. Untuk informasi selengkapnya tentang tipe kunci, lihat Pengenalan kunci asimetris.
Nilai valid:

  • RSA_2048

  • EC_SM2.

RSA_2048

WrappingAlgorithm

string

Yes

Algoritma enkripsi yang digunakan untuk mengenkripsi kunci data menggunakan kunci publik yang ditentukan oleh PublicKeyBlob, dalam format berikut. Untuk informasi selengkapnya tentang algoritma enkripsi, lihat AsymmetricDecrypt.
Nilai valid:

  • RSAES_OAEP_SHA_256

  • RSAES_OAEP_SHA_1

  • SM2PKE.

RSAES_OAEP_SHA_256

DryRun

string

No

Apakah akan mengaktifkan fitur dry run.

  • true: mengaktifkan fitur.

  • false (default): menonaktifkan fitur.

Mode DryRun digunakan untuk menguji panggilan API dan memverifikasi izin pada Sumber daya yang dapat Anda akses serta validitas parameter permintaan. Jika Anda mengaktifkan mode DryRun, KMS akan selalu mengembalikan tanggapan kegagalan beserta penyebab kegagalannya. Penyebab kegagalan berikut disertakan:

  • DryRunOperationError: Permintaan akan berhasil jika parameter DryRun tidak ditentukan.

  • ValidationError: Parameter yang ditentukan dalam permintaan tidak valid.

  • AccessDeniedError: Anda tidak memiliki otorisasi untuk melakukan operasi ini pada Sumber daya KMS.

false

Elemen respons

Element

Type

Description

Example

object

KeyVersionId

string

The ID of the key version that is used to encrypt the plaintext. It is the primary version of the specified KMS key.

2ab1a983-7072-4bbc-a582-584b5bd8****

KeyId

string

The ID of the key. If you use a key alias or key ARN in the request, the key ID is returned.

599fa825-17de-417e-9554-bb032cc6****

CiphertextBlob

string

The ciphertext of the data key. The data key is encrypted using the primary version of the specified KMS key.

ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS****

RequestId

string

The ID of the request, which is a unique identifier generated by Alibaba Cloud. You can use the request ID to troubleshoot issues.

7021b6ec-4be7-4d3c-8a68-1e85d4d515a0

ExportedDataKey

string

The exported data key that is protected by the public key.

BQKP+1zK6+ZEMxTP5qaVzcsgXtWplYBKm0NXdSnB5FzliFxE1bSiu4dnEIlca2JpeH7yz1/S6fed630H+hIH6DoM25fTLNcKj+mFB0Xnh9m2+HN59Mn4qyTfcUeadnfCXSWcGBouhXFwcdd2rJ3n337bzTf4jm659gZu3L0i6PLuxM9p7mqdwO0cKJPfGVfhnfMz+f4alMg79WB/NNyE2lyX7/qxvV49ObNrrJbKSFiz8Djocaf0IESNLMbfYI5bXjWkJlX92DQbKhibtQW8ZOJ//ZC6t0AWcUoKL6QDm/dg5koQalcleRinpB+QadFm894sLbVZ9+N4GVs*******

Contoh

Respons sukses

JSONformat

{
  "KeyVersionId": "2ab1a983-7072-4bbc-a582-584b5bd8****",
  "KeyId": "599fa825-17de-417e-9554-bb032cc6****",
  "CiphertextBlob": "ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS****",
  "RequestId": "7021b6ec-4be7-4d3c-8a68-1e85d4d515a0",
  "ExportedDataKey": "BQKP+1zK6+ZEMxTP5qaVzcsgXtWplYBKm0NXdSnB5FzliFxE1bSiu4dnEIlca2JpeH7yz1/S6fed630H+hIH6DoM25fTLNcKj+mFB0Xnh9m2+HN59Mn4qyTfcUeadnfCXSWcGBouhXFwcdd2rJ3n337bzTf4jm659gZu3L0i6PLuxM9p7mqdwO0cKJPfGVfhnfMz+f4alMg79WB/NNyE2lyX7/qxvV49ObNrrJbKSFiz8Djocaf0IESNLMbfYI5bXjWkJlX92DQbKhibtQW8ZOJ//ZC6t0AWcUoKL6QDm/dg5koQalcleRinpB+QadFm894sLbVZ9+N4GVs*******"
}

Kode kesalahan

HTTP status code

Error code

Error message

Description

400 InvalidParameter The specified parameter is not valid. An invalid value is specified for the parameter.
500 InternalFailure InternalFailure
404 Forbidden.KeyNotFound The specified Key is not found. The error message returned because the specified CMK does not exist.
404 InvalidAccessKeyId.NotFound The Access Key ID provided does not exist in our records.

Lihat Error Codes untuk daftar lengkap.

Catatan rilis

Lihat Release Notes untuk daftar lengkap.