Encrypt

Prev Next

Available in Classic and VPC

Encrypt up to 32 KB of data with the current version of the key. It can only be requested as an AES256 or RSA2048 key type.

Request

This section describes the request format. The method and URI are as follows:

Method URI
POST /kms/v1/keys/{keyTag}/encrypt

Request headers

For information about the request headers common to all Key Management Service APIs, see the token authentication method in Key Management Service request headers.

Request path parameters

You can use the following path parameters with your request:

Field Type Required Description
keyTag String Required Key tag
  • Unique identifier for the key derived from the key name.
  • See Get key list.
  • Use to request encryption or decryption with REST APIs.
  • Key tags are not treated as confidential information.

Request body

You can include the following data in the body of your request:

Field Type Required Description
plaintext String or Array[String] Required Base64-encoded string data
  • Up to 32 KB of data can be encrypted.
context String Conditional Base64-encoded string data
  • Required when using keys with convergent encryption.
  • Can be up to 50 bytes long.
  • Always generate the same ciphertext, even when encrypting the same data multiple times.

Request example

The request example is as follows:

  • Request plaintext as String type
curl --location --request POST 'https://ocapi.ncloud.com/kms/v1/keys/a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6/encrypt' \
--header 'x-ncp-ocapi-token: {Access Token}' \
--data '{
  "plaintext": "{BASE64_PLAINTEXT}",
  "context": "{BASE64_CONTEXT}"
}'
  • Request plaintext as Array[String] type
curl --location --request POST 'https://ocapi.ncloud.com/kms/v1/keys/a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6/encrypt' \
--header 'x-ncp-ocapi-token: {Access Token}' \
--data '{
  "plaintext": ["{BASE64_PLAINTEXT_1}", "{BASE64_PLAINTEXT_2}", "{BASE64_PLAINTEXT_3}"],
  "context": "{BASE64_CONTEXT}"
}'

Response

This section describes the response format.

Response body

The response body includes the following data:

Field Type Required Description
code String - Success or Failure
data Object - Response result
data.ciphertext String or Array[String] - String data created by encrypting a plaintext

Response status codes

For information about the response status codes common to all Key Management Service APIs, see Key Management Service response status codes.

Response example

The response example is as follows:

  • Request plaintext as String type
{
    "code": "SUCCESS",
    "data": {
        "ciphertext": "{CIPHERTEXT}"
    }
}
  • Request plaintext as Array[String] type
{
    "code": "SUCCESS",
    "data": {
        "ciphertext": [
            "{CIPHERTEXT_1}",
            "{CIPHERTEXT_2}",
            "{CIPHERTEXT_3}"
        ]
    }
}

When encrypting directly with RSA2048 public key

The encryption algorithm parameters for the RSA2048 key are as follows:

Item Value
Algorithm RSAES-OAEP
OAEP hash SHA-256
MGF MGF1 with SHA-256
Label None
Maximum plaintext size 190 bytes

Here is an example parameter definition in Java.

OAEPParameterSpec spec = new OAEPParameterSpec(
        "SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
Note

NCP KMS does not support OAEP-SHA1 in accordance with national cryptographic guidelines and operates exclusively using the single specification shown above. Therefore, there is no need to specify a separate algorithm parameter when making encryption or decryption requests. Data encrypted directly with a public key without calling the KMS API can also be decrypted using Decrypt if it was encrypted under the same conditions as described above. For public key query and encryption examples, see Get public key.