1# Exporting a Key (C/C++)
2
3
4This topic walks you through on how to export the public key of a persistently stored asymmetric key. Currently, HUKS supports export of the ECC, RSA, Ed25519, X25519, and SM2 public keys.
5>**NOTE**<br>
6> The mini-system devices support export of only the RSA public keys.
7
8## Add the dynamic library in the CMake script.
9```txt
10   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
11```
12
13## How to Develop
14
151. Set parameters.
16   - **keyAlias**: key alias encapsulated in an [OH_Huks_Blob](../../reference/apis-universal-keystore-kit/_o_h___huks___blob.md) struct. The maximum length of the key alias is 128 bytes.
17   - **paramSetIn**: This parameter is reserved. Leave it empty.
18   - **key**: [OH_Huks_Blob](../../reference/apis-universal-keystore-kit/_o_h___huks___blob.md) object used to hold the key exported. Ensure that there is enough memory for storing the key exported.
19
202. Use [OH_Huks_GetKeyItemParamSet](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_getkeyitemparamset) to obtain key properties.
21
223. Check the return value. If the operation is successful, the exported key is in the **key** field in the DER format defined in X.509. For details about the format, see [Public Key Material Format](huks-concepts.md#public-key-material-format). If the operation fails, an error code is returned.
23
24```c++
25#include "huks/native_huks_api.h"
26#include "huks/native_huks_param.h"
27#include <string.h>
28static napi_value ExportKey(napi_env env, napi_callback_info info)
29{
30    /* 1. Set the key alias. */
31    const char *alias = "test_key";
32    struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias };
33    /* Request the memory for holding the public key to be exported. */
34    uint8_t *pubKey = (uint8_t *)malloc(512); // Request memory based on the key size.
35    if (pubKey == nullptr) {
36        return nullptr;
37    }
38    struct OH_Huks_Blob keyBlob = { 256, pubKey };
39    struct OH_Huks_Result ohResult;
40    do {
41        ohResult = OH_Huks_ExportPublicKeyItem(&aliasBlob, nullptr, &keyBlob);
42        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
43            break;
44        }
45    } while (0);
46    free(pubKey);
47    napi_value ret;
48    napi_create_int32(env, ohResult.errorCode, &ret);
49    return ret;
50}
51```
52