vue项目登录密码加密-AES

vue登录密码加密-AES


login.js
引入模块

	import request from '@/utils/request'
	import CryptoJS from 'crypto-js/crypto-js' //aes加密
	import { getKeyiv } from './reward' //api

调用加密规则

	function xwok() {
	    let key = ''
	    let iv = ''
	    return new Promise(resolve => {
	        getKeyiv().then(res => {
	            key = res.data.key
	            iv = res.data.iv
	            resolve({
	                key,
	                iv
	            })
	        })
	    })
	}

AES加密,返回base64

	export async function Encrypt(word, keyStr, ivStr) {
	  //获取async函数的返回值,获取resolve并解析
	    let obl = await xwok()
	    // console.log(obl)
	    let key = CryptoJS.enc.Utf8.parse(obl.key)
	    let iv = CryptoJS.enc.Utf8.parse(obl.iv)
	    if (keyStr) {
	        key = CryptoJS.enc.Utf8.parse(keyStr);
	        iv = CryptoJS.enc.Utf8.parse(ivStr);
	    }
	
	    let srcs = CryptoJS.enc.Utf8.parse(word);
	    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
	        iv: iv,
	        mode: CryptoJS.mode.CBC,
	        padding: CryptoJS.pad.Pkcs7
	    });
	    // console.log("-=-=-=-", CryptoJS.enc.Base64.stringify(encrypted.ciphertext))
	    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
	}

登录

	export async function login(username, password) {
	    let npss = await Encrypt(password)
	    return request({
	        url: '/api/login',
	        method: 'POST',
	        data: {
	            username,
	            password:npss
	        }
	    })
	}

你可能感兴趣的:(javascript,vue)