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