var CryptoJS = require("crypto-js");
import Constant from "../const/Constant";
export default class CryptoJsUtil {
static getAesString(data, key, iv) {//加密
// debugger
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(data, key,
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString(); //返回的是base64格式的密文
}
static getDAesString(encrypted, key, iv) {//解密
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var decrypted = CryptoJS.AES.decrypt(encrypted, key,
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
static getAES(data) { //加密
// debugger
var key = Constant.KEY; //密钥
var iv = Constant.KEY;
var encrypted = CryptoJsUtil.getAesString(data, key, iv); //密文
var encrypted1 = CryptoJS.enc.Utf8.parse(encrypted);
return encrypted;
}
static getDAes(data) {//解密
var key = Constant.KEY; //密钥
var iv = Constant.KEY;
var decryptedStr = CryptoJsUtil.getDAesString(data, key, iv);
return decryptedStr;
}
static getMd5(data) {
return CryptoJS.MD5(data)
}
}
JS加密解密工具
最新推荐文章于 2024-07-18 15:50:27 发布