AES encryption and decryption (vue)

Requirements: String encryption (to prevent sensitive values ​​from being viewed directly)

Step 1: Install crypto-js library

npm install crypto-js --save

Step 2: Introduce it where needed

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

Step 3: Encryption method

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

Step 4: Decryption method

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

Guess you like

Origin blog.csdn.net/weixin_44056717/article/details/130006850