解密/加密方法: Decrypt, Encrypt
import CryptoJS from 'crypto-js'; //引用AES源码js
import moment from 'moment';
// const CryptoJS = require('crypto-js')
const key = CryptoJS.enc.Utf8.parse('dPCtSgMDTKAgWjY1'); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse(''); //十六位十六进制数作为密钥偏移量
const keyGG = CryptoJS.enc.Utf8.parse(moment().format('YYYYMMDD') + moment().format('YYYYMMDD')); //十六位十六进制数作为密钥
//解密方法
export function Decrypt(word) {
let encryptedHexStr = CryptoJS.enc.Base64.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
//加密方法
export function Encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
//输出16进制
let aesString = encrypted.ciphertext.toString();
let encryptedHexStr = CryptoJS.enc.Hex.parse(aesString);
let resultString = CryptoJS.enc.Base64.stringify(encryptedHexStr);
return resultString;
// //base64输出
// return encrypted;
}
export default {
Decrypt,
Encrypt,
};
标签:enc,加密,Utf8,解密,iv,parse,let,CryptoJS
From: https://blog.csdn.net/zuo0407/article/details/143188467