/*
* 利用String對象的charCodeAt()方法和fromCharCode()方法對可用JSON.parse進行序列化的數據進行加密的數據加密解密
* Author: zhangji
* Create: 2019.10.22
**/
const Crypto = {
//加密
encryption(data) {
data = JSON.stringify(data)
let str = '';
let alterText = [];
let varCost = [];
const TextLength = data.length;
for (let i = 0; i < TextLength; i++) {
let random = parseInt(Math.random() * 266);
alterText[i] = data.charCodeAt(i) + random;
varCost[i] = random;
}
for (let i = 0; i < TextLength; i++) {
str += String.fromCharCode(alterText[i], varCost[i]);
}
return str;
},
//解密
decrypt(text) {
let str = '';
let alterText = [];
let varCost = [];
const TextLength = text.length;
for (let i = 0; i < TextLength; i++) {
alterText[i] = text.charCodeAt(i);
varCost[i] = text.charCodeAt(i + 1);
}
for (let i = 0; i < TextLength; i = i + 2) {
str += String.fromCharCode(alterText[i] - varCost[i]);
}
return JSON.parse(str);
}
};
export default Crypto;
JS 可逆加密的一種實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.