HarmonyOS Native RSA 加解密实现咨询

目前工程正在做数据加密模块,其中需要用到RSA/ECB/OAEPWithSHA-256AndMGF1Padding 加密功能。看官网只有AES加解密的实力代码。

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/huks-encryption-decryption-ndk-V5

按照提示改写了一下,加密后的数据无法解密,是否能提供一份RSA/ECB/OAEPWithSHA-256AndMGF1Padding加解密的示例代码。

HarmonyOS
2024-12-25 09:18:21
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit
#include "napi/native_api.h"
#include "huks/native_huks_api.h"
#include "huks/native_huks_param.h"
#include <hilog/log.h>
#include <string.h>

#include "huks/native_huks_api.h"
#include "huks/native_huks_param.h"
#include <string.h>
  OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params,
uint32_t paramCount) {
  OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
  if (ret.errorCode != OH_HUKS_SUCCESS) {
    return ret;
  }

  ret = OH_Huks_AddParams(*paramSet, params, paramCount);
  if (ret.errorCode != OH_HUKS_SUCCESS) {
    OH_Huks_FreeParamSet(paramSet);
    return ret;
  }

  ret = OH_Huks_BuildParamSet(paramSet);
  if (ret.errorCode != OH_HUKS_SUCCESS) {
    OH_Huks_FreeParamSet(paramSet);
    return ret;
  }

  return ret;
}

static struct OH_Huks_Param g_genEncDecParams[] = {
  {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_RSA},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT | OH_HUKS_KEY_PURPOSE_ENCRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_RSA_KEY_SIZE_2048},
};

static struct OH_Huks_Param g_encryptParams[] = {
  {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_RSA},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_RSA_KEY_SIZE_2048},
{.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_OAEP},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
};

static struct OH_Huks_Param g_decryptParams[] = {
  {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_RSA},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_RSA_KEY_SIZE_2048},
{.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_OAEP},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
};

static const uint32_t RSA_COMMON_SIZE = 2048;

OH_Huks_Result HksRSACipherTestEncrypt(const struct OH_Huks_Blob *keyAlias,
const struct OH_Huks_ParamSet *encryptParamSet,
const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText) {
uint8_t handleE[sizeof(uint64_t)] = {0};
struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE};
OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
if (ret.errorCode != OH_HUKS_SUCCESS) {
  return ret;
}

ret = OH_Huks_FinishSession(&handleEncrypt, encryptParamSet, inData, cipherText);

return ret;
}

OH_Huks_Result HksRSACipherTestDecrypt(const struct OH_Huks_Blob *keyAlias,
const struct OH_Huks_ParamSet *decryptParamSet,
const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText,
const struct OH_Huks_Blob *inData) {
uint8_t handleD[sizeof(uint64_t)] = {0};
struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD};
OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
if (ret.errorCode != OH_HUKS_SUCCESS) {
  return ret;
}

ret = OH_Huks_FinishSession(&handleDecrypt, decryptParamSet, cipherText, plainText);

return ret;
}

static napi_value RSAEncDecKey(napi_env env, napi_callback_info info) {
char tmpKeyAlias[] = "test_enc_dec";
struct OH_Huks_Blob keyAlias = {(uint32_t)strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias};
struct OH_Huks_ParamSet *genParamSet = nullptr;
struct OH_Huks_ParamSet *encryptParamSet = nullptr;
struct OH_Huks_ParamSet *decryptParamSet = nullptr;
OH_Huks_Result ohResult;
do {
ohResult = InitParamSet(&genParamSet, g_genEncDecParams, sizeof(g_genEncDecParams) / sizeof(OH_Huks_Param));
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
ohResult = InitParamSet(&encryptParamSet, g_encryptParams, sizeof(g_encryptParams) / sizeof(OH_Huks_Param));
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
ohResult = InitParamSet(&decryptParamSet, g_decryptParams, sizeof(g_decryptParams) / sizeof(OH_Huks_Param));
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}
char tmpInData[] = "RSA_ECB_INDATA_1";
struct OH_Huks_Blob inData = {(uint32_t)strlen(tmpInData), (uint8_t *)tmpInData};
/* 1. Generate Key */
ohResult = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
  break;
}
/* 2. Encrypt */
uint8_t cipher[RSA_COMMON_SIZE] = {0};
struct OH_Huks_Blob cipherText = {RSA_COMMON_SIZE, cipher};
ohResult = HksRSACipherTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
  break;
}

/* 3. 解密 */
uint8_t plain[RSA_COMMON_SIZE] = {0};
struct OH_Huks_Blob plainText = {RSA_COMMON_SIZE, plain};
ohResult = HksRSACipherTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
OH_LOG_INFO(LOG_APP, "xxxxxx %{public}s", ohResult.data);
} while (0);
/* 4. Delete Key */
(void)OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);

OH_Huks_FreeParamSet(&genParamSet);
OH_Huks_FreeParamSet(&encryptParamSet);
OH_Huks_FreeParamSet(&decryptParamSet);

napi_value ret;
napi_create_int32(env, ohResult.errorCode, &ret);
return ret;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.

密钥格式请参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/huks-concepts-V5#%E5%AF%86%E9%92%A5%E6%9D%90%E6%96%99%E6%A0%BC%E5%BC%8F

分享
微博
QQ
微信
回复
2024-12-25 11:26:04
相关问题
RSA导入外部密钥实现加解密
1209浏览 • 1回复 待解决
HarmonyOS 加解密咨询
506浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
595浏览 • 1回复 待解决
HarmonyOS中有RSA加解密相关吗?
301浏览 • 0回复 待解决
HarmonyOSRSA加解密相关吗
854浏览 • 0回复 待解决
HarmonyOS 有没有相关rsa加解密内容
296浏览 • 1回复 待解决
HarmonyOS 是否有rsa加解密的例子?
285浏览 • 1回复 待解决
HarmonyOS 关于RSA公钥加解密问题
678浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1595浏览 • 1回复 待解决
HarmonyOS sm4、rsa加解密库及参考文档
1128浏览 • 1回复 待解决
多种加密方式实现加解密
1399浏览 • 1回复 待解决
HarmonyOS RSA解密问题
804浏览 • 1回复 待解决
HarmonyOS 加解密问题
513浏览 • 1回复 待解决
HarmonyOS RSA解密数据
245浏览 • 1回复 待解决
HarmonyOS 加解密 demo
957浏览 • 1回复 待解决
HarmonyOS RSA公钥解密
571浏览 • 1回复 待解决
HarmonyOSRSA解密问题?
266浏览 • 0回复 待解决
HarmonyOS AES加解密问题
596浏览 • 1回复 待解决
HarmonyOS 加解密算法匹配
447浏览 • 1回复 待解决
HarmonyOS AES-CBC加解密
478浏览 • 1回复 待解决