1# 随机生成非对称密钥对(C/C++) 2 3以RSA和SM2为例,随机生成非对称密钥对(OH_CryptoKeyPair),并获得二进制数据。 4 5非对称密钥对可用于后续加解密等操作,二进制数据可用于存储或运输。 6 7## 在CMake脚本中链接相关动态库 8```txt 9target_link_libraries(entry PUBLIC libohcrypto.so) 10``` 11 12## 随机生成RSA密钥对 13 14对应的算法规格请查看[非对称密钥生成和转换规格:RSA](crypto-asym-key-generation-conversion-spec.md#rsa)。 15 161. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create),指定字符串参数'RSA1024|PRIMES_2',创建RSA密钥类型为RSA1024、素数个数为2的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。 17 182. 调用[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),随机生成非对称密钥对象(OH_CryptoKeyPair)。 19 203. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode)获取公钥密钥对象的二进制数据。 21 22```c++ 23#include "CryptoArchitectureKit/crypto_common.h" 24#include "CryptoArchitectureKit/crypto_asym_key.h" 25 26static OH_Crypto_ErrCode randomGenerateAsymKey() 27{ 28 OH_CryptoAsymKeyGenerator *ctx = nullptr; 29 OH_CryptoKeyPair *keyPair = nullptr; 30 OH_Crypto_ErrCode ret; 31 32 ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx); 33 if (ret != CRYPTO_SUCCESS) { 34 OH_CryptoAsymKeyGenerator_Destroy(ctx); 35 return ret; 36 } 37 38 39 ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair); 40 if (ret != CRYPTO_SUCCESS) { 41 OH_CryptoAsymKeyGenerator_Destroy(ctx); 42 OH_CryptoKeyPair_Destroy(keyPair); 43 return ret; 44 } 45 46 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); 47 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; 48 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob); 49 if (ret != CRYPTO_SUCCESS) { 50 OH_CryptoAsymKeyGenerator_Destroy(ctx); 51 OH_CryptoKeyPair_Destroy(keyPair); 52 return ret; 53 } 54 55 OH_Crypto_FreeDataBlob(&retBlob); 56 57 OH_CryptoAsymKeyGenerator_Destroy(ctx); 58 OH_CryptoKeyPair_Destroy(keyPair); 59 return ret; 60} 61``` 62 63## 随机生成SM2密钥对 64 65对应的算法规格请查看[非对称密钥生成和转换规格:SM2](crypto-asym-key-generation-conversion-spec.md#sm2)。 66 671. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create),指定字符串参数'SM2_256',创建密钥算法为SM2、密钥长度为256位的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。 68 692. 调用[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),随机生成非对称密钥对象(OH_CryptoKeyPair)。 70 713. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode)获取公钥密钥对象的二进制数据。 72 73 74```c++ 75#include "CryptoArchitectureKit/crypto_common.h" 76#include "CryptoArchitectureKit/crypto_asym_key.h" 77 78static OH_Crypto_ErrCode randomGenerateSm2KeyPair() 79{ 80 OH_CryptoAsymKeyGenerator *ctx = nullptr; 81 OH_CryptoKeyPair *dupKeyPair = nullptr; 82 OH_Crypto_ErrCode ret; 83 84 ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx); 85 if (ret != CRYPTO_SUCCESS) { 86 OH_CryptoAsymKeyGenerator_Destroy(ctx); 87 return ret; 88 } 89 90 ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &dupKeyPair); 91 if (ret != CRYPTO_SUCCESS) { 92 OH_CryptoAsymKeyGenerator_Destroy(ctx); 93 OH_CryptoKeyPair_Destroy(dupKeyPair); 94 return ret; 95 } 96 97 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair); 98 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; 99 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob); 100 if (ret != CRYPTO_SUCCESS) { 101 OH_CryptoAsymKeyGenerator_Destroy(ctx); 102 OH_CryptoKeyPair_Destroy(dupKeyPair); 103 return ret; 104 } 105 106 OH_CryptoAsymKeyGenerator_Destroy(ctx); 107 OH_CryptoKeyPair_Destroy(dupKeyPair); 108 return ret; 109} 110``` 111 112