需要引入一个JS文件
/** DES 加密操作
* @param {Object} txtString 需要加密的字符串
* @param {Object} KEY DES加密的KEY
* @param {Object} IV DES加密的偏移量
*/
function JiaMi(txtString, KEY, IV) {
var key = CryptoJS.enc.Utf8.parse(KEY);
var iv = CryptoJS.enc.Utf8.parse(IV);
var encrypted = CryptoJS.DES.encrypt(txtString, key, {iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
/** 解密操作
* @param {Object} txtString 需要解密的字符
* @param {Object} KEY DES加密的KEY
* @param {Object} IV DES加密的偏移量
*/
function JieMi(txtString, KEY, IV){
var key = CryptoJS.enc.Utf8.parse(KEY);
var iv = CryptoJS.enc.Utf8.parse(IV);
var decrypt = CryptoJS.DES.decrypt(txtString, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
return decrypt.toString();
}
var txtString = "看中国足球加油!";
//加密
var jStr =JiaMi(txtString,"ssssssss","ssssssss");//blZMKqO/RE2/3irkSavVKDx6wNn87ZKq8ccq61RlWiVH0iXhOD5FTQ==
console.log(jStr);
//解密
console.log(JieMi(jStr,"ssssssss","ssssssss"));//看中国足球加油!