更新记录
1.0.0(2025-10-31) 下载此版本
找了半天没有适配鸿蒙的常用加密方法,找资料做了一个常用加密的插件
平台兼容性
uni-app(4.76)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| × | - | × | × | - | - | × | × | 19 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × |
鸿蒙Next加密UTS插件
适用于鸿蒙Next系统的UniApp UTS加密插件,支持AES ECB、MD5和SM2加密算法。
系统要求
- 鸿蒙Next系统 5.1.1+
- UniApp HBuilderX 3.9+
- API版本 12+
安装方法
- 将插件导入到uni_modules目录
- 在pages.json中配置插件
- 在代码中引入使用
功能特性
- ✅ AES ECB模式加密解密
- ✅ MD5消息摘要
- ✅ SM2公钥加密
- ✅ 异步API调用
- ✅ 符合鸿蒙Next加密框架规范
使用方法
1. 导入插件
import { harmonyEncrypt } from '@/uni_modules/harmony-encrypt';
2. AES ECB加密解密
// 加密
async encryptData() {
try {
const key = '1234567890123456'; // 16字节密钥
const plainText = 'Hello World';
const encrypted = await harmonyEncrypt.aesECBEncrypt(plainText, key);
console.log('加密结果:', encrypted);
} catch (error) {
console.error('加密失败:', error);
}
}
// 解密
async decryptData() {
try {
const key = '1234567890123456'; // 16字节密钥
const encrypted = 'base64加密字符串';
const decrypted = await harmonyEncrypt.aesECBDecrypt(encrypted, key);
console.log('解密结果:', decrypted);
} catch (error) {
console.error('解密失败:', error);
}
}
3. MD5加密
async hashData() {
try {
const plainText = 'Hello World';
const md5Hash = await harmonyEncrypt.md5Encrypt(plainText);
console.log('MD5结果:', md5Hash);
} catch (error) {
console.error('MD5加密失败:', error);
}
}
4. SM2公钥加密
async sm2EncryptData() {
try {
const publicKey = '04' + '公钥数据(128位十六进制)';
const plainText = 'Hello World';
const encrypted = await harmonyEncrypt.sm2Encrypt(plainText, publicKey);
console.log('SM2加密结果:', encrypted);
} catch (error) {
console.error('SM2加密失败:', error);
}
}
Vue3组件中使用示例
<template>
<view class="container">
<button @click="testEncrypt">测试加密</button>
<text>{{ result }}</text>
</view>
</template>
<script setup>
import { ref } from 'vue';
import { harmonyEncrypt } from '@/uni_modules/harmony-encrypt';
const result = ref('');
const testEncrypt = async () => {
try {
// 测试AES加密
const key = '1234567890123456';
const plainText = 'Hello Harmony Next';
const encrypted = await harmonyEncrypt.aesECBEncrypt(plainText, key);
// 测试解密
const decrypted = await harmonyEncrypt.aesECBDecrypt(encrypted, key);
// 测试MD5
const md5Hash = await harmonyEncrypt.md5Encrypt(plainText);
result.value = `
原文: ${plainText}
AES加密: ${encrypted}
AES解密: ${decrypted}
MD5: ${md5Hash}
`;
} catch (error) {
result.value = '加密测试失败: ' + error.message;
}
};
</script>
API参考
harmonyEncrypt.aesECBEncrypt(plainText, key)
- 描述: AES ECB模式加密
- 参数:
plainText: string - 明文key: string - 16字节密钥
- 返回: Promise
- Base64编码的密文
harmonyEncrypt.aesECBDecrypt(encryptedBase64, key)
- 描述: AES ECB模式解密
- 参数:
encryptedBase64: string - Base64编码的密文key: string - 16字节密钥
- 返回: Promise
- 明文
harmonyEncrypt.md5Encrypt(plainText)
- 描述: MD5消息摘要
- 参数:
plainText: string - 明文
- 返回: Promise
- 32位十六进制字符串
harmonyEncrypt.sm2Encrypt(plainText, publicKey)
- 描述: SM2公钥加密
- 参数:
plainText: string - 明文publicKey: string - 带04前缀的128位十六进制公钥
- 返回: Promise
- 带04前缀的十六进制密文
注意事项
- 异步调用: 所有加密方法都是异步的,需要使用
await关键字 - 密钥长度: AES密钥必须是16字节(128位)
- 公钥格式: SM2公钥必须是128位十六进制字符串,带04前缀
- 错误处理: 建议使用try-catch处理可能的加密错误
- 性能: 加密操作可能消耗一定资源,避免在主线程执行大量加密
更新日志
v1.0.3 (2025-01-01)
- 修复MD5 digest返回值类型错误
- 修复SM2 convertKey API调用错误
- 所有加密方法改为异步模式
- 更新加密算法规范
v1.0.2 (2024-12-31)
- 修复Base64Helper同步方法调用
- 修复cryptoFramework API参数类型
v1.0.1 (2024-12-30)
- 初始版本发布
- 支持AES ECB、MD5、SM2加密
许可证
MIT License
技术支持
如有问题,请在GitHub提交Issue或联系开发者。

收藏人数:
下载插件并导入HBuilderX
赞赏(0)
下载 5
赞赏 0
下载 12197496
赞赏 1828
赞赏
京公网安备:11010802035340号