openharmony 鸿蒙 crypto-rsa-asym-encrypt-decrypt-pkcs1-ndk

2026-08-25 浏览 (1)

Encryption and Decryption with an RSA Asymmetric Key Pair (PKCS1) (C/C++)

For details about the algorithm specifications, see RSA.

Encryption

  1. Call OH_CryptoAsymKeyGenerator_Create and OH_CryptoAsymKeyGenerator_Generate to generate an asymmetric key pair (keyPair) of the RSA1024 type with two prime numbers. The keyPair object includes a public key (PubKey) and a private key (PriKey).

    For details about how to generate an RSA asymmetric key pair, see the following example. To learn more, see RSA and Randomly Generating an Asymmetric Key Pair. There may be differences between the input parameters in the reference documents and those in the following example.

  2. Call OH_CryptoAsymCipher_Create with the string parameter 'RSA1024|PKCS1' to create a Cipher instance for encryption and decryption. The key type is RSA1024, and the padding mode is PKCS1.

  3. Call OH_CryptoAsymCipher_Init to initialize the Cipher instance. Specifically, set mode to CRYPTO_ENCRYPT_MODE, and specify the key for encryption (keyPair).

  4. Call OH_CryptoAsymCipher_Final and pass the plaintext to obtain the encrypted data.

    • The output of OH_CryptoAsymCipher_Final may be NULL. To avoid exceptions, always check whether the result is NULL before accessing specific data.
    • If the data to be encrypted is considerably long, you can call OH_CryptoAsymCipher_Final multiple times to pass in and encrypt the data by segment.

Decryption

  1. If RSA is used, the Cipher instance cannot be initialized repeatedly. Call OH_CryptoAsymCipher_Create to create a new Cipher instance.

  2. Call OH_CryptoAsymCipher_Init to initialize the Cipher instance. Specifically, set mode to CRYPTO_DECRYPT_MODE, and specify the key for decryption (keyPair).

  3. Call OH_CryptoAsymCipher_Final and pass the ciphertext to obtain the decrypted data.


#include "CryptoArchitectureKit/crypto_architecture_kit.h"
#include <cstring>

static OH_Crypto_ErrCode doRsaEncrypt(const Crypto_DataBlob *plainData, OH_CryptoKeyPair **keyPair,
    OH_CryptoAsymKeyGenerator **keyGen, Crypto_DataBlob *encryptedData)
{
    OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA1024", keyGen);
    if (ret != CRYPTO_SUCCESS) {
        return ret;
    }

    ret = OH_CryptoAsymKeyGenerator_Generate(*keyGen, keyPair);
    if (ret != CRYPTO_SUCCESS) {
        OH_CryptoAsymKeyGenerator_Destroy(*keyGen);
        return ret;
    }

    OH_CryptoAsymCipher *cipher = nullptr;
    ret = OH_CryptoAsymCipher_Create("RSA1024|PKCS1", &cipher);
    if (ret != CRYPTO_SUCCESS) {
        OH_CryptoKeyPair_Destroy(*keyPair);
        OH_CryptoAsymKeyGenerator_Destroy(*keyGen);
        return ret;
    }

    ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, *keyPair);
    if (ret != CRYPTO_SUCCESS) {
        OH_CryptoAsymCipher_Destroy(cipher);
        OH_CryptoKeyPair_Destroy(*keyPair);
        OH_CryptoAsymKeyGenerator_Destroy(*keyGen);
        return ret;
    }

    ret = OH_CryptoAsymCipher_Final(cipher, plainData, encryptedData);
    OH_CryptoAsymCipher_Destroy(cipher);
    if (ret != CRYPTO_SUCCESS) {
        OH_CryptoKeyPair_Destroy(*keyPair);
        OH_CryptoAsymKeyGenerator_Destroy(*keyGen);
        return ret;
    }

    return ret;
}

static OH_Crypto_ErrCode doRsaDecrypt(const Crypto_DataBlob *encryptedData, OH_CryptoKeyPair *keyPair,
    const Crypto_DataBlob *expectedPlainData)
{
    OH_CryptoAsymCipher *cipher = nullptr;
    OH_Crypto_ErrCode ret = OH_CryptoAsymCipher_Create("RSA1024|PKCS1", &cipher);
    if (ret != CRYPTO_SUCCESS) {
        return ret;
    }

    ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_DECRYPT_MODE, keyPair);
    if (ret != CRYPTO_SUCCESS) {
        OH_CryptoAsymCipher_Destroy(cipher);
        return ret;
    }

    Crypto_DataBlob decrypted = { 0 };
    ret = OH_CryptoAsymCipher_Final(cipher, encryptedData, &decrypted);
    OH_CryptoAsymCipher_Destroy(cipher);
    if (ret != CRYPTO_SUCCESS) {
        return ret;
    }

    if ((decrypted.len != expectedPlainData->len)||
        (memcmp(decrypted.data, expectedPlainData->data, decrypted.len) != 0)) {
        OH_Crypto_FreeDataBlob(&decrypted);
        return CRYPTO_OPERTION_ERROR;
    }

    OH_Crypto_FreeDataBlob(&decrypted);
    return ret;
}

OH_Crypto_ErrCode doTestRsaEncDec()
{
    const char *testData = "Hello, RSA!";
    Crypto_DataBlob plainData = {
        .data = (uint8_t *)testData,
        .len = strlen(testData)
    };

    OH_CryptoKeyPair *keyPair = nullptr;
    OH_CryptoAsymKeyGenerator *keyGen = nullptr;
    Crypto_DataBlob encryptedData = { 0 };

    OH_Crypto_ErrCode ret = doRsaEncrypt(&plainData, &keyPair, &keyGen, &encryptedData);
    if (ret != CRYPTO_SUCCESS) {
        return ret;
    }

    ret = doRsaDecrypt(&encryptedData, keyPair, &plainData);
    OH_Crypto_FreeDataBlob(&encryptedData);
    OH_CryptoKeyPair_Destroy(keyPair);
    OH_CryptoAsymKeyGenerator_Destroy(keyGen);
    return ret;
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 crypto-key-agreement-using-ecdh

openharmony 鸿蒙 crypto-aes-sym-encrypt-decrypt-gcm-ndk

openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-gcm-ndk

openharmony 鸿蒙 crypto-sm2-sign-sig-verify-pkcs1-ndk

openharmony 鸿蒙 crypto-key-derivation-using-scrypt-ndk

openharmony 鸿蒙 crypto-sym-encrypt-decrypt-spec

openharmony 鸿蒙 crypto-rsa-sign-sig-verify-pkcs1-by-segment

openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-cbc

openharmony 鸿蒙 crypto-prikey-to-get-pubkey-ndk

openharmony 鸿蒙 crypto-convert-string-data-to-asym-key-pair

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/ge9YK6Ct