1e41f4b71Sopenharmony_ci# Exporting a Key (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis 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.
5e41f4b71Sopenharmony_ci>**NOTE**<br>
6e41f4b71Sopenharmony_ci> The mini-system devices support export of only the RSA public keys.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci## Add the dynamic library in the CMake script.
9e41f4b71Sopenharmony_ci```txt
10e41f4b71Sopenharmony_ci   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
11e41f4b71Sopenharmony_ci```
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## How to Develop
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci1. Set parameters.
16e41f4b71Sopenharmony_ci   - **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.
17e41f4b71Sopenharmony_ci   - **paramSetIn**: This parameter is reserved. Leave it empty.
18e41f4b71Sopenharmony_ci   - **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.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci2. Use [OH_Huks_GetKeyItemParamSet](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_getkeyitemparamset) to obtain key properties.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci3. 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.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci```c++
25e41f4b71Sopenharmony_ci#include "huks/native_huks_api.h"
26e41f4b71Sopenharmony_ci#include "huks/native_huks_param.h"
27e41f4b71Sopenharmony_ci#include <string.h>
28e41f4b71Sopenharmony_cistatic napi_value ExportKey(napi_env env, napi_callback_info info)
29e41f4b71Sopenharmony_ci{
30e41f4b71Sopenharmony_ci    /* 1. Set the key alias. */
31e41f4b71Sopenharmony_ci    const char *alias = "test_key";
32e41f4b71Sopenharmony_ci    struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias };
33e41f4b71Sopenharmony_ci    /* Request the memory for holding the public key to be exported. */
34e41f4b71Sopenharmony_ci    uint8_t *pubKey = (uint8_t *)malloc(512); // Request memory based on the key size.
35e41f4b71Sopenharmony_ci    if (pubKey == nullptr) {
36e41f4b71Sopenharmony_ci        return nullptr;
37e41f4b71Sopenharmony_ci    }
38e41f4b71Sopenharmony_ci    struct OH_Huks_Blob keyBlob = { 256, pubKey };
39e41f4b71Sopenharmony_ci    struct OH_Huks_Result ohResult;
40e41f4b71Sopenharmony_ci    do {
41e41f4b71Sopenharmony_ci        ohResult = OH_Huks_ExportPublicKeyItem(&aliasBlob, nullptr, &keyBlob);
42e41f4b71Sopenharmony_ci        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
43e41f4b71Sopenharmony_ci            break;
44e41f4b71Sopenharmony_ci        }
45e41f4b71Sopenharmony_ci    } while (0);
46e41f4b71Sopenharmony_ci    free(pubKey);
47e41f4b71Sopenharmony_ci    napi_value ret;
48e41f4b71Sopenharmony_ci    napi_create_int32(env, ohResult.errorCode, &ret);
49e41f4b71Sopenharmony_ci    return ret;
50e41f4b71Sopenharmony_ci}
51e41f4b71Sopenharmony_ci```
52