AES加密-(登录注册)
第一步:npm 下载 crypto-js
第二步:在项目utils文件夹新建AES.js文件夹:
js代码:
import CryptoJS from 'crypto-js' const keyStr = 'xeegj7gggadwdddbtia-ow7x' export default { // 随机生成指定数量的16进制key generatekey(num) { const library = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' let key = '' for (var i = 0; i < num; i++) { const randomPoz = library.length key += library.substring(randomPoz, randomPoz + 1) } return key }, // 加密 encrypt(word) { var key = CryptoJS.enc.Utf8.parse(keyStr) var srcs = CryptoJS.enc.Utf8.parse(word) var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) return encrypted.toString() }, // 解密 decrypt(word) { var key = CryptoJS.enc.Utf8.parse(keyStr); var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return CryptoJS.enc.Utf8.stringify(decrypt).toString(); } }
第三步:接口api文件user.js:
import request from '@/utils/request' import AES from '@/utils/AES' // 注册方法 export function regist(ph, passwd, cd) { var encrypts = AES.encrypt(passwd) const password = encrypts const phone = ph const type = 'regist' const code = cd const data = { phone, password, type, code } return request({ url: '/api/user/v1/client/regist', method: 'post', data }) }
第四步:在组件使用regist方法