纯js实现字符串的加密和解密

加密:

function compileStr(code){
    var c=String.fromCharCode(code.charCodeAt(0)+code.length);  
    for(var i=1;i<code.length;i++){        
        c+=String.fromCharCode(code.charCodeAt(i)+code.charCodeAt(i-1));  
    }     
    return escape(c);
}

解密

function uncompileStr(code){
    code = unescape(code);        
    var c=String.fromCharCode(code.charCodeAt(0)-code.length);        
    for(var i=1;i<code.length;i++){        
        c+=String.fromCharCode(code.charCodeAt(i)-c.charCodeAt(i-1));        
    }        
    return c;
} 

例子;


let  str ='ccfdasdaghg123456';


console.log(compileStr(str))  // t%C6%C9%CA%C5%D4%D7%C5%C8%CF%CF%98cegik


console.log(uncompileStr(t%C6%C9%CA%C5%D4%D7%C5%C8%CF%CF%98cegik)) // ccfdasdaghg123456


落魄前端-小陈
0
0
0
评论
浏览
收藏