GenerateDataKey
Generates a random data key (DK) for local data encryption.
Operation description
-
For details about the access policies that must be granted to a Resource Access Management (RAM) user or RAM role to invoke this operation, see Access control.
-
You can invoke this operation through a shared gateway or a dedicated gateway. For more information, see Alibaba Cloud SDK.
Shared gateway: Access KMS over the Internet or through a VPC endpoint. This method requires that you enable public network access. For more information, see Access keys in a KMS instance over the Internet.
Dedicated gateway: Access KMS through the KMS private endpoint (
<YOUR_KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com).
QPS limits
Calls through a shared gateway: The QPS limit for a single user is 1,000 calls per second. If you exceed this limit, API calls are throttled, which may affect your business. Call this operation at a reasonable rate.
Calls through a dedicated gateway: The QPS limit for a single user is determined by the performance specifications of your KMS instance. For more information, see Performance data.
Details
This operation generates a random data key (DK), encrypts it by using the customer master key (CMK) that you specify, and returns both the plaintext and ciphertext of the DK. Use the plaintext DK to encrypt data locally outside of KMS. When you store the encrypted data, also store the ciphertext of the DK. You can retrieve the plaintext DK from the Plaintext field and the ciphertext DK from the CiphertextBlob field in the response.
The CMK specified in the request is used only to encrypt the DK and has no effect on DK generation. KMS does not record or store the randomly generated DK. You are responsible for persisting the DK ciphertext.
Use the following procedure to encrypt data locally:
-
Invoke the GenerateDataKey operation to obtain a DK for data encryption.
-
Use the plaintext DK (returned in the Plaintext field of the response) to encrypt data locally, then purge the plaintext DK from memory.
-
Store the DK ciphertext (returned in the CiphertextBlob field of the response) together with the locally encrypted data.
To decrypt data locally:
Invoke the Decrypt operation to decrypt the locally stored DK ciphertext. This operation returns the plaintext DK.
Use the plaintext DK to decrypt data locally, then purge the plaintext DK from memory.
This topic provides an example of generating a random DK for the key with the ID key-hzz630494463ejqjx****.
Try it now
Test
RAM authorization
|
Action |
Access level |
Resource type |
Condition key |
Dependent action |
|
kms:GenerateDataKey |
get |
*Key
|
|
None |
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
| KeyId |
string |
Yes |
The ID of the key. You can also specify a key alias or a key Amazon Resource Name (ARN). For more information about aliases, see Manage key aliases. Note
To access a key in another Alibaba Cloud account, you must specify the key ARN. The key ARN format is |
key-hzz630494463ejqjx**** |
| KeySpec |
string |
No |
The length of the data key (DK) to generate. Valid values:
Note
Use KeySpec or NumberOfBytes to specify the DK length. If neither is specified, KMS generates a 256-bit DK. If both are specified, KMS ignores the KeySpec parameter. |
AES_256 |
| NumberOfBytes |
integer |
No |
The length of the data key (DK) to generate, in bytes. Valid values: 1 to 1024. Default value:
|
256 |
| EncryptionContext |
object |
No |
A JSON string of key-value pairs. If you specify this parameter, you must provide the same parameter when you call the Decrypt operation. For more information, see EncryptionContext. |
{"Example":"Example"} |
| DryRun |
string |
No |
Specifies whether to enable DryRun mode.
DryRun mode is used to test API calls, verify that you have the required permissions on the relevant resources, and check whether request parameters are configured correctly. When DryRun mode is enabled, KMS always returns a failure with a reason. Possible failure reasons include:
|
false |
| Recipient |
string |
No |
{ "AttestationDocument":"base64-encoded-attestion-document", "KeyEncryptionAlgorithm":"RSAES_OAEP_SHA_256" } |
For details about common request parameters, see Common parameters.
Response elements
|
Element |
Type |
Description |
Example |
|
object |
|||
| KeyVersionId |
string |
The key version ID. The globally unique identifier of the CMK version. |
2ab1a983-7072-4bbc-a582-584b5bd8**** |
| KeyId |
string |
The key ID. If the KeyId parameter in the request uses a key alias or key ARN, the key ID is also returned in the response. |
key-hzz630494463ejqjx**** |
| CiphertextBlob |
string |
The ciphertext of the data key (DK) encrypted by the primary version of the specified key. |
ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS**** |
| RequestId |
string |
The request ID. The unique identifier generated by Alibaba Cloud for this request. You can use this ID to troubleshoot and locate issues. |
7021b6ec-4be7-4d3c-8a68-1e85d4d515a0 |
| Plaintext |
string |
The Base64 encoding of the plaintext of the data key (DK). |
QmFzZTY0IGVuY29kZWQgcGxhaW50**** |
| CiphertextForRecipient |
string |
NIahY6pgjK4ZMP2R0EmsmBqntrv0AI2rcDyU7Su6uOT9Le7EOvlCpjHJfr9z3M0vkfulQoyuETmKSpYDfixE3auE4MwxloT6D9Gfsk6hm5FV2iAxL//Ms2kLv6K4z6yGi7lKm2yjX4***== |
Examples
Success response
JSON format
{
"KeyVersionId": "2ab1a983-7072-4bbc-a582-584b5bd8****",
"KeyId": "key-hzz630494463ejqjx****",
"CiphertextBlob": "ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS****",
"RequestId": "7021b6ec-4be7-4d3c-8a68-1e85d4d515a0",
"Plaintext": "QmFzZTY0IGVuY29kZWQgcGxhaW50****",
"CiphertextForRecipient": "NIahY6pgjK4ZMP2R0EmsmBqntrv0AI2rcDyU7Su6uOT9Le7EOvlCpjHJfr9z3M0vkfulQoyuETmKSpYDfixE3auE4MwxloT6D9Gfsk6hm5FV2iAxL//Ms2kLv6K4z6yGi7lKm2yjX4***=="
}
Error codes
|
HTTP status code |
Error code |
Error message |
Description |
|---|---|---|---|
| 400 | UnsupportedOperation | This action is not supported. | The operation is not supported. |
| 404 | Forbidden.AliasNotFound | The specified Alias is not found. | The error message returned because the specified alias does not exist. |
| 404 | Forbidden.KeyNotFound | The specified Key is not found. | The error message returned because the specified CMK does not exist. |
| 409 | Rejected.Disabled | The request was rejected because the key state is Disabled. | The request was rejected because the key state is Disabled. |
| 409 | Rejected.PendingDeletion | The request was rejected because the key state is PendingDeletion. | The request was rejected because the key state is PendingDeletion. |
| 409 | Rejected.Unavailable | The request was rejected because the key state is Unavailable. | The request was denied because the key status is unavailable. |
See Error Codes for a complete list.
Release notes
See Release Notes for a complete list.