Get Public Key

Prev Next

Available in Classic and VPC

Get a publick key. It can only be requested as an RSA or ECDSA key type.

Request

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

Method URI
POST /kms/v1/keys/{keyTag}/get-pub-key

Request headers

For information about the 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
keyVersion Integer Optional Version of the key to query
  • The most recent version of the key is displayed when not entered
  • Disabled key versions can't be retrieved

Request example

The request example is as follows:

curl --location --request POST 'https://ocapi.ncloud.com/kms/v1/keys/a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6/get-pub-key' \
--header 'x-ncp-ocapi-token: {Access Token}' \
--data '{
  "keyVersion": 2
}'

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.publicKey String - Public key

Response status codes

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

Response example

The response example is as follows:

{
    "code": "SUCCESS",
    "data": {
        "publicKey": "{PUBLIC_KEY_PEM}"
    }
}

Verifying a signature with the public key

This section describes how to directly verify a signature value created by Sign using the retrieved public key.

data.publicKey is a PEM string (-----BEGIN PUBLIC KEY-----) in X.509 SubjectPublicKeyInfo format.

You must specify the following three items according to the requested key type:

Item RSA2048 ECDSA
Signature algorithm RSASSA-PSS SHA256withECDSA
Signature parameters SHA-256, MGF1(SHA-256), salt length 222 bytes, trailer field 1 No separate parameters
Signature encoding Fixed 256 bytes ASN.1 DER

The salt length of 222 bytes is the maximum RSA-PSS salt length, calculated as ceil((2048 - 1) / 8) - 32 - 2 = 222. OpenSSL, Go, and Node.js can automatically infer the salt length from the signature value during verification, but Java (JCA) doesn't support this inference, so it must be specified explicitly.

Verification example

The following is an example of verifying a signature value in Java:

// 1. Parse the public key (PEM)
String base64Key = publicKey
        .replace("-----BEGIN PUBLIC KEY-----", "")
        .replace("-----END PUBLIC KEY-----", "")
        .replaceAll("\\s", "");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64Key));

// 2. Remove the "ncpkms:v{key version}:" prefix from the signature value, then Base64-decode it
byte[] signatureBytes = Base64.getDecoder()
        .decode(signature.substring(signature.lastIndexOf(':') + 1));

// 3. The signed data — the raw bytes obtained by Base64-decoding the data from the Sign request
byte[] data = Base64.getDecoder().decode(base64Data);
  • RSA2048 key
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(keySpec);

Signature verifier = Signature.getInstance("RSASSA-PSS");
verifier.setParameter(new PSSParameterSpec(
        "SHA-256",
        "MGF1",
        MGF1ParameterSpec.SHA256,
        222,    // Maximum RSA-PSS salt length (RSA2048 + SHA-256)
        1
));
verifier.initVerify(key);
verifier.update(data);

boolean valid = verifier.verify(signatureBytes);
  • ECDSA key
PublicKey key = KeyFactory.getInstance("EC").generatePublic(keySpec);

Signature verifier = Signature.getInstance("SHA256withECDSA");
verifier.initVerify(key);
verifier.update(data);

boolean valid = verifier.verify(signatureBytes);
Note

To avoid setting the signature parameters yourself, use Verify instead.

Encrypting with the public key

This section describes how to directly encrypt data with the retrieved public key. This is only possible for the NCP KMS RSA2048 key type. The ECDSA key type is provided for sign/verify (SIGN_VERIFY) only and does not support encryption.

You must specify the following conditions when encrypting:

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

Encryption example

The following is an example of encrypting data with the public key in Java:

// 1. Parse the public key (PEM)
String base64Key = publicKey
        .replace("-----BEGIN PUBLIC KEY-----", "")
        .replace("-----END PUBLIC KEY-----", "")
        .replaceAll("\\s", "");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64Key));
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(keySpec);

// 2. Encrypt — plaintext can be up to 190 bytes
OAEPParameterSpec spec = new OAEPParameterSpec(
        "SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] ciphertextBytes = cipher.doFinal(plaintext);

// 3. To decrypt with the Decrypt API, prepend the "ncpkms:v{key version}:" prefix
String ciphertext = "ncpkms:v" + keyVersion + ":" + Base64.getEncoder().encodeToString(ciphertextBytes);
Note

If you encrypt through the KMS API, you can skip this process and use Encrypt directly. Data encrypted directly with the public key can also be decrypted with Decrypt, as long as it was encrypted under the same conditions as above.