Skip to content

Instantly share code, notes, and snippets.

@anyangdp
Last active November 11, 2021 03:19
Show Gist options
  • Save anyangdp/014df98fb125f643bcd214b5d21ca30c to your computer and use it in GitHub Desktop.
Save anyangdp/014df98fb125f643bcd214b5d21ca30c to your computer and use it in GitHub Desktop.
小程序文件上传加密处理(包括不仅限于图片、视频)支持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)
}
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment