# 随机生成非对称密钥对(C/C++) 以RSA和SM2为例,随机生成非对称密钥对(OH_CryptoKeyPair),并获得二进制数据。 非对称密钥对可用于后续加解密等操作,二进制数据可用于存储或运输。 ## 在CMake脚本中链接相关动态库 ```txt target_link_libraries(entry PUBLIC libohcrypto.so) ``` ## 随机生成RSA密钥对 对应的算法规格请查看[非对称密钥生成和转换规格:RSA](crypto-asym-key-generation-conversion-spec.md#rsa)。 1. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create),指定字符串参数'RSA1024|PRIMES_2',创建RSA密钥类型为RSA1024、素数个数为2的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。 2. 调用[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),随机生成非对称密钥对象(OH_CryptoKeyPair)。 3. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode)获取公钥密钥对象的二进制数据。 ```c++ #include "CryptoArchitectureKit/crypto_common.h" #include "CryptoArchitectureKit/crypto_asym_key.h" static OH_Crypto_ErrCode randomGenerateAsymKey() { OH_CryptoAsymKeyGenerator *ctx = nullptr; OH_CryptoKeyPair *keyPair = nullptr; OH_Crypto_ErrCode ret; ret = OH_CryptoAsymKeyGenerator_Create("RSA1024|PRIMES_2", &ctx); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(ctx); return ret; } ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(ctx); OH_CryptoKeyPair_Destroy(keyPair); return ret; } OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(ctx); OH_CryptoKeyPair_Destroy(keyPair); return ret; } OH_Crypto_FreeDataBlob(&retBlob); OH_CryptoAsymKeyGenerator_Destroy(ctx); OH_CryptoKeyPair_Destroy(keyPair); return ret; } ``` ## 随机生成SM2密钥对 对应的算法规格请查看[非对称密钥生成和转换规格:SM2](crypto-asym-key-generation-conversion-spec.md#sm2)。 1. 调用[OH_CryptoAsymKeyGenerator_Create](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_create),指定字符串参数'SM2_256',创建密钥算法为SM2、密钥长度为256位的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。 2. 调用[OH_CryptoAsymKeyGenerator_Generate](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptoasymkeygenerator_generate),随机生成非对称密钥对象(OH_CryptoKeyPair)。 3. 调用[OH_CryptoPubKey_Encode](../../reference/apis-crypto-architecture-kit/_crypto_asym_key_api.md#oh_cryptopubkey_encode)获取公钥密钥对象的二进制数据。 ```c++ #include "CryptoArchitectureKit/crypto_common.h" #include "CryptoArchitectureKit/crypto_asym_key.h" static OH_Crypto_ErrCode randomGenerateRSA() { OH_CryptoAsymKeyGenerator *ctx = nullptr; OH_CryptoKeyPair *dupKeyPair = nullptr; OH_Crypto_ErrCode ret; ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &ctx); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(ctx); return ret; } ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &dupKeyPair); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(ctx); OH_CryptoKeyPair_Destroy(dupKeyPair); return ret; } OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair); Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob); if (ret != CRYPTO_SUCCESS) { OH_CryptoAsymKeyGenerator_Destroy(ctx); OH_CryptoKeyPair_Destroy(dupKeyPair); return ret; } OH_CryptoAsymKeyGenerator_Destroy(ctx); OH_CryptoKeyPair_Destroy(dupKeyPair); return ret; } ```