对于文字内容较少的需要加密的可以采用这种形式;对于内容较多的,市面上很多采用base64和md5也是不错的选择,最好的我认为还是后端和前端共同约定处理加密更稳妥。
- 直接调用去用也可以,加密规则也可以自行修改
var qqSecret = {
dCode: new Date().getDate(),
/**
* 加密
* @param str
* @returns {string}
*/
add: function (str) {
let strCodeArr = [], strCodeStr = '';
for (let k = 0; k < str.length; k++) {
strCodeArr.push(String.fromCharCode((str.charCodeAt(k)) + this.dCode));
}
// 每个加密字用qq*连接作为分隔
strCodeStr = strCodeArr.join("*qq*");
return strCodeStr;
},
/**
* 解密
* @param str
* @returns {string|string}
*/
remove: function (str) {
var openSecretStr = '', openSecretArr = str.split('*qq*');
for (let k = 0; k < openSecretArr.length; k++) {
openSecretStr += String.fromCharCode((openSecretArr[k].charCodeAt(0) - this.dCode));
}
return openSecretStr;
}
};
var str5='hello hi!承认你忘不掉我的色彩!';
console.log(qqSecret.add(str5));
console.log(qqSecret.remove(qqSecret.add(str5)));
- 这是思路
// 加密
let str4='你好哈哈哈123',str4CodeArr=[],str4CodeStr='',sCode=new Date().getDate();
for(let k=0;k<str4.length;k++){
// 这是对应位置正常的charcode
// str4CodeArr.push((str4.charCodeAt(k)));
// 把正常的chacode加今天的日期数然后用fromCharCode 返回。
str4CodeArr.push(String.fromCharCode((str4.charCodeAt(k))+sCode));
}
// 每个加密字用qq*连接作为分隔
str4CodeStr=str4CodeArr.join("*qq*");
console.log(str4CodeStr);
// 解密
var openSecret='',openSecretArr=str4CodeStr.split('*qq*');
for(let k=0;k<openSecretArr.length;k++){
openSecret+=String.fromCharCode((openSecretArr[k].charCodeAt(0)-sCode));
}
console.log(openSecret);
网友评论