1# Randomly Generating an Asymmetric Key Pair (C/C++) 2 3 4This topic uses RSA and SM2 as an example to describe how to generate an asymmetric key pair (**OH_CryptoKeyPair**) and obtain the binary data. 5 6 7The **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. 8 9## Adding the Dynamic Library in the CMake Script 10```txt 11 target_link_libraries(entry PUBLIC libohcrypto.so) 12``` 13 14## Randomly Generating an RSA Key Pair 15 16For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa). 17 181. 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. 19 202. 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**). 21 223. 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. 23 24 25```c++ 26#include "CryptoArchitectureKit/crypto_common.h" 27#include "CryptoArchitectureKit/crypto_asym_key.h" 28 29static OH_Crypto_ErrCode randomGenerateAsymKey() 30{ 31 OH_CryptoAsymKeyGenerator *ctx = nullptr; 32 OH_CryptoKeyPair *keyPair = nullptr; 33 OH_Crypto_ErrCode ret; 34 35 ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx); 36 if (ret != CRYPTO_SUCCESS) { 37 OH_CryptoAsymKeyGenerator_Destroy(ctx); 38 return ret; 39 } 40 41 42 ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair); 43 if (ret != CRYPTO_SUCCESS) { 44 OH_CryptoAsymKeyGenerator_Destroy(ctx); 45 OH_CryptoKeyPair_Destroy(keyPair); 46 return ret; 47 } 48 49 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 50 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; 51 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob); 52 if (ret != CRYPTO_SUCCESS) { 53 OH_CryptoAsymKeyGenerator_Destroy(ctx); 54 OH_CryptoKeyPair_Destroy(keyPair); 55 return ret; 56 } 57 58 OH_Crypto_FreeDataBlob(&retBlob); 59 60 OH_CryptoAsymKeyGenerator_Destroy(ctx); 61 OH_CryptoKeyPair_Destroy(keyPair); 62 return ret; 63} 64``` 65 66## Randomly Generating an SM2 Key Pair 67 68For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2). 69 701. 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. 71 722. 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**). 73 743. 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. 75 76 77```c++ 78#include "CryptoArchitectureKit/crypto_common.h" 79#include "CryptoArchitectureKit/crypto_asym_key.h" 80 81static OH_Crypto_ErrCode randomGenerateRSA() 82{ 83 OH_CryptoAsymKeyGenerator *ctx = nullptr; 84 OH_CryptoKeyPair *dupKeyPair = nullptr; 85 OH_Crypto_ErrCode ret; 86 87 ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx); 88 if (ret != CRYPTO_SUCCESS) { 89 OH_CryptoAsymKeyGenerator_Destroy(ctx); 90 return ret; 91 } 92 93 ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &dupKeyPair); 94 if (ret != CRYPTO_SUCCESS) { 95 OH_CryptoAsymKeyGenerator_Destroy(ctx); 96 OH_CryptoKeyPair_Destroy(dupKeyPair); 97 return ret; 98 } 99 100 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair); 101 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; 102 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob); 103 if (ret != CRYPTO_SUCCESS) { 104 OH_CryptoAsymKeyGenerator_Destroy(ctx); 105 OH_CryptoKeyPair_Destroy(dupKeyPair); 106 return ret; 107 } 108 109 OH_CryptoAsymKeyGenerator_Destroy(ctx); 110 OH_CryptoKeyPair_Destroy(dupKeyPair); 111 return ret; 112} 113``` 114