• Home
Name Date Size #Lines LOC

..--

AppScope/22-Oct-2025-3633

entry/22-Oct-2025-2,9792,695

hvigor/22-Oct-2025-2422

screenshots/device/22-Oct-2025-

.gitignoreD22-Oct-202598 99

README.mdD22-Oct-20255 KiB5336

README_zh.mdD22-Oct-20258.9 KiB13195

build-profile.json5D22-Oct-20251.1 KiB4241

hvigorfile.tsD22-Oct-2025159 21

hvigorwD22-Oct-20252 KiB6253

hvigorw.batD22-Oct-20252.1 KiB8060

oh-package.json5D22-Oct-2025859 2725

ohosTest.mdD22-Oct-20254.8 KiB2523

README.md

1# Crypto Framework
2
3### Introduction
4
5This sample shows how [@ohos.security.cryptoFramework](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md) works to implement the function of encrypting, decrypting, signing and verifying text file. The implementation scenario is as follows:
6
71. The software needs to encrypt and store the local text file, and it needs to call Crypto Framework to select the key text file and encrypt the local text file, among which the key text file can be generated by Crypto Framework.
8
92. The software needs to decrypt and store the local text file, and needs to call Crypto Framework to select the key text file and decrypt the local text file, where the key text file can be generated by Crypto Framework.
10
113. The software needs to sign the stored local text file, call the encryption and Crypto Framework to select the key text file, and sign the integrity of the local text file, among which the key text file can be generated by Crypto Framework.
12
134. The software needs to verify the signature integrity of the stored local text file, and needs to call Crypto Framework to select the key text file and the signature file, and verify and sign the file, among which the key text file can be generated by Crypto Framework.
14
15### Usage
16
171. Click **Encrypted** in the tab bar of the main page, click **Open File** in the encrypt interface, select the text file through the file chooser, and automatically return to the encrypt interface after selection.
18   Its contents are displayed in the **File Content** text box,
19   Click **Select Key** to select the key file, if there is no key file, you can generate and save the key file through the file chooser by clicking **Generate AES Key Randomly** on the encryption interface.
20   After selecting the text file and key file, click the **Encrypt** button to complete the encryption and save the ciphertext file through the file chooser.s
21   The encrypted message content is displayed in the **Encrypted Content** text box below.
22
232. Click **Decrypt** in the tab bar of the main page, click **Open File** on the decrypt interface, select the text file through the file chooser, and automatically return to the decrypt interface after selection.
24   Its contents are displayed in the **File Content** text box,
25   Click **Select Key** to select the same key file as Encryption.
26   After selecting the text file and key file, click the **Decrypt** button to complete the decryption and save the plaintext file through the file chooser.
27   The decrypted message content is displayed in the **Decrypted Content** text box below.
28
293. Click **Signature** in the tab bar of the main page, click **Open File** on the sign interface, select the text file through the file chooser, and automatically return to the sign interface after selection.
30   Its contents are displayed in the **File Content** text box,
31   Click **Select Key** to select the key file, if there is no key file, you can generate and save the key file through the file selector by clicking **Generate RSA Key Randomly** on the signature interface, note that RSA key generation takes a long time.
32   After selecting the text file and key file, click the **Sign** button to complete the integrity signature and save the signature file through the file chooser.
33
344. Click the **Verify** tab bar of the main page, click **Open File** on the verify interface, select the text file through the file chooser, and automatically return to the verify interface after selection.
35   Its contents are displayed in the **File Content** text box,
36   Click **Select Key** to select a key file that matches the signature. Click **Select Signature File** to select the signature file saved when signing through the file chooser,
37   After selecting the text file, key file, and signature file, click the **Verify** button to complete the signature verification.
38
39### Dependency
40
41* filePicker[@ohos.file.picker](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-core-file-kit/js-apis-file-picker.md)
42* fileManager[@ohos.file.fs](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-core-file-kit/js-apis-file-fs.md)
43* UI Ability Context[@ohos.app.ability.common](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis-ability-kit/js-apis-app-ability-common.md)
44
45### Constraints
46
471. This sample can only be run on standard-system devices.
482. This sample has been adapted to the API version 9 SDK, version number 3.2.12.2.
493. This sample requires DevEco Studio 3.1 Release(Build Version: 3.1.0.500) to compile and run.
504. This sample uses filePicker to select file, the files saved with filePicker, such as new keys generated, can be updated in real time, but the files directly pushed to the folder, they requires reboot the device and refresh the display.
515. This sample supports text file currently only English characters, and as a demonstration, the length of text that can be used successfully does not exceed 8,000 bytes.
52
53

README_zh.md

1# 通用密钥库系统
2
3### 介绍
4
5本示例使用[@ohos.security.cryptoFramework](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md)
6相关接口实现了对文本文件的加解密、签名验签操作。
7
8实现场景如下:
9
101)软件需要加密存储本地文本文件,需要调用加解密算法库框架选择密钥文本文件,对本地文本文件进行加密,其中密钥文本文件可由加解密算法库生成。
11
122)软件需要解密存储本地文本文件,需要调用加解密算法库框架选择密钥文本文件,对本地文本文件进行解密,其中密钥文本文件可由加解密算法库生成。
13
143)软件需要对存储本地文本文件签名,调用加解密算法库框架选择密钥文本文件,对本地文本文件进行完整性签名,其中密钥文本文件可由加解密算法库生成。
15
164)软件需要验证存储本地文本文件的签名完整性,需要调用加解密算法库框架选择密钥文本文件与签名文件,对文件进行验签,其中密钥文本文件可由加解密算法库生成。
17
18### 效果预览
19
20| 加密页面                                 | 解密页面                                 | 签名页面                              | 验签界面                                |
21|--------------------------------------|--------------------------------------|-----------------------------------|-------------------------------------|
22| ![](screenshots/device/encrypt.jpeg) | ![](screenshots/device/decrypt.jpeg) | ![](screenshots/device/sign.jpeg) | ![](screenshots/device/verify.jpeg) |
23
24使用说明
25
261.点击主页面tab栏的**加密**,在加密界面点击**打开文件**,通过文件选择器选择文本文件,选择完毕自动返回加密界面,
27其内容显示在**文件内容**文本框中,
28点击**选择密钥**,选择密钥文件,如果没有密钥文件,可以通过在加密界面点击**随机生成AES密钥**,生成并通过文件选择器保存密钥文件。
29选择完文本文件和密钥文件后,点击**加密**按钮,完成加密,并通过文件选择器保存密文文件。
30下方**加密内容**文本框中显示加密后的消息内容。
31
322.点击主页面tab栏的**解密**,在解密界面点击**打开文件**,通过文件选择器选择文本文件,选择完毕自动返回解密界面,
33其内容显示在**文件内容**文本框中,
34点击**选择密钥**,选择和**加密相同**的密钥文件。
35选择完文本文件和密钥文件后,点击**解密**按钮,完成解密,并通过文件选择器保存明文文件。
36下方**解密内容**文本框中显示解密后的消息内容。
37
383.点击主页面tab栏的**签名**,在签名界面点击**打开文件**,通过文件选择器选择文本文件,选择完毕自动返回签名界面,
39其内容显示在**文件内容**文本框中,
40点击**选择密钥**,选择密钥文件,如果没有密钥文件,可以通过在签名界面点击**随机生成RSA密钥**,生成并通过文件选择器保存密钥文件,注意RSA密钥生成耗时较长。
41选择完文本文件和密钥文件后,点击**签名**按钮,完成完整性签名,并通过文件选择器保存签名文件。
42
434.点击主页面tab栏的**验签**,在验签界面点击**打开文件**,通过文件选择器选择文本文件,选择完毕自动返回验签界面,
44其内容显示在**文件内容**文本框中,
45点击**选择密钥**,选择和签名一致的密钥文件。点击**选择签名文件**,通过文件选择器选择签名时保存的签名文件,
46选择完文本文件、密钥文件和签名文件后,点击**验签**按钮,完成验签。
47
48### 工程目录
49
50```
51entry/src/main/ets/
52|---cryptoframework
53|   |---CryptoOperation.ts                                    // 封装加解密算法库相关算法能力
54|---entryability
55|   |---EntryAbility.ts
56|---pages
57|   |---Index.ets                                             // 主页界面
58|---tab
59|   |---Decrypt.ets                                           // 解密tab界面
60|   |---Encrpyt.ts                                            // 加密tab界面
61|   |---Sign.ts                                               // 签名tab界面
62|   |---Verify.ts                                             // 验签tab界面
63|---textfilemanager
64|   |---TextFileManager.ts                                    // 封装文件读写相关能力
65|---util
66|   |---Logger.ts                                             // 日志文件
67```
68
69### 具体实现
70
71*
72
73进行对文本文件进行加解密、签名验签的功能接口封装在CryptoOperation中,源码参考:[CryptoOperation.ts](entry/src/main/ets/cryptoframework/CryptoOperation.ts)
74
75* 对文本文件加密:在[Encrypt.ets](entry/src/main/ets/tab/Encrypt.ets)
76  文件中依次点击打开文件、选择密钥,通过filePicker选择明文文本文件和密钥文件,点击加密按钮,调用加密函数实现对文本内容进行加密,并将密文文件通过filePicker保存。
77  需要生成密钥文本文件时,可以点击随机生成AES密钥,通过调用[cryptoFramework.symKeyGenerator](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#symkeygenerator)
78  生成用于加密的AES密钥,并通过filePicker保存密钥文本文件。
79  对消息加密的过程中采用[cryptoFramework.Cipher](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cipher)
80  完成加密操作。
81* 对文本文件解密:在[Decrypt.ets](entry/src/main/ets/tab/Decrypt.ets)
82  文件中依次点击打开文件、选择密钥,通过filePicker选择密文文本文件和密钥文件,点击解密按钮,调用解密函数实现对文本内容进行解密,并将明文文件通过filePicker保存。
83  对消息解密的过程中采用[cryptoFramework.Cipher](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cipher)
84  完成解密操作。
85* 对文本文件签名:在[Sign.ets](entry/src/main/ets/tab/Sign.ets)
86  文件中依次点击打开文件、选择密钥,通过filePicker选择待签名文本文件和密钥文件,点击签名按钮,调用签名函数实现对文本内容进行加密,并将签名文件通过filePicker保存。
87  需要生成密钥文本文件时,可以点击随机生成RSA密钥,通过调用[cryptoFramework.asyKeyGenerator](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#asykeygenerator)
88  生成用于签名的RSA密钥,并通过filePicker保存密钥文本文件。
89  对消息签名的过程中采用[cryptoFramework.Sign](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#sign)
90  完成加密操作。
91* 对文本文件验签:在[Verify.ets](entry/src/main/ets/tab/Verify.ets)
92  文件中依次点击打开文件、选择密钥、选择签名文件,通过filePicker选择密文文本文件、密钥文件和签名文件,点击验签按钮,调用验签函数实现对文本内容进行验签,并显示验签结果。
93  对消息验签的过程中采用[cryptoFramework.Verify](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#verify)
94  完成验签操作。
95
96### 相关权限
97
98不涉及。
99
100### 依赖
101
102* filePicker文件选择器[@ohos.file.picker](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-file-picker.md)
103
104* 文件管理[@ohos.file.fs](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-file-fs.md)
105
106* 应用上下文Context[@ohos.app.ability.common](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-ability-kit/js-apis-app-ability-common.md)
107
108### 约束与限制
109
1101.本示例仅支持标准系统上运行。
111
1122.本示例已适配API version 9版本SDK,版本号:3.2.12.2,镜像版本号:OpenHarmony 4.0.7.2。
113
1143.本示例需要使用DevEco Studio 3.1 Release (Build Version: 3.1.0.500)及以上版本才可编译运行。
115
1164.本示例所选择文件,用filePicker文件选择器保存的文件,比如新增的密钥,能够实时更新,如直接推送至文件夹,需要重启后刷新显示。
117
1185.本示例所选择文本文件内容当前只支持英文字符,作为演示,能成功使用的文本长度不超过8000字节。
119
120### 下载
121
122如需单独下载本工程,执行如下命令:
123
124```
125git init
126git config core.sparsecheckout true
127echo code/BasicFeature/Security/CryptoFramework/ > .git/info/sparse-checkout
128git remote add origin https://gitee.com/openharmony/applications_app_samples.git
129git pull origin master
130
131```