• Home
Name Date Size #Lines LOC

..--

AppScope/06-May-2025-3533

entry/06-May-2025-2,2472,014

hvigor/06-May-2025-88

screenshots/device/06-May-2025-

.gitignoreD06-May-2025159 1212

README.mdD06-May-20252.9 KiB3220

README_zh.mdD06-May-20256.4 KiB9363

build-profile.json5D06-May-20251.1 KiB4342

hvigorfile.tsD06-May-2025768 171

hvigorwD06-May-20252 KiB6253

hvigorw.batD06-May-20252 KiB7356

oh-package.json5D06-May-2025201 1211

ohosTest.mdD06-May-20251.2 KiB

README.md

1# Harmony Universal Keystore Service
2
3### Introduction
4
5This sample shows how `@ohos.security.huks` works to implement encryption and decryption of arbitrary input content. The implementation scenario is as follows:
6
71. Using a new key for message encryption and decryption transmission: Use the HUKS generated key interface to generate a new key locally, simulating the scenario where the device encrypts the sent message and decrypts the received message.
8
92. Using an old key for message encryption and decryption transmission: Import an old key using HUKS' import key interface to simulate the scenario of encrypting the sent message on the old device and decrypting the received message on the new device.
10
113. Click the **Minimum Access Control** button on the main page, enter the password to be saved in the input box of the new key interface, and click ** Save password **
12   Button, after entering the PIN code, the encrypted password content will be displayed in the lower text box, and then click ** Read Password ** button, and the decrypted message content will be displayed in the lower text box.
13
14### Usage
15
161. Click the **New Key** button on the main page, enter the message content to be sent in the input box of the new key interface, and click **Send Message**
17button, the encrypted message content will be displayed in the text box below, and then click the **Receive Message** button. The decrypted message content will be displayed in the text box below.
182. Click the **Back** button in the upper left corner to return to the main interface.
193. Click on the **Old Key** button on the main page, enter the message content to be sent in the input box of the old key interface, and click **Send Message**
20button, the encrypted message content will be displayed in the text box below. First, click the **Import Key** button, and a pop-up window will prompt that the key was successfully imported. Then, click the **Receive Message** button, and the decrypted message content will be displayed in the text box below.
214. Click the **Minimum Access Control** button on the main page, enter the message content to be sent in the input box of the new key interface, and click **Save Password**
22button, the encrypted password content will be displayed in the text box below, and then click the **Read Password** button. After entering the PIN code(lock screen password, need to be recorded in advance), the decrypted password content will be displayed in the text box below.
23
24### Constraints
25
261. This sample can only be run on standard-system devices.
27
282. This sample has been adapted to the API version 10 SDK (4.0.10.11).
29
303. This sample requires DevEco Studio 3.1.1 Release (Build Version: 3.1.0.501) to compile and run.
31
324. If this example is run on a device without a device certificate, you need to run the 'hdc shell setenforce 0' command on the local command line to ensure that the example function can run properly.

README_zh.md

1# 通用密钥库系统
2
3### 介绍
4
5本示例使用[@ohos.security.huks](https://docs.openharmony.cn/pages/v4.0/en/application-dev/reference/apis/js-apis-huks.md)相关接口实现了对任意输入内容进行加密和解密的功能。
6
7实现场景如下:
8
91)使用新密钥进行消息加解密传输:使用HUKS的生成密钥接口在本地生成一个新密钥,模拟设备对发送的消息进行加密和对接收的消息进行解密场景。
10
112)使用旧密钥进行消息加解密传输:使用HUKS的导入密钥接口导入一个旧密钥,模拟在旧设备上对发送的消息进行加密,在新设备上对接收的消息进行解密的场景。
12
133)使用低安访问控制进行密码加解密:使用HUKS的生成密钥接口在本地生成一个新的密钥,模拟设备对密码进行加密和对接收的密码进行解密场景。
14
15### 效果预览
16
17| 主页                                | 使用新密钥消息加解密页面       | 使用旧密钥消息加解密页面                       | 使用低安访问控制保存/读取密码                              |
18|-----------------------------------|--------------------|------------------------------------|----------------------------------------------|
19| ![](screenshots/device/index.png) | ![](screenshots/device/newKey.png) | ![](screenshots/device/oldKey.png) | ![](screenshots/device/minAccessControl.png) |
20
21使用说明
22
231.点击主页面的**新密钥**按钮,在新密钥界面的输入框中输入待发送的消息内容,点击**发送消息**
24按钮,下方文本框中显示加密后的消息内容,然后点击**接收消息**按钮,下方文本框中显示解密后的消息内容。
25
262.点击主页面的**旧密钥**按钮,在旧密钥界面的输入框中输入待发送的消息内容,点击**发送消息**
27按钮,下方文本框中显示加密后的消息内容,首先点击**导入密钥**按钮,弹窗提示导入密钥成功,然后点击**接收消息**按钮,下方文本框中显示解密后的消息内容。
28
293.点击主页面的**低安访问控制**按钮,在新密钥界面的输入框中输入待保存的密码内容,点击**保存密码**
30按钮,下方文本框中显示加密后的密码内容,录入锁屏密码, 然后点击**读取密码**按钮,在输入PIN码(锁屏密码,需要预先录入)之后,下方文本框中显示解密后的消息内容。
31
32### 工程目录
33
34```
35entry/src/main/ets/
36|---common
37|   |---NewKey.ets                                            // 新密钥组件
38|   |---OldKey.ets                                            // 旧密钥组件
39|   |---MinAccessControl.ets                                  // 低安访问控制组件
40|---entryability
41|   |---EntryAbility.ts
42|---model
43|   |---HuksModel.ts                                          // 通用密钥库模型文件
44|   |---Logger.ts                                             // 日志文件
45|---pages
46|   |---Index.ets                                             // 主页界面
47|   |---SelectPage.ets                                        // 选择跳转界面
48```
49
50### 具体实现
51
52* 使用新密钥、旧密钥与低安访问控制进行消息的加密和解密的功能接口封装在HuksModel中,源码参考:[HuksModel.ts](entry/src/main/ets/model/HuksModel.ts)
53    * 使用新密钥加密:在[NewKey.ets](entry/src/main/ets/common/NewKey.ets)文件中通过HuksModel.encryptData方式调用加密函数实现对发送的消息内容进行加密,设置好生成密钥和解密密钥的密钥属性信息,加密前会先调用生成密钥接口
54      [huks.generateKeyItem](https://docs.openharmony.cn/pages/v4.0/en/application-dev/reference/apis/js-apis-huks.md#huksgeneratekeyitem9)
55      生成用于加密的AES密钥,对消息加密的过程中采用三段式接口[huks.initSession,huks.updateSession,huks.finishSession](https://docs.openharmony.cn/pages/v4.0/en/application-dev/reference/apis/js-apis-huks.md#huksinitsession9)
56      完成加密操作。
57    * 使用新密钥解密:在[NewKey.ets](entry/src/main/ets/common/NewKey.ets)文件中通过HuksModel.decryptData方式调用解密函数实现对发送消息的解密,设置好解密密钥的密钥属性信息,使用加密时的密钥对消息进行解密,仍然是采用三段式接口完成解密操作,解密动作完成后调用删除密钥接口
58      [huks.deleteKeyItem](https://docs.openharmony.cn/pages/v4.0/en/application-dev/reference/apis/js-apis-huks.md#huksdeletekeyitem9)清除本地保存的密钥。
59    * 使用旧密钥加密:在[OldKey.ets](entry/src/main/ets/common/OldKey.ets)文件中通过HuksModel.encryptDataUserOldKey方式调用加密函数对发送的消息内容进行加密,设置好加密密钥的密钥属性信息,采用三段式接口使用本地已保存的旧密钥完成加密操作。
60    * 使用旧密钥解密:在[OldKey.ets](entry/src/main/ets/common/OldKey.ets)文件中通过HuksModel.decryptDataUserOldKey方式调用解密函数对发送的消息内容进行解密,首先模拟在新设备上导入旧设备的加密密钥,同样采用三段式接口完成解密操作,解密完成后删除密钥。
61    * 使用低安访问控制管理密码:在[MinAccessControl.ets](entry/src/main/ets/common/MinAccessControl.ets)文件中通过 huksModel.encryptDataUseSm2 方式生成SM2密钥并调用加密函数对发送的消息内容进行加密,设置好加密密钥的密钥属性信息,采用两段式接口使用本地已保存的旧密钥完成加密操作。读取密码时(需先设定系统锁屏密码)调用IAM的PIN码(锁屏密码)接口进行验证。
62
63
64### 相关权限
65
66ohos.permission.ACCESS_BIOMETRIC
67
68### 依赖
69
70不涉及。
71
72### 约束与限制
73
741.本示例仅支持标准系统上运行。
75
762.本示例支持API10版本SDK,SDK版本号(API Version 10 Release),镜像版本号(4.0Release)。
77
783.本示例需要使用DevEco Studio 版本号(4.0Release)及以上版本才可编译运行。
79
804.本示例在无设备证书的设备中运行时,需预先在本机命令行中"hdc shell setenforce 0"命令,才可保证示例功能正常执行。
81
82### 下载
83
84如需单独下载本工程,执行如下命令:
85
86```
87git init
88git config core.sparsecheckout true
89echo code/BasicFeature/Security/Huks/ > .git/info/sparse-checkout
90git remote add origin https://gitee.com/openharmony/applications_app_samples.git
91git pull origin master
92
93```