1# 随机生成非对称密钥对(C/C++)
2
3
4以RSA和SM2为例,随机生成非对称密钥对(OH_CryptoKeyPair),并获得二进制数据。
5
6
7非对称密钥对可用于后续加解密等操作,二进制数据可用于存储或运输。
8
9## 在CMake脚本中链接相关动态库
10```txt
11   target_link_libraries(entry PUBLIC libohcrypto.so)
12```
13
14## 随机生成RSA密钥对
15
16对应的算法规格请查看[非对称密钥生成和转换规格:RSA](crypto-asym-key-generation-conversion-spec.md#rsa)。
17
181. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create),指定字符串参数'RSA1024|PRIMES_2',创建RSA密钥类型为RSA1024、素数个数为2的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。
19
202. 调用[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),随机生成非对称密钥对象(OH_CryptoKeyPair)。
21
223. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode)获取公钥密钥对象的二进制数据。
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## 随机生成SM2密钥对
67
68对应的算法规格请查看[非对称密钥生成和转换规格:SM2](crypto-asym-key-generation-conversion-spec.md#sm2)。
69
701. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create),指定字符串参数'SM2_256',创建密钥算法为SM2、密钥长度为256位的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。
71
722. 调用[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),随机生成非对称密钥对象(OH_CryptoKeyPair)。
73
743. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode)获取公钥密钥对象的二进制数据。
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
115