harmony 鸿蒙随机生成非对称密钥对(C/C++)

  • 2025-06-16
  • 浏览 (4)

随机生成非对称密钥对(C/C++)

以RSA和SM2为例,随机生成非对称密钥对(OH_CryptoKeyPair),并获得二进制数据。

非对称密钥对可用于后续加解密等操作,二进制数据可用于存储或运输。

在CMake脚本中链接相关动态库

target_link_libraries(entry PUBLIC libohcrypto.so)

随机生成RSA密钥对

对应的算法规格请查看非对称密钥生成和转换规格:RSA

  1. 调用OH_CryptoAsymKeyGenerator_Create,指定字符串参数’RSA1024|PRIMES_2’,创建RSA密钥类型为RSA1024、素数个数为2的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。

  2. 调用OH_CryptoAsymKeyGenerator_Generate,随机生成非对称密钥对象(OH_CryptoKeyPair)。

  3. 调用OH_CryptoPubKey_Encode获取公钥密钥对象的二进制数据。

#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

  1. 调用OH_CryptoAsymKeyGenerator_Create,指定字符串参数’SM2_256’,创建密钥算法为SM2、密钥长度为256位的非对称密钥生成器(OH_CryptoAsymKeyGenerator)。

  2. 调用OH_CryptoAsymKeyGenerator_Generate,随机生成非对称密钥对象(OH_CryptoKeyPair)。

  3. 调用OH_CryptoPubKey_Encode获取公钥密钥对象的二进制数据。

#include "CryptoArchitectureKit/crypto_common.h"
#include "CryptoArchitectureKit/crypto_asym_key.h"

static OH_Crypto_ErrCode randomGenerateSm2KeyPair()
{
    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;
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Crypto Architecture Kit(加解密算法框架服务)

harmony 鸿蒙使用3DES对称密钥(ECB模式)加解密(C/C++)

harmony 鸿蒙使用3DES对称密钥(ECB模式)加解密(ArkTS)

harmony 鸿蒙使用AES对称密钥(CBC模式)加解密(C/C++)

harmony 鸿蒙使用AES对称密钥(CBC模式)加解密(ArkTS)

harmony 鸿蒙使用AES对称密钥(CCM模式)加解密(C/C++)

harmony 鸿蒙使用AES对称密钥(CCM模式)加解密(ArkTS)

harmony 鸿蒙使用AES对称密钥(ECB模式)加解密(C/C++)

harmony 鸿蒙使用AES对称密钥(ECB模式)加解密(ArkTS)

harmony 鸿蒙使用AES对称密钥(GCM模式)分段加解密(C/C++)

0  赞