• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Randomly Generating an Asymmetric Key Pair (C/C++)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10This topic uses RSA and SM2 as an example to describe how to generate an asymmetric key pair (**OH_CryptoKeyPair**) and obtain the binary data.
11
12The **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.
13
14## Adding the Dynamic Library in the CMake Script
15```txt
16target_link_libraries(entry PUBLIC libohcrypto.so)
17```
18
19## Randomly Generating an RSA Key Pair
20
21For details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa).
22
231. Call [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.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.
24
252. Call [OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_generate) to randomly generate an asymmetric key object (**OH_CryptoKeyPair**).
26
273. Call [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptopubkey_encode) to obtain the binary data of the public key object.
28
29```c++
30#include "CryptoArchitectureKit/crypto_common.h"
31#include "CryptoArchitectureKit/crypto_asym_key.h"
32
33static OH_Crypto_ErrCode randomGenerateAsymKey()
34{
35    OH_CryptoAsymKeyGenerator *ctx = nullptr;
36    OH_CryptoKeyPair *keyPair = nullptr;
37    OH_Crypto_ErrCode ret;
38
39    ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx);
40    if (ret != CRYPTO_SUCCESS) {
41        OH_CryptoAsymKeyGenerator_Destroy(ctx);
42        return ret;
43    }
44
45
46    ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair);
47    if (ret != CRYPTO_SUCCESS) {
48        OH_CryptoAsymKeyGenerator_Destroy(ctx);
49        OH_CryptoKeyPair_Destroy(keyPair);
50        return ret;
51    }
52
53    OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
54    Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
55    ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob);
56    if (ret != CRYPTO_SUCCESS) {
57        OH_CryptoAsymKeyGenerator_Destroy(ctx);
58        OH_CryptoKeyPair_Destroy(keyPair);
59        return ret;
60    }
61
62    OH_Crypto_FreeDataBlob(&retBlob);
63
64    OH_CryptoAsymKeyGenerator_Destroy(ctx);
65    OH_CryptoKeyPair_Destroy(keyPair);
66    return ret;
67}
68```
69
70## Randomly Generating an SM2 Key Pair
71
72For details about the algorithm specifications, see [SM2](crypto-asym-key-generation-conversion-spec.md#sm2).
73
741. Call [OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.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.
75
762. Call [OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptoasymkeygenerator_generate) to randomly generate an asymmetric key object (**OH_CryptoKeyPair**).
77
783. Call [OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/capi-crypto-asym-key-h.md#oh_cryptopubkey_encode) to obtain the binary data of the public key object.
79
80
81```c++
82#include "CryptoArchitectureKit/crypto_common.h"
83#include "CryptoArchitectureKit/crypto_asym_key.h"
84
85static OH_Crypto_ErrCode randomGenerateSm2KeyPair()
86{
87    OH_CryptoAsymKeyGenerator *ctx = nullptr;
88    OH_CryptoKeyPair *dupKeyPair = nullptr;
89    OH_Crypto_ErrCode ret;
90
91    ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx);
92    if (ret != CRYPTO_SUCCESS) {
93        OH_CryptoAsymKeyGenerator_Destroy(ctx);
94        return ret;
95    }
96
97    ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &dupKeyPair);
98    if (ret != CRYPTO_SUCCESS) {
99        OH_CryptoAsymKeyGenerator_Destroy(ctx);
100        OH_CryptoKeyPair_Destroy(dupKeyPair);
101        return ret;
102    }
103
104    OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
105    Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
106    ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob);
107    if (ret != CRYPTO_SUCCESS) {
108        OH_CryptoAsymKeyGenerator_Destroy(ctx);
109        OH_CryptoKeyPair_Destroy(dupKeyPair);
110        return ret;
111    }
112
113    OH_Crypto_FreeDataBlob(&retBlob);
114    OH_CryptoAsymKeyGenerator_Destroy(ctx);
115    OH_CryptoKeyPair_Destroy(dupKeyPair);
116    return ret;
117}
118```
119