0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

鸿蒙开发:Universal Keystore Kit密钥管理服务 明文导入密钥 ArkTS

jf_46214456 来源:jf_46214456 作者:jf_46214456 2024-07-08 10:22 次阅读

明文导入密钥(ArkTS)

分别以导入AES256与RSA2048密钥为例,具体的场景介绍及支持的算法规格

开发步骤

  1. 指定密钥别名keyAlias。 密钥别名的最大长度为64字节。
  2. 封装密钥属性集和密钥材料。
    • 密钥属性集同样与密钥生成中指定的密钥属性一致,须包含[HuksKeyAlg]、[HuksKeySize]、[HuksKeyPurpose]属性。
    • 密钥材料须符合[HUKS密钥材料格式],并以Uint8Array形式赋值给[HuksOptions]的inData字段。
  3. 调用[huks.importKeyItem],传入密钥别名和密钥属性集,即可导入密钥。
  4. 开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]
/* 以下以导入AES256密钥的Callback操作使用为例 */
import { huks } from "@kit.UniversalKeystoreKit"
/* 密钥材料 */
let plainTextSize32 = new Uint8Array([
    0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63, 0x2a, 0x7c, 0x86, 0xba, 0xca,
    0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa, 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f, 0x9e, 0x3c, 0xe5, 0xf9
]);
/* 1.确定密钥别名 */
let keyAlias = 'AES256Alias_sample';
/* 2.封装密钥属性集和密钥材料 */

let properties: Array< huks.HuksParam > = [
    {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value:huks.HuksKeyAlg.HUKS_ALG_AES
    },
    {
        tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
        value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256
    },
    {
        tag: huks.HuksTag.HUKS_TAG_PURPOSE,
        value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
    },
]
let options: huks.HuksOptions = {
    properties: properties,
    inData: plainTextSize32
};
/* 3.明文导入密钥 */
try {
    huks.importKeyItem(keyAlias, options, (error, data) = > {
        if (error) {
            console.error(`callback: importKeyItem failed` + error);
        } else {
            console.info(`callback: importKeyItem success`);
        }
    });
} catch (error) {
    console.error(`callback: importKeyItem input arg invalid` + error);
}
/* 以下以导入RSA2048密钥的Callback操作使用为例 */
import { huks } from "@kit.UniversalKeystoreKit"
let rsa2048KeyPairMaterial = new Uint8Array([
  0x01, 0x00, 0x00, 0x00, // 密钥算法(小端表示)huks.HuksKeyAlg.HUKS_ALG_RSA = 1
  0x00, 0x08, 0x00, 0x00, // 密钥大小(比特):2048
  0x00, 0x01, 0x00, 0x00, // 模数n长度(字节):256
  0x03, 0x00, 0x00, 0x00, // 公钥指数e长度(字节):3
  0x00, 0x01, 0x00, 0x00, // 私钥指数d长度(字节):256
  // 模数n
  0xc5, 0x35, 0x62, 0x48, 0xc4, 0x92, 0x87, 0x73, 0x0d, 0x42, 0x96, 0xfc, 0x7b, 0x11, 0x05, 0x06,
  0x0f, 0x8d, 0x66, 0xc1, 0x0e, 0xad, 0x37, 0x44, 0x92, 0x95, 0x2f, 0x6a, 0x55, 0xba, 0xec, 0x1d,
  0x54, 0x62, 0x0a, 0x4b, 0xd3, 0xc7, 0x05, 0xe4, 0x07, 0x40, 0xd9, 0xb7, 0xc2, 0x12, 0xcb, 0x9a,
  0x90, 0xad, 0xe3, 0x24, 0xe8, 0x5e, 0xa6, 0xf8, 0xd0, 0x6e, 0xbc, 0xd1, 0x69, 0x7f, 0x6b, 0xe4,
  0x2b, 0x4e, 0x1a, 0x65, 0xbb, 0x73, 0x88, 0x6b, 0x7c, 0xaf, 0x7e, 0xd0, 0x47, 0x26, 0xe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0x00, 0x01,
  // 私钥指数d
  0x88, 0x4b, 0x82, 0xe7, 0xe3, 0xe3, 0x99, 0x75, 0x6c, 0x9e, 0xaf, 0x17, 0x44, 0x3e, 0xd9, 0x07,
  0xfd, 0x4b, 0xae, 0xce, 0x92, 0xc4, 0x28, 0x44, 0x5e, 0x42, 0x79, 0x08, 0xb6, 0xc3, 0x7f, 0x58,
  0x2d, 0xef, 0xac, 0x4a, 0x07, 0xcd, 0xaf, 0x46, 0x8f, 0xb4, 0xc4, 0x43, 0xf9, 0xff, 0x5f, 0x74,
  0x2d, 0xb5, 0xe0, 0x1c, 0xab, 0xf4, 0x6e, 0xd5, 0xdb, 0xc8, 0x0c, 0xfb, 0x76, 0x3c, 0x38, 0x66,
  0xf3, 0x7f, 0x01, 0x43, 0x7a, 0x30, 0x39, 0x02, 0x80, 0xa4, 0x11, 0xb3, 0x04, 0xd9, 0xe3, 0x57,
  0x23, 0xf4, 0x07, 0xfc, 0x91, 0x8a, 0xc6, 0xcc, 0xa2, 0x16, 0x29, 0xb3, 0xe5, 0x76, 0x4a, 0xa8,
  0x84, 0x19, 0xdc, 0xef, 0xfc, 0xb0, 0x63, 0x33, 0x0b, 0xfa, 0xf6, 0x68, 0x0b, 0x08, 0xea, 0x31,
  0x52, 0xee, 0x99, 0xef, 0x43, 0x2a, 0xbe, 0x97, 0xad, 0xb3, 0xb9, 0x66, 0x7a, 0xae, 0xe1, 0x8f,
  0x57, 0x86, 0xe5, 0xfe, 0x14, 0x3c, 0x81, 0xd0, 0x64, 0xf8, 0x86, 0x1a, 0x0b, 0x40, 0x58, 0xc9,
  0x33, 0x49, 0xb8, 0x99, 0xc6, 0x2e, 0x94, 0x70, 0xee, 0x09, 0x88, 0xe1, 0x5c, 0x4e, 0x6c, 0x22,
  0x72, 0xa7, 0x2a, 0x21, 0xdd, 0xd7, 0x1d, 0xfc, 0x63, 0x15, 0x0b, 0xde, 0x06, 0x9c, 0xf3, 0x28,
  0xf3, 0xac, 0x4a, 0xa8, 0xb5, 0x50, 0xca, 0x9b, 0xcc, 0x0a, 0x04, 0xfe, 0x3f, 0x98, 0x68, 0x81,
  0xac, 0x24, 0x53, 0xea, 0x1f, 0x1c, 0x6e, 0x5e, 0xca, 0xe8, 0x31, 0x0d, 0x08, 0x12, 0xf3, 0x26,
  0xf8, 0x5e, 0xeb, 0x10, 0x27, 0xae, 0xaa, 0xc3, 0xad, 0x6c, 0xc1, 0x89, 0xdb, 0x7d, 0x5a, 0x12,
  0x55, 0xad, 0x11, 0x19, 0xa1, 0xa9, 0x8f, 0x0b, 0x6d, 0x78, 0x8d, 0x1c, 0xdf, 0xe5, 0x63, 0x82,
  0x0b, 0x7d, 0x23, 0x04, 0xb4, 0x75, 0x8c, 0xed, 0x77, 0xfc, 0x1a, 0x85, 0x29, 0x11, 0xe0, 0x61,
]);

/* 1.确定密钥别名 */
let keyAlias = 'RSA_sample';
/* 2.封装密钥属性集和密钥材料 */
let properties: Array< huks.HuksParam > = [
  {
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
    value:huks.HuksKeyAlg.HUKS_ALG_RSA
  },
  {
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048
  },
  {
// 此 tag表示密钥导入后的用途,导入后将不可更改
    tag: huks.HuksTag.HUKS_TAG_PURPOSE,
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT
  },
  {
// 此 tag表示需导入的密钥类型
    tag: huks.HuksTag.HUKS_TAG_IMPORT_KEY_TYPE,
// 此 value表示导入密钥对,若改为HUKS_KEY_TYPE_PUBLIC_KEY时表示仅导入公钥
    value: huks.HuksImportKeyType.HUKS_KEY_TYPE_KEY_PAIR 
  },
]
let options: huks.HuksOptions = {
  properties: properties,
  inData: rsa2048KeyPairMaterial
};
/* 3.明文导入密钥 */
try {
  huks.importKeyItem(keyAlias, options, (error, data) = > {
    if (error) {
      console.error(`callback: importKeyItem failed` + error);
    } else {
      console.info(`callback: importKeyItem success`);
    }
  });
} catch (error) {
  console.error(`callback: importKeyItem input arg invalid` + error);
}

调测验证

调用[huks.isKeyItemExist]验证密钥是否存在,如密钥存在即表示密钥导入成功。

QQ截图20240705211227.png

`HarmonyOSOpenHarmony鸿蒙文档籽料:mau123789是v直接拿`

import { huks } from "@kit.UniversalKeystoreKit";
let keyAlias = 'AES256Alias_sample';
let isKeyExist = false;

let keyProperties: Array< huks.HuksParam > = [
    {
        tag: huks.HuksTag.HUKS_TAG_ALGORITHM,
        value: huks.HuksKeyAlg.HUKS_ALG_AES
    }
]
let huksOptions: huks.HuksOptions = {
    properties: keyProperties, // 非空填充
    inData: new Uint8Array(new Array()) // 非空填充
}
try {
    huks.isKeyItemExist(keyAlias, huksOptions, (error, data) = > {
        if (error) {
            console.error(`callback: isKeyItemExist failed` + error);
        } else {
            if (data !== null && data.valueOf() !== null) {
                isKeyExist = data.valueOf();
                console.info(`callback: isKeyItemExist success, isKeyExist = ${isKeyExist}`);
            }
        }
    });
} catch (error) {
    console.error(`callback: isKeyItemExist input arg invalid` + error);
}

审核编辑 黄宇

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 密钥
    +关注

    关注

    1

    文章

    133

    浏览量

    19649
  • 鸿蒙
    +关注

    关注

    56

    文章

    2265

    浏览量

    42456
收藏 人收藏

    评论

    相关推荐

    鸿蒙开发Universal Keystore Kit 密钥管理服务 查询密钥别名集 ArkTS

    HUKS提供了接口供应用查询密钥别名集。
    的头像 发表于 07-18 09:34 186次阅读

    鸿蒙开发Universal Keystore Kit 密钥管理服务 获取密钥属性ArkTS

    HUKS提供了接口供业务获取指定密钥的相关属性。在获取指定密钥属性前,需要确保已在HUKS中生成或导入持久化存储的密钥
    的头像 发表于 07-17 10:46 208次阅读

    鸿蒙开发Universal Keystore Kit 密钥管理服务 获取密钥属性C C++

    HUKS提供了接口供业务获取指定密钥的相关属性。在获取指定密钥属性前,需要确保已在HUKS中生成或导入持久化存储的密钥
    的头像 发表于 07-17 09:47 189次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b> <b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> 获取<b class='flag-5'>密钥</b>属性C C++

    鸿蒙开发Universal Keystore Kit 密钥管理服务 HMAC ArkTS

    HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code),是一种基于Hash函数和密钥进行消息认证的方法。
    的头像 发表于 07-12 18:22 491次阅读

    鸿蒙开发Universal Keystore Kit密钥管理服务 密钥删除ArkTS

    为保证数据安全性,当不需要使用该密钥时,应该删除密钥
    的头像 发表于 07-12 14:56 183次阅读

    鸿蒙开发Universal Keystore Kit密钥管理服务 密钥派生C、C++

    以HKDF256密钥为例,完成密钥派生。具体的场景介绍及支持的算法规格,请参考[密钥生成支持的算法]。
    的头像 发表于 07-11 14:28 174次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b><b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> <b class='flag-5'>密钥</b>派生C、C++

    鸿蒙开发Universal Keystore Kit 密钥管理服务 密钥协商 C、C++

    以协商密钥类型为ECDH,并密钥仅在HUKS内使用为例,完成密钥协商。具体的场景介绍及支持的算法规格,请参考[密钥生成支持的算法]。
    的头像 发表于 07-10 14:27 202次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b> <b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> <b class='flag-5'>密钥</b>协商 C、C++

    鸿蒙开发Universal Keystore Kit 密钥管理服务 密钥协商ArkTS

    以协商密钥类型为X25519 256,并密钥仅在HUKS内使用为例,完成密钥协商。
    的头像 发表于 07-10 09:22 203次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b> <b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> <b class='flag-5'>密钥</b>协商<b class='flag-5'>ArkTS</b>

    鸿蒙开发Universal Keystore Kit 密钥管理服务 密钥使用介绍及通用流程

    为了实现对数据机密性、完整性等保护,可使用生成/导入密钥,对数据进行密钥操作
    的头像 发表于 07-09 11:56 273次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b> <b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> <b class='flag-5'>密钥</b>使用介绍及通用流程

    鸿蒙开发Universal Keystore Kit密钥管理服务 加密导入密钥C、C++

    以加密导入ECDH密钥对为例,涉及业务侧加密密钥的[密钥生成]、[协商]等操作不在本示例中体现。
    的头像 发表于 07-08 15:26 228次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b><b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> 加密<b class='flag-5'>导入</b><b class='flag-5'>密钥</b>C、C++

    鸿蒙开发Universal Keystore Kit 密钥管理服务 加密导入密钥 ArkTS

    以加密导入ECDH密钥对为例,涉及业务侧加密密钥的[密钥生成]、[协商])等操作不在本示例中体现。
    的头像 发表于 07-08 14:22 205次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b> <b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> 加密<b class='flag-5'>导入</b><b class='flag-5'>密钥</b> <b class='flag-5'>ArkTS</b>

    鸿蒙开发Universal Keystore Kit密钥管理服务 明文导入密钥C、C++

    明文导入ECC密钥为例。具体的场景介绍及支持的算法规格
    的头像 发表于 07-08 10:01 207次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b><b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> <b class='flag-5'>明文</b><b class='flag-5'>导入</b><b class='flag-5'>密钥</b>C、C++

    鸿蒙开发Universal Keystore Kit密钥管理服务 密钥导入介绍及算法规格

    如果业务在HUKS外部生成密钥(比如应用间协商生成、服务器端生成),业务可以将密钥导入到HUKS中由HUKS进行管理
    的头像 发表于 07-06 10:45 534次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b><b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> <b class='flag-5'>密钥</b><b class='flag-5'>导入</b>介绍及算法规格

    鸿蒙开发Universal Keystore Kit密钥管理服务 生成密钥ArkTS

    以生成DH密钥为例,生成随机密钥。具体的场景介绍及支持的算法规格
    的头像 发表于 07-05 15:17 203次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b><b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b> 生成<b class='flag-5'>密钥</b><b class='flag-5'>ArkTS</b>

    鸿蒙开发Universal Keystore Kit密钥管理服务简介

    Universal Keystore Kit密钥管理服务,下述简称为HUKS)向业务/应用提供
    的头像 发表于 07-04 14:20 184次阅读
    <b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>Universal</b> <b class='flag-5'>Keystore</b> <b class='flag-5'>Kit</b><b class='flag-5'>密钥</b><b class='flag-5'>管理</b><b class='flag-5'>服务</b>简介