import CryptoJS from 'crypto-js';
const secretKey = CryptoJS.enc.Utf8.parse('asqwdjjfwnlgi');
// 加密函数
export function encryptData(data) {
let srcs = CryptoJS.enc.Utf8.parse(data);
let encrypted = CryptoJS.AES.encrypt(srcs, secretKey, {
iv: secretKey,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
// 解密函数
export function decryptData(ciphertext) {
var encrypted = CryptoJS.enc.Base64.parse(ciphertext);
let src = CryptoJS.enc.Base64.stringify(encrypted);
const bytes = CryptoJS.AES.decrypt(src, secretKey, {
iv: secretKey,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return bytes.toString(CryptoJS.enc.Utf8);
}
// 使用示例
// const encryptedData = encryptData(data);
// console.log('Encrypted data:', encryptedData);
// const decryptedData = decryptData(encryptedData);
// console.log('Decrypted data:', decryptedData);
import { decryptData, encryptData } from '@/utils/crypto';
console.log(encryptData('Hello'), '加密');
let v = encryptData('Hello')
console.log(decryptData(v),'解密1')
// 打印
![](https://i-blog.csdnimg.cn/direct/06f10d614cd54266bda933960656bff7.png)