Randomly generates a data key and uses a Key Management Service (KMS) key and a public key to encrypt the data key. This operation returns the ciphertext of the data key that is encrypted by using the key and the ciphertext of the data key that is encrypted by using the public key.
Usage notes
This API can be called through either a shared gateway or a dedicated gateway. For more information, see Alibaba Cloud SDK.
Shared gateway: Accesses KMS through public network or VPC domain names, using one of the following formats:
Public network domain names:
kms.<REGION_ID>.aliyuncs.com
.VPC domain names:
kms-vpc.<REGION_ID>.aliyuncs.com
.
This method requires enabling public network access. For instructions, see Access KMS instance keys over the Internet.
Dedicated gateway: Access KMS through the KMS private network domain names, following the format:
<YOUR_KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com
.
Description
We recommend that you perform the following steps to import your data key to a cryptographic module:
1. Call the GenerateAndExportDataKey operation to obtain the ciphertext of the data key that is encrypted by using a KMS key and the ciphertext of the data key that is encrypted by using a public key.
2. Store the ciphertext of the data key that is encrypted by using a key in KMS or in a storage service such as ApsaraDB. This ciphertext is used for backup and restoration.
3. Import the ciphertext of the data key that is encrypted by using the public key to a cryptographic module where paired private keys are stored. This way, the data key is distributed from KMS to the cryptographic module. You can use the data key to encrypt or decrypt data.
The KMS key that you specify in the request of this operation 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 that is randomly generated by calling this operation. You must take note of the data key and the returned data key ciphertext.
Debugging
Request parameters
Parameter | Type | Required | Example | Description |
Action | String | Yes | GenerateAndExportDataKey | The operation that you want to perform. Set the value to GenerateAndExportDataKey. |
KeyId | String | Yes | 1234abcd-12ab-34cd-56ef-12345678**** | 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 |
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 | 32 | The length of the data key that you want to generate. Valid values: 1 to 1024. Unit: bytes. |
EncryptionContext | Map | No | {"Example":"Example"} | The JSON string that consists of key-value pairs. If you specify this parameter, an equivalent value is required when you decrypt or re-encrypt the data key. For more information, see EncryptionContext. |
PublicKeyBlob | String | Yes | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAndKfC2ReLL2+y8a0+ZBBeAft/uBYo86GZiYJuflqgUzKxpyuvlo3uQkBv6b+nx+0tz8g8v7GhpPWMSW5L9mNHYsvYFsa7jTxsYdt17yj6GlUHPuMIs8hr5qbwl38IHU1iIa7nYWwE2fb3ePOvLDACRJVgGpU0yxioW80d2QD+9aU4jF5dlAahcfgsNzo2CXzCUc1+xbmNuq7Rp+H9VJB9dyYOwqnW3RhOLBo21FzpORapf0UiRlrHRpk1V6ez+aE1dofaYh/9bh0m6ioxj7j5hpZbWccuEZTMBKd+cbuBkRhJzc6Tti6qwZbDiu4fUwbZS0Tqpuo1UadiyxMW******** | The Base64-encoded public key. |
WrappingKeySpec | String | Yes | RSA_2048 | The type of the public key specified by PublicKeyBlob. For more information about key types, see Introduction to asymmetric keys. Valid values:
|
WrappingAlgorithm | String | Yes | RSAES_OAEP_SHA_256 | The encryption algorithm based on which you use the public key specified by PublicKeyBlob to encrypt the data key. For more information about encryption algorithms, see AsymmetricDecrypt. Valid values:
|
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:
|
Response parameters
Parameter | Type | Example | Description |
KeyVersionId | String | 2ab1a983-7072-4bbc-a582-584b5bd8**** | The version ID of the key that is used to encrypt the plaintext. The primary version of the key is returned. |
KeyId | String | 599fa825-17de-417e-9554-bb032cc6**** | 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. |
ExportedDataKey | String | BQKP+1zK6+ZEMxTP5qaVzcsgXtWplYBKm0NXdSnB5FzliFxE1bSiu4dnEIlca2JpeH7yz1/S6fed630H+hIH6DoM25fTLNcKj+mFB0Xnh9m2+HN59Mn4qyTfcUeadnfCXSWcGBouhXFwcdd2rJ3n337bzTf4jm659gZu3L0i6PLuxM9p7mqdwO0cKJPfGVfhnfMz+f4alMg79WB/NNyE2lyX7/qxvV49ObNrrJbKSFiz8Djocaf0IESNLMbfYI5bXjWkJlX92DQbKhibtQW8ZOJ//ZC6t0AWcUoKL6QDm/dg5koQalcleRinpB+QadFm894sLbVZ9+N4GVs******* | The ciphertext of the data key that is encrypted by using the public key. |
Examples
Sample requests
http(s)://[Endpoint]/?Action=GenerateAndExportDataKey
&KeyId=1234abcd-12ab-34cd-56ef-12345678****
&KeySpec=AES_256
&NumberOfBytes=32
&PublicKeyBlob=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAndKfC2ReLL2+y8a0+ZBBeAft/uBYo86GZiYJuflqgUzKxpyuvlo3uQkBv6b+nx+0tz8g8v7GhpPWMSW5L9mNHYsvYFsa7jTxsYdt17yj6GlUHPuMIs8hr5qbwl38IHU1iIa7nYWwE2fb3ePOvLDACRJVgGpU0yxioW80d2QD+9aU4jF5dlAahcfgsNzo2CXzCUc1+xbmNuq7Rp+H9VJB9dyYOwqnW3RhOLBo21FzpORapf0UiRlrHRpk1V6ez+aE1dofaYh/9bh0m6ioxj7j5hpZbWccuEZTMBKd+cbuBkRhJzc6Tti6qwZbDiu4fUwbZS0Tqpuo1UadiyxMW********
&WrappingKeySpec=RSA_2048
&WrappingAlgorithm=RSAES_OAEP_SHA_256
&DryRun=false
&<Common request parameters>
Sample success responses
XML
format
HTTP/1.1 200 OK
Content-Type:application/xml
<GenerateAndExportDataKeyResponse>
<KeyVersionId>2ab1a983-7072-4bbc-a582-584b5bd8****</KeyVersionId>
<KeyId>599fa825-17de-417e-9554-bb032cc6****</KeyId>
<CiphertextBlob>ODZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmS7FmDBBQ0BkKsQrtRnidtPwirmDcS0ZuJCU41xxAAWk4Z8qsADfbV0b+i6kQmlvj79dJdGOvtX69Uycs901qOjop4bTS****</CiphertextBlob>
<RequestId>7021b6ec-4be7-4d3c-8a68-1e85d4d515a0</RequestId>
<ExportedDataKey>BQKP+1zK6+ZEMxTP5qaVzcsgXtWplYBKm0NXdSnB5FzliFxE1bSiu4dnEIlca2JpeH7yz1/S6fed630H+hIH6DoM25fTLNcKj+mFB0Xnh9m2+HN59Mn4qyTfcUeadnfCXSWcGBouhXFwcdd2rJ3n337bzTf4jm659gZu3L0i6PLuxM9p7mqdwO0cKJPfGVfhnfMz+f4alMg79WB/NNyE2lyX7/qxvV49ObNrrJbKSFiz8Djocaf0IESNLMbfYI5bXjWkJlX92DQbKhibtQW8ZOJ//ZC6t0AWcUoKL6QDm/dg5koQalcleRinpB+QadFm894sLbVZ9+N4GVs*******</ExportedDataKey>
</GenerateAndExportDataKeyResponse>
JSON
format
HTTP/1.1 200 OK
Content-Type:application/json
{
"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*******"
}
Error codes
HttpCode | Error code | Error message | Description |
400 | InvalidParameter | The specified parameter is not valid. | The specified parameter is invalid. |
404 | Forbidden.KeyNotFound | The specified Key is not found. | The specified key does not exist. |
For a list of error codes, see Service error codes.