All Products
Search
Document Center

Key Management Service:Encrypt

Last Updated:Mar 08, 2024

Encrypts plaintext by using a symmetric key.

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:

QPS limits

You can call this operation up to 750 times per second per account. If the number of the 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

  • KMS uses the primary version of a key to encrypt data.
  • Only data of 6 KB or less can be encrypted. For example, you can call this operation to encrypt Rivest-Shamir-Adleman (RSA) keys, database access passwords, or other sensitive information.
  • If you want to migrate encrypted data between regions, you can call the Encrypt operation to encrypt the data key plaintext that is transferred from the source region in the destination region. In this case, the data key ciphertext is generated in the destination region. You can also call the Decrypt operation to decrypt the data key in the destination region.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

Request parameters

Parameter

Type

Required

Example

Description

Action String Yes Encrypt

The operation that you want to perform. Set the value to Encrypt.

KeyId String Yes 1234abcd-12ab-34cd-56ef-12345678****

The ID of the key. The ID must be globally unique. You can also set this parameter to an alias that is bound to the key. For more information, see Alias overview.

Plaintext String Yes SGVsbG8gd29y****

The plaintext. The plaintext must be Base64 encoded.

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.

Response parameters

Parameter

Type

Example

Description

KeyVersionId String 86a9efd9-3d16-4894-bd4f-1fc43f3f****

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

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

The ID of the key. The ID must be globally unique. If KeyId is set to an alias of the key, the ID of the key to which the alias is bound is returned.

CiphertextBlob String DZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmaaSl+TztSIMe43nbTH/Z1Wr4XfLftKhAciUmDQXuMRl4WTvKhxjMThjK****

The ciphertext that is encrypted by using the primary key version.

RequestId String 475f1620-b9d3-4d35-b5c6-3fbdd941423d

The ID of the request.

Examples

Sample requests

http(s)://[Endpoint]/?Action=Encrypt
&KeyId=1234abcd-12ab-34cd-56ef-12345678****
&Plaintext=SGVsbG8gd29y****
&Common request parameters

Sample success responses

XML format

HTTP/1.1 200 OK
Content-Type:application/xml

<EncryptResponse>
    <KeyVersionId>86a9efd9-3d16-4894-bd4f-1fc43f3f****</KeyVersionId>
    <KeyId>1234abcd-12ab-34cd-56ef-12345678****</KeyId>
    <CiphertextBlob>DZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmaaSl+TztSIMe43nbTH/Z1Wr4XfLftKhAciUmDQXuMRl4WTvKhxjMThjK****</CiphertextBlob>
    <RequestId>475f1620-b9d3-4d35-b5c6-3fbdd941423d</RequestId>
</EncryptResponse>

JSON format

HTTP/1.1 200 OK
Content-Type:application/json

{
  "KeyVersionId" : "86a9efd9-3d16-4894-bd4f-1fc43f3f****",
  "KeyId" : "1234abcd-12ab-34cd-56ef-12345678****",
  "CiphertextBlob" : "DZhOWVmZDktM2QxNi00ODk0LWJkNGYtMWZjNDNmM2YyYWJmaaSl+TztSIMe43nbTH/Z1Wr4XfLftKhAciUmDQXuMRl4WTvKhxjMThjK****",
  "RequestId" : "475f1620-b9d3-4d35-b5c6-3fbdd941423d"
}

Error codes

HTTP status code

Error code

Error message

Description

400 InvalidParameter The specified parameter is invalid. The specified parameter is invalid.
404 Forbidden.KeyNotFound The specified Key is not found. The specified key does not exist.
404 Forbidden.AliasNotFound The specified Alias is not found. The specified alias is not found.

For a list of error codes, see Service error codes.