Encrypt
Encrypts plaintext into ciphertext by using a customer master key (CMK). The
Encrypt operation has two primary use cases:
-
You can encrypt up to 4 kilobytes (4096 bytes) of arbitrary data such as an RSA key, a database password, or other sensitive information.
-
To move encrypted data from one AWS region to another, you can use this operation to encrypt in the new region the plaintext data key that was used to encrypt the data in the original region. This provides you with an encrypted copy of the data key that can be decrypted in the new region and used there to decrypt the encrypted data.
To perform this operation on a CMK in a different AWS account, specify the key ARN or alias ARN in the value of the KeyId parameter.
Unless you are moving encrypted data from one region to another, you don't use this
operation to encrypt a generated data key within a region. To get data keys that are
already
encrypted, call the GenerateDataKey or GenerateDataKeyWithoutPlaintext operation. Data keys don't need to be encrypted
again by calling Encrypt.
To encrypt data locally in your application, use the GenerateDataKey operation to return a plaintext data encryption key and a copy of the key encrypted under the CMK of your choosing.
Request Syntax
{
"EncryptionContext": {
"string" : "string"
},
"GrantTokens": [ "string" ],
"KeyId": "string",
"Plaintext": blob
}
Request Parameters
For information about the parameters that are common to all actions, see Common Parameters.
The request accepts the following data in JSON format.
Note
In the following list, the required parameters are described first.
- KeyId
-
A unique identifier for the customer master key (CMK).
To specify a CMK, use its key ID, Amazon Resource Name (ARN), alias name, or alias ARN. When using an alias name, prefix it with "alias/". To specify a CMK in a different AWS account, you must use the key ARN or alias ARN.
For example:
-
Key ID:
1234abcd-12ab-34cd-56ef-1234567890ab -
Key ARN:
arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab -
Alias name:
alias/ExampleAlias -
Alias ARN:
arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias
To get the key ID and key ARN for a CMK, use ListKeys or DescribeKey. To get the alias name and alias ARN, use ListAliases.
Type: String
Length Constraints: Minimum length of 1. Maximum length of 2048.
Required: Yes
-
- Plaintext
-
Data to be encrypted.
Type: Base64-encoded binary data object
Length Constraints: Minimum length of 1. Maximum length of 4096.
Required: Yes
- EncryptionContext
-
Name-value pair that specifies the encryption context to be used for authenticated encryption. If used here, the same value must be supplied to the
DecryptAPI or decryption will fail. For more information, see Encryption Context.Type: String to string map
Required: No
- GrantTokens
-
A list of grant tokens.
For more information, see Grant Tokens in the AWS Key Management Service Developer Guide.
Type: Array of strings
Array Members: Minimum number of 0 items. Maximum number of 10 items.
Length Constraints: Minimum length of 1. Maximum length of 8192.
Required: No
Response Syntax
{
"CiphertextBlob": blob,
"KeyId": "string"
}
Response Elements
If the action is successful, the service sends back an HTTP 200 response.
The following data is returned in JSON format by the service.
- CiphertextBlob
-
The encrypted plaintext. When you use the HTTP API or the AWS CLI, the value is Base64-encoded. Otherwise, it is not encoded.
Type: Base64-encoded binary data object
Length Constraints: Minimum length of 1. Maximum length of 6144.
- KeyId
-
The ID of the key used during encryption.
Type: String
Length Constraints: Minimum length of 1. Maximum length of 2048.
Errors
For information about the errors that are common to all actions, see Common Errors.
- DependencyTimeoutException
-
The system timed out while trying to fulfill the request. The request can be retried.
HTTP Status Code: 500
- DisabledException
-
The request was rejected because the specified CMK is not enabled.
HTTP Status Code: 400
- InvalidGrantTokenException
-
The request was rejected because the specified grant token is not valid.
HTTP Status Code: 400
- InvalidKeyUsageException
-
The request was rejected because the specified
KeySpecvalue is not valid.HTTP Status Code: 400
- KeyUnavailableException
-
The request was rejected because the specified CMK was not available. The request can be retried.
HTTP Status Code: 500
- KMSInternalException
-
The request was rejected because an internal exception occurred. The request can be retried.
HTTP Status Code: 400
- KMSInvalidStateException
-
The request was rejected because the state of the specified resource is not valid for this request.
For more information about how key state affects the use of a CMK, see How Key State Affects Use of a Customer Master Key in the AWS Key Management Service Developer Guide.
HTTP Status Code: 400
- NotFoundException
-
The request was rejected because the specified entity or resource could not be found.
HTTP Status Code: 400
Examples
The following examples are formatted for legibility.
Example Request
CopyPOST / HTTP/1.1 Host: kms.us-west-2.amazonaws.com Content-Length: 107 X-Amz-Target: TrentService.Encrypt X-Amz-Date: 20160517T203825Z Content-Type: application/x-amz-json-1.1 Authorization: AWS4-HMAC-SHA256\ Credential=AKIAI44QH8DHBEXAMPLE/20160517/us-west-2/kms/aws4_request,\ SignedHeaders=content-type;host;x-amz-date;x-amz-target,\ Signature=67ccaa73c1af7fe83973ce8139104d55f3bdcebee323d2f2e65996d99015ace2 { "Plaintext": "VGhpcyBpcyBEYXkgMSBmb3IgdGhlIEludGVybmV0Cg==", "KeyId": "1234abcd-12ab-34cd-56ef-1234567890ab" }
Example Response
CopyHTTP/1.1 200 OK Server: Server Date: Tue, 17 May 2016 20:38:30 GMT Content-Type: application/x-amz-json-1.1 Content-Length: 379 Connection: keep-alive x-amzn-RequestId: 50a0c603-1c6f-11e6-bb9e-3fadde80ce75 { "CiphertextBlob": "CiDPoCH188S65r5Cy7pAhIFJMXDlU7mewhSlYUpuQIVBrhKmAQEBAgB4z6Ah9fPEuua+Qsu6QISBSTFw5VO5nsIUpWFKbkCFQa4AAAB9MHsGCSqGSIb3DQEHBqBuMGwCAQAwZwYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAxLc9b6QThC9jB/ZjYCARCAOt8la8qXLO5wB3JH2NlwWWzWRU2RKqpO9A/0psE5UWwkK6CnwoeC3Zj9Q0A66apZkbRglFfY1lTY+Tc=", "KeyId": "arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" }
See Also
For more information about using this API in one of the language-specific AWS SDKs, see the following:


