zoukankan      html  css  js  c++  java
  • JS 可逆加密的一种实现

    /* 
     * 利用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;
    
  • 相关阅读:
    curl常用选项
    cuda
    mysql 备份文件.xbstream 恢复到本地
    firewall 常用命令(update...)
    ownCloud 研究笔记(update...)
    V3
    English trip EM3-LP-3A ROOMMATES Teacher:Corrine
    V3
    English trip EM3-LP-5A Shopping Teacher:Taylor
    新概念 Lesson 11 Which book?
  • 原文地址:https://www.cnblogs.com/YooHoeh/p/12098869.html
Copyright © 2011-2022 走看看