当前位置: 首页 > news >正文

鸿蒙开发:Universal Keystore Kit(密钥管理服务)【加解密(C/C++)】

加解密(C/C++)

以AES 256密钥为例,完成加解密。具体的场景介绍及支持的算法规格。

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

   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)

开发步骤

生成密钥

  1. 指定密钥别名。
  2. 初始化密钥属性集。
  3. 调用OH_Huks_GenerateKeyItem生成密钥)。
  4. 开发前请熟悉鸿蒙开发指导文档gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

除此之外,开发者也可以参考[密钥导入],导入已有的密钥。

加密

  1. 获取密钥别名。
  2. 获取待加密的数据。
  3. 调用[OH_Huks_InitParamSet]指定算法参数配置。 在下方示例中,使用算法AES进行加密时,必须要选择其对应分组模式以及填充模式,用例中选取的分组模式为CBC、填充模式为PKCS7,此时必须要填参数IV。
  4. 调用[OH_Huks_InitSession]初始化密钥会话,并获取会话的句柄handle。
  5. 调用[OH_Huks_FinishSession]结束密钥会话,获取加密后的密文。

解密

  1. 获取密钥别名。
  2. 获取待解密的密文。
  3. 调用[OH_Huks_InitParamSet]指定算法参数配置。 在下方示例中,使用算法AES进行解密时,必须要选择其对应分组模式以及填充模式,用例中选取的分组模式为CBC、填充模式为PKCS7,此时必须要填参数IV。
  4. 调用[OH_Huks_InitSession]初始化密钥会话,并获取会话的句柄handle。
  5. 调用[OH_Huks_FinishSession]结束密钥会话,获取解密后的数据。
  6. HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿

QQ截图20240705210937.png

删除密钥

当密钥废弃不用时,需要调用OH_Huks_DeleteKeyItem删除密钥。

#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 const uint32_t IV_SIZE = 16;
static uint8_t IV[IV_SIZE] = { 0 }; // this is a test value, for real use the iv should be different every time
static struct OH_Huks_Param g_genEncDecParams[] = {{.tag = OH_HUKS_TAG_ALGORITHM,.uint32Param = OH_HUKS_ALG_AES}, {.tag = OH_HUKS_TAG_PURPOSE,.uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT}, {.tag = OH_HUKS_TAG_KEY_SIZE,.uint32Param = OH_HUKS_AES_KEY_SIZE_256}, {.tag = OH_HUKS_TAG_PADDING,.uint32Param = OH_HUKS_PADDING_NONE}, {.tag = OH_HUKS_TAG_BLOCK_MODE,.uint32Param = OH_HUKS_MODE_CBC}
};
static struct OH_Huks_Param g_encryptParams[] = {{.tag = OH_HUKS_TAG_ALGORITHM,.uint32Param = OH_HUKS_ALG_AES}, {.tag = OH_HUKS_TAG_PURPOSE,.uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT}, {.tag = OH_HUKS_TAG_KEY_SIZE,.uint32Param = OH_HUKS_AES_KEY_SIZE_256}, {.tag = OH_HUKS_TAG_PADDING,.uint32Param = OH_HUKS_PADDING_NONE}, {.tag = OH_HUKS_TAG_BLOCK_MODE,.uint32Param = OH_HUKS_MODE_CBC}, {.tag = OH_HUKS_TAG_IV,.blob = {.size = IV_SIZE,.data = (uint8_t *)IV // this is a test value, for real use the iv should be different every time }}
};
static struct OH_Huks_Param g_decryptParams[] = {{.tag = OH_HUKS_TAG_ALGORITHM,.uint32Param = OH_HUKS_ALG_AES}, {.tag = OH_HUKS_TAG_PURPOSE,.uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT}, {.tag = OH_HUKS_TAG_KEY_SIZE,.uint32Param = OH_HUKS_AES_KEY_SIZE_256}, {.tag = OH_HUKS_TAG_PADDING,.uint32Param = OH_HUKS_PADDING_NONE}, {.tag = OH_HUKS_TAG_BLOCK_MODE,.uint32Param = OH_HUKS_MODE_CBC}, {.tag = OH_HUKS_TAG_IV,.blob = {.size = IV_SIZE,.data = (uint8_t *)IV // this is a test value, for real use the iv should be different every time }}
};
static const uint32_t AES_COMMON_SIZE = 1024;
OH_Huks_Result HksAesCipherTestEncrypt(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 HksAesCipherTestDecrypt(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 EncDecKey(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 {/* 1. Generate Key *//** 模拟生成密钥场景* 1.1. 确定密钥别名*//** 1.2. 获取生成密钥算法参数配置*/ohResult = InitParamSet(&genParamSet, g_genEncDecParams, sizeof(g_genEncDecParams) / sizeof(OH_Huks_Param));if (ohResult.errorCode != OH_HUKS_SUCCESS) {break;}/** 1.3. 调用generateKeyItem*/ohResult = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);if (ohResult.errorCode != OH_HUKS_SUCCESS) {break;}/* 2. Encrypt *//** 模拟加密场景* 2.1. 获取密钥别名*//** 2.2. 获取待加密的数据*//** 2.3. 获取加密算法参数配置*/ohResult = InitParamSet(&encryptParamSet, g_encryptParams, sizeof(g_encryptParams) / sizeof(OH_Huks_Param));if (ohResult.errorCode != OH_HUKS_SUCCESS) {break;}char tmpInData[] = "AES_ECB_INDATA_1";struct OH_Huks_Blob inData = { (uint32_t)strlen(tmpInData), (uint8_t *)tmpInData };uint8_t cipher[AES_COMMON_SIZE] = {0};struct OH_Huks_Blob cipherText = {AES_COMMON_SIZE, cipher};/** 2.4. 调用initSession获取handle*//** 2.5. 调用finishSession获取加密后的密文*/ohResult = HksAesCipherTestEncrypt(&keyAlias, encryptParamSet, &inData, &cipherText);if (ohResult.errorCode != OH_HUKS_SUCCESS) {break;}/* 3. Decrypt *//** 模拟解密场景* 3.1. 获取密钥别名*//** 3.2. 获取待解密的密文*//** 3.3. 获取解密算法参数配置*/ohResult = InitParamSet(&decryptParamSet, g_decryptParams, sizeof(g_decryptParams) / sizeof(OH_Huks_Param));if (ohResult.errorCode != OH_HUKS_SUCCESS) {break;}uint8_t plain[AES_COMMON_SIZE] = {0};struct OH_Huks_Blob plainText = {AES_COMMON_SIZE, plain};/** 3.4. 调用initSession获取handle*//** 3.5. 调用finishSession获取解密后的数据*/ohResult = HksAesCipherTestDecrypt(&keyAlias, decryptParamSet, &cipherText, &plainText, &inData);} while (0);/* 4. Delete Key *//** 模拟删除密钥场景* 4.1. 获取密钥别名*//** 4.2. 调用deleteKeyItem删除密钥    */(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;
}
http://www.lryc.cn/news/396723.html

相关文章:

  • Python的入门知识(上)
  • 2024春秋杯网络安全联赛夏季赛-PWN
  • 怎么提高音频声音大小?提高音频声音大小的四种方法
  • 从数据仓库到数据湖(下):热门的数据湖开源框架
  • 对话AI原生 | 千帆AppBuilder重构企业AI原生应用开发体验
  • CF253C Text Editor 题解
  • Spring Boot 创建定时任务
  • Vue使用Echarts(入门级)
  • 2025届秋招提前批信息汇总(计算机类)
  • Scala Collection(集合)
  • Go 语言 UUID 库 google/uuid 源码解析:UUID version4 的实现
  • 开发个人Go-ChatGPT--6 OpenUI
  • Spring中的工厂模式详解及应用示例
  • Electron 简单搭建项目
  • 旗晟智能巡检机器人:开启工业运维的智能化新篇章
  • vue3的常用 Composition API有哪些?
  • 深度优先算法-DFS(算法篇)
  • C++模块化之内部类
  • k8s-第九节-命名空间
  • 【AI大模型新型智算中心技术体系深度分析 2024】
  • 王道计算机数据结构+插入排序、冒泡排序、希尔排序、快速排序、简单选择排序
  • python爬虫学习(三十三天)---多线程上篇
  • JavaScript 原型链那些事
  • nginx的知识面试易考点
  • 每日Attention学习9——Efficient Channel Attention
  • Java语言程序设计——篇三(1)
  • 基于SpringBoot实现轻量级的动态定时任务调度
  • 夸克升级“超级搜索框” 推出AI搜索为中心的一站式AI服务
  • element-ui el-select选择器组件下拉框增加自定义按钮
  • Python基于you-get下载网页上的视频