1e41f4b71Sopenharmony_ci# Randomly Generating an Asymmetric Key Pair (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis topic uses RSA and SM2 as an example to describe how to generate an asymmetric key pair (**OH_CryptoKeyPair**) and obtain the binary data.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciThe **OH_CryptoKeyPair** object created can be used for subsequent encryption and decryption operations, and the binary data can be used for key storage and transfer.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script
10e41f4b71Sopenharmony_ci```txt
11e41f4b71Sopenharmony_ci   target_link_libraries(entry PUBLIC libohcrypto.so)
12e41f4b71Sopenharmony_ci```
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Randomly Generating an RSA Key Pair
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa).
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci1. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create) with the string parameter **'RSA1024|PRIMES_2'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 1024-bit RSA key with two primes.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci2. Use [OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate) to randomly generate an asymmetric key object (**OH_CryptoKeyPair**).
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci3. Use [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode) to obtain the binary data of the public key.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci```c++
26e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h"
27e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h"
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode randomGenerateAsymKey()
30e41f4b71Sopenharmony_ci{
31e41f4b71Sopenharmony_ci    OH_CryptoAsymKeyGenerator *ctx = nullptr;
32e41f4b71Sopenharmony_ci    OH_CryptoKeyPair *keyPair = nullptr;
33e41f4b71Sopenharmony_ci    OH_Crypto_ErrCode ret;
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci    ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx);
36e41f4b71Sopenharmony_ci    if (ret != CRYPTO_SUCCESS) {
37e41f4b71Sopenharmony_ci        OH_CryptoAsymKeyGenerator_Destroy(ctx);
38e41f4b71Sopenharmony_ci        return ret;
39e41f4b71Sopenharmony_ci    }
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci    ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair);
43e41f4b71Sopenharmony_ci    if (ret != CRYPTO_SUCCESS) {
44e41f4b71Sopenharmony_ci        OH_CryptoAsymKeyGenerator_Destroy(ctx);
45e41f4b71Sopenharmony_ci        OH_CryptoKeyPair_Destroy(keyPair);
46e41f4b71Sopenharmony_ci        return ret;
47e41f4b71Sopenharmony_ci    }
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci    OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
50e41f4b71Sopenharmony_ci    Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
51e41f4b71Sopenharmony_ci    ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob);
52e41f4b71Sopenharmony_ci    if (ret != CRYPTO_SUCCESS) {
53e41f4b71Sopenharmony_ci        OH_CryptoAsymKeyGenerator_Destroy(ctx);
54e41f4b71Sopenharmony_ci        OH_CryptoKeyPair_Destroy(keyPair);
55e41f4b71Sopenharmony_ci        return ret;
56e41f4b71Sopenharmony_ci    }
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci    OH_Crypto_FreeDataBlob(&retBlob);
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci    OH_CryptoAsymKeyGenerator_Destroy(ctx);
61e41f4b71Sopenharmony_ci    OH_CryptoKeyPair_Destroy(keyPair);
62e41f4b71Sopenharmony_ci    return ret;
63e41f4b71Sopenharmony_ci}
64e41f4b71Sopenharmony_ci```
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci## Randomly Generating an SM2 Key Pair
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2).
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci1. Use [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create) with the string parameter **'SM2_256'** to create an asymmetric key generator (**OH_CryptoAsymKeyGenerator**) object for a 256-bit SM2 key pair.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci2. Use [OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate) to randomly generate an asymmetric key object (**OH_CryptoKeyPair**).
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci3. Use [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode) to obtain the binary data of the public key.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci```c++
78e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h"
79e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h"
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_cistatic OH_Crypto_ErrCode randomGenerateRSA()
82e41f4b71Sopenharmony_ci{
83e41f4b71Sopenharmony_ci    OH_CryptoAsymKeyGenerator *ctx = nullptr;
84e41f4b71Sopenharmony_ci    OH_CryptoKeyPair *dupKeyPair = nullptr;
85e41f4b71Sopenharmony_ci    OH_Crypto_ErrCode ret;
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci    ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx);
88e41f4b71Sopenharmony_ci    if (ret != CRYPTO_SUCCESS) {
89e41f4b71Sopenharmony_ci        OH_CryptoAsymKeyGenerator_Destroy(ctx);
90e41f4b71Sopenharmony_ci        return ret;
91e41f4b71Sopenharmony_ci    }
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci    ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &dupKeyPair);
94e41f4b71Sopenharmony_ci    if (ret != CRYPTO_SUCCESS) {
95e41f4b71Sopenharmony_ci        OH_CryptoAsymKeyGenerator_Destroy(ctx);
96e41f4b71Sopenharmony_ci        OH_CryptoKeyPair_Destroy(dupKeyPair);
97e41f4b71Sopenharmony_ci        return ret;
98e41f4b71Sopenharmony_ci    }
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci    OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
101e41f4b71Sopenharmony_ci    Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
102e41f4b71Sopenharmony_ci    ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob);
103e41f4b71Sopenharmony_ci    if (ret != CRYPTO_SUCCESS) {
104e41f4b71Sopenharmony_ci        OH_CryptoAsymKeyGenerator_Destroy(ctx);
105e41f4b71Sopenharmony_ci        OH_CryptoKeyPair_Destroy(dupKeyPair);
106e41f4b71Sopenharmony_ci        return ret;
107e41f4b71Sopenharmony_ci    }
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci    OH_CryptoAsymKeyGenerator_Destroy(ctx);
110e41f4b71Sopenharmony_ci    OH_CryptoKeyPair_Destroy(dupKeyPair);
111e41f4b71Sopenharmony_ci    return ret;
112e41f4b71Sopenharmony_ci}
113e41f4b71Sopenharmony_ci```
114