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
1天前
浏览
收藏 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;
}

密钥格式请参考:

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
微信
回复
1天前
相关问题
RSA导入外部密钥实现加解密
813浏览 • 1回复 待解决
HarmonyOS 加解密咨询
31浏览 • 1回复 待解决
HarmonyOS AES加解密咨询
87浏览 • 1回复 待解决
HarmonyOSRSA加解密相关吗
461浏览 • 0回复 待解决
HarmonyOS 是否有rsa加解密的例子?
45浏览 • 1回复 待解决
HarmonyOS 关于RSA公钥加解密问题
33浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1101浏览 • 1回复 待解决
多种加密方式实现加解密
1081浏览 • 1回复 待解决
HarmonyOS RSA解密问题
490浏览 • 1回复 待解决
HarmonyOS 加解密问题
47浏览 • 1回复 待解决
HarmonyOS RSA公钥解密
16浏览 • 1回复 待解决
HarmonyOS RSA解密数据
34浏览 • 1回复 待解决
HarmonyOS 加解密 demo
532浏览 • 1回复 待解决
HarmonyOS AES加解密问题
29浏览 • 1回复 待解决
HarmonyOS 加解密算法匹配
69浏览 • 1回复 待解决
HarmonyOS 加解密算法如何使用
64浏览 • 1回复 待解决
HarmonyOS RSA如何用公钥解密
666浏览 • 0回复 待解决