用到插件:crypto-js
安 装:yarn add crypto-js
引 入:import CryptoJS from 'crypto-js';
封装
加 密:
cryptoJSFun(value,valueKey) {
let cipherText = CryptoJS.AES.encrypt(value, valueKey).toString();
return cipherText;
},
//value : 需要加密的值
//valueKey: 密匙,解密要用到
解 密:
getCryptoJSFun(value,valueKey) {
let bytes = CryptoJS.AES.decrypt(value, valueKey);
let cipherText = bytes.toString(CryptoJS.enc.Utf8);
return cipherText;
},
//value: 需要解密的值
//valueKey: 跟加密时的密匙保持一致
本 地 存:
uni.setStorageSync('pw', this.cryptoJSFun('需加密的值','passwordkey'));
本 地 取:
let pw = this.getCryptoJSFun(uni.getStorageSync('pw'), 'passwordkey');
console.log('保存的密码', pw);