小程序文件上传加密处理(包括不仅限于图片、视频aes)

  1. 加密所需插件(cryptojs)
    github地址:https://github.com/brix/crypto-js
    文档地址:https://cryptojs.gitbook.io/docs/#ciphers
安装包
npm install crypto-js
小程序引入:
const CryptoJs = require('crypto-js');
使用小程序api中FileSystemManager.readFile方法读取文件流,可以使用binary或者base64的encoding
const fs = wx.getFileSystemManager()
fs.readFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  encoding: 'binary',
  position: 0,
  success(res) {
    console.log(res.data)
    /**
    * 根据key的位数,决定了使用128,196,256加密,保证后端加密使用同样的key,同样的mode和padding
    */
    const defaultKey = CryptoJs.enc.Utf8.parse("12232"); // 默认的key
    let aesObj = CryptoJs.AES.encrypt("originStr", defaultKey, {
            mode: CryptoJs.mode.ECB,
            padding: CryptoJs.pad.Pkcs7
        });
    // 此时aesObj是AES对象,传输到服务端,可以直接执行aesObj.toSting(),自动将其转化为base64字符串
    // 执行post请求,将其发送到服务端
  },
  fail(res) {
    console.error(res)
  }
})

https://gist.github.com/anyangdp/014df98fb125f643bcd214b5d21ca30c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值