AES加密解密(vue)

需求:字符串加密(防止敏感数值直接查看)

步骤一:安装crypto-js库

npm install crypto-js --save

步骤二:在需要的地方引入

import crypto  from 'crypto-js';
//或
const crypto = require('crypto-js');

步骤三:加密方式

let str = 'hello你好';
let key = 'isakey'; // 密钥自己定
let encrypted = crypto.AES.encrypt(str, key).toString();
console.log(encrypted) //加密后输出U2FsdGVkX1+ZfV7LHDw4DmYr+1dU5X5p6hKvj6Ia8mE=;

步骤四:解密方式

let encrypted = 'U2FsdGVkX1+ZfV7LHDw4DmYr+1dU5X5p6hKvj6Ia8mE=';
let key = 'isakey'; // 密钥
let decrypted = crypto.AES.decrypt(encrypted, key).toString(enc.Utf8);
console.log(decrypted) //解密后输出hello你好;

你可能感兴趣的:(工作遇到的问题,vue.js,javascript,前端)