Generates a random data key that is used to encrypt on-premises data.
Usage notes
- Keys outside Key Management Service (KMS) instances: To perform cryptographic operations, use Alibaba Cloud SDK to call operations.
- Keys in KMS instances: To perform cryptographic operations, use one of the following methods:
- Method 1 (recommended): Use KMS Instance SDK to call KMS Instance API operations. For more information, see KMS Instance SDK and KMS Instance API.
- Method 2: Use Alibaba Cloud SDK to call operations. The authentication method supports only Resource Access Management (RAM) roles whose trusted entities are Alibaba Cloud services. For more information, see Create a RAM role for a trusted Alibaba Cloud service.
Limits
You can call this operation up to 750 times per second per account. If the number of calls per second exceeds the limit, throttling is triggered. As a result, your business may be affected. We recommend that you take note of the limit when you call this operation.
Description
This operation creates a random data key, encrypts the data key by using a key, and returns the plaintext and ciphertext of the data key. You can use the data key plaintext to encrypt on-premises data without using KMS. You must store the data ciphertext together with the data key ciphertext. You can obtain the data key plaintext from Plaintext in the response and the data key ciphertext from CiphertextBlob in the response.
The key that you specify in the request is used to only encrypt the data key and is not involved in the generation of the data key. KMS does not record or store the data key. Therefore, you must store the data key ciphertext in persistent storage.
We recommend that you encrypt on-premises data by performing the following steps:
1. Call the GenerateDataKey operation to obtain a data key that is used for data encryption.
2. Use the data key plaintext that you obtain to encrypt on-premises data without using KMS. Then, delete the data key plaintext from the memory.
3. Store the data ciphertext together with the data key ciphertext that you obtain.
We recommend that you decrypt on-premises data by performing the following steps:
- Call the Decrypt operation to decrypt the locally stored data key ciphertext. The data key plaintext is returned.
- Use the data key plaintext to decrypt on-premises data and then delete the data key plaintext from the memory.
In this example, a random data key is generated for a key whose ID is key-hzz630494463ejqjx****
.
Debugging
Request parameters
Parameter |
Type |
Required |
Example |
Description |
Action | String | Yes | GenerateDataKey | The operation that you want to perform. Set the value to GenerateDataKey. |
KeyId | String | Yes | key-hzz630494463ejqjx**** | The ID, alias, or Alibaba Cloud Resource Name (ARN) of the key. For more information, see Manage a key alias. Note When you access a key within another Alibaba Cloud account, you must enter the ARN of the key. The ARN of the key is in the acs:kms:${region}:${account}:key/${keyid} format. |
KeySpec | String | No | AES_256 | The type of the data key that you want to generate. Valid values:
Note We recommend that you use KeySpec or NumberOfBytes to specify the length of a data key. If none of the parameters are specified, KMS generates a 256-bit data key. If both parameters are specified, KMS ignores the KeySpec parameter. |
NumberOfBytes | Integer | No | 256 | The length of the data key that you want to generate. Unit: bytes. Valid values: 1 to 1024. Default value:
|
EncryptionContext | Map | No | {"Example":"Example"} | The JSON string that consists of key-value pairs. If you configure this parameter, an equivalent value is required when you call the Decrypt operation. For more information, see EncryptionContext. |
DryRun | String | No | false | Specifies whether to perform a dry run. Valid values:
A dry run is used for testing in API calls. You can perform a dry run to check whether you have the permissions to access the resources that you want to manage and whether the request parameters are correctly configured. If you set DryRun to true in the request, KMS always returns an error code that indicates the cause of the error. KMS may return the following error codes:
|
For more information about common request parameters, see Common parameters.
Response parameters
Parameter |
Type |
Example |
Description |
KeyVersionId | String | 2ab1a983-7072-4bbc-a582-584b5bd8**** | The version ID of the key. The ID must be globally unique. |
KeyId | String | key-hzz630494463ejqjx**** | The ID of the key. If KeyId is set to the alias or ARN of the key, the ID of the key is returned. |
CiphertextBlob | String | ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS**** | The ciphertext of the data key that is encrypted by using the primary version of the key. |
RequestId | String | 7021b6ec-4be7-4d3c-8a68-1e85d4d515a0 | The request ID. |
Plaintext | String | QmFzZTY0IGVuY29kZWQgcGxhaW50**** | The Base64-encoded plaintext of the data key. |
Examples
Sample requests
http(s)://[Endpoint]/?Action=GenerateDataKey
&KeyId=key-hzz630494463ejqjx****
&KeySpec=AES_256
&NumberOfBytes=256
&DryRun=false
&<Common request parameters>
Sample success responses
XML
format
HTTP/1.1 200 OK
Content-Type:application/xml
<GenerateDataKeyResponse>
<KeyVersionId>2ab1a983-7072-4bbc-a582-584b5bd8****</KeyVersionId>
<KeyId>key-hzz630494463ejqjx****</KeyId>
<CiphertextBlob>ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS****</CiphertextBlob>
<RequestId>7021b6ec-4be7-4d3c-8a68-1e85d4d515a0</RequestId>
<Plaintext>QmFzZTY0IGVuY29kZWQgcGxhaW50****</Plaintext>
</GenerateDataKeyResponse>
JSON
format
HTTP/1.1 200 OK
Content-Type:application/json
{
"KeyVersionId" : "2ab1a983-7072-4bbc-a582-584b5bd8****",
"KeyId" : "key-hzz630494463ejqjx****",
"CiphertextBlob" : "ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS****",
"RequestId" : "7021b6ec-4be7-4d3c-8a68-1e85d4d515a0",
"Plaintext" : "QmFzZTY0IGVuY29kZWQgcGxhaW50****"
}
Error codes
HttpCode |
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 specified alias is not found. |
404 | Forbidden.KeyNotFound | The specified Key is not found. | The specified key does not exist. |
409 | Rejected.Disabled | The request was rejected because the key state is Disabled. | The request is denied because the key is disabled. |
409 | Rejected.PendingDeletion | The request was rejected because the key state is PendingDeletion. | The request is denied because the key is in the Pending Deletion state. |
409 | Rejected.Unavailable | The request was rejected because the key state is Unavailable. | The request is denied because the key is unavailable. |
For a list of error codes, see Service error codes.