更新记录

1.0.0(2025-04-28) 下载此版本

1.0.0(2023-06-24)

发布插件


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 3.98 app-vue app-nvue
钉钉小程序 快手小程序 飞书小程序 京东小程序 鸿蒙元服务
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari

gm-crypt-nodejs

JavaScript Style GuideLicense: MIT

gm-crypt在安卓端报错,本插件是将TextEncoder引入到插件内部,解决安卓端TextEncoder报错问题

使用方法和原插件一样

Roadmap

  • [√] SM4
  • [ ] SM3
  • [ ] SM2

复制下面的代码到新建的crypt.js

const SM4 = require("@/uni_modules/zwz-gm-crypt").sm4;
const sm4Config = {
  // encrypt/decypt main key; cannot be omitted
  key: 'JeF8U9wHFOMfs2Y8',

  // optional; can be 'cbc' or 'ecb'
  mode: 'cbc', // default

  // optional; when use cbc mode, it's necessary
  iv: 'UISwD9fW6cFh9SNS', // default is null 可以不传

  // optional: this is the cipher data's type; Can be 'base64' or 'text'
  cipherType: 'base64' // default is base64  可以不传
};
let sm4 = new SM4(sm4Config); // 上面的key和iv可以自己修改

export function encode(str) {
    if(typeof str != 'string') {
        str = JSON.stringify(str)
    }
    return sm4.encrypt(str)
}

export function decode(str) {
    if(!str) return str
    let result = sm4.decrypt(str)
    if(result) {
        result = JSON.parse(result)
    }
    return result
}

在请求拦截器和响应拦截器,或者其他要加解密的页面中引入crypt.js

引入

import {encode,decode} from "**/crypt.js"; // 改成你crypt.js所在的目录

加密

let encrypted = encode(data);
console.log('已加密的数据===>',encrypted)

解密

const decrypted = decode(data);
console.log('解密的数据===>',decrypted)

SM4 下面是官方说明

Documentation

Init

const SM4 = require('gm-crypt').sm4

let sm4Config = {
  // encrypt/decypt main key; cannot be omitted
  key: 'JeF8U9wHFOMfs2Y8',

  // optional; can be 'cbc' or 'ecb'
  mode: 'cbc', // default

  // optional; when use cbc mode, it's necessary
  iv: 'UISwD9fW6cFh9SNS', // default is null

  // optional: this is the cipher data's type; Can be 'base64' or 'text'
  cipherType: 'base64' // default is base64
}

let sm4 = new SM4(sm4Config)

Encrypt

let plaintext = '中国国密加解密算法'
let ciphertext = sm4.encrypt(plaintext)
// ciphertext's result is 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='

Decrypt

let ciphertext = 'j/+HgSpv8RZQI2YtSq0L1RnemiSokMm1VvLHSTt245U='
let plaintext = sm4.decrypt(ciphertext)
// plaintext's result is '中国国密加解密算法'

隐私、权限声明

1. 本插件需要申请的系统权限列表:

无.

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问