字符串加密

字符串加密

加密操作时常也是会用到,比如用在自动登录的情况下,将账号密码进行加密后保存,打开网页自动执行登录操作的过程

那么问题来了,如果对账号密码进行保存而保持一定的安全性呢,这时候就需要对字符串进行加密操作了,当然如何解密也只有自己知道

首先来看四个函数

function setCookie(c_name,value,expiredays){
  var exdate=new Date()
  exdate.setDate(exdate.getDate()+expiredays)
  document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function getCookie(c_name){
  if (document.cookie.length>0){
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1){ 
      c_start=c_start + c_name.length+1 
      c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
        return unescape(document.cookie.substring(c_start,c_end))
      } 
    }
  return ""
}
2.js对字符串进行加密和解密
function compileStr(code){ //对字符串进行加密
  code = String(code)       
  var c=''; 
  for(var i=0;i<code.length;i++){        
    c+=String.fromCharCode(code.charCodeAt(i)+998); //+998 对数字编码加密,当然可以是任意自己下想要的数字
  }     
 return escape(c);   
}  

//字符串进行解密   
function uncompileStr(code){        
 code=unescape(code);       
 var c='';        
 for(var i=0;i<code.length;i++){        
  c+=String.fromCharCode(code.charCodeAt(i)-998);        
 }        
 return c;   
}  

经过上面的加密操作后就可以正常存入cookie或者localStroage了,解密后请直接使用