AES加密、解密
import CryptoJS from 'crypto-js/crypto-js' // 默认的 KEY 与 iv 与后端保持一致 ,不采用后端传值密钥 const KEY = CryptoJS.enc.Utf8.parse('xxxxxxxxxxxxxxxx')// 密钥 (16位) const IV = CryptoJS.enc.Utf8.parse('xxxxxxxxxxxxxxxx')// 偏移量(16位) /** * AES加密 :字符串 key iv 返回base64 */ export function Encrypt(word, keyStr, ivStr) { let key = KEY let iv = IV if (keyStr) { key = CryptoJS.enc.Utf8.parse(keyStr) iv = CryptoJS.enc.Utf8.parse(ivStr) } const srcs = CryptoJS.enc.Utf8.parse(word) var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding }) return CryptoJS.enc.Base64.stringify(encrypted.ciphertext) } /** * AES 解密 :字符串 key iv 返回base64 * */ export function Decrypt(word, keyStr, ivStr) { let key = KEY let iv = IV if (keyStr) { key = CryptoJS.enc.Utf8.parse(keyStr) iv = CryptoJS.enc.Utf8.parse(ivStr) } const base64 = CryptoJS.enc.Base64.parse(word) const src = CryptoJS.enc.Base64.stringify(base64) var decrypt = CryptoJS.AES.decrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding }) var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8) return decryptedStr.toString() }
使用
import { Encrypt, Decrypt } from '@/utils/AES'
解密:Encrypt(this.loginForm.passwords)
加密:Decrypt(password)