html5中文学习网

您的位置: 首页 > 网站及特效实例 > javascript特效 » 正文

javascript将url中的参数加密解密代码_javascript技巧_

[ ] 已经帮助:人解决问题

今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码:JpBHTML5中文学习网 - HTML5先行者学习网

复制代码 代码如下:
JpBHTML5中文学习网 - HTML5先行者学习网
<SCRIPT LANGUAGE="JavaScript">    JpBHTML5中文学习网 - HTML5先行者学习网
<!-- Begin    JpBHTML5中文学习网 - HTML5先行者学习网
function Encrypt(str, pwd) {    JpBHTML5中文学习网 - HTML5先行者学习网
    if(str=="")return "";    JpBHTML5中文学习网 - HTML5先行者学习网
    str = escape(str);    JpBHTML5中文学习网 - HTML5先行者学习网
    if(!pwd || pwd==""){ var pwd="1234"; }    JpBHTML5中文学习网 - HTML5先行者学习网
    pwd = escape(pwd);    JpBHTML5中文学习网 - HTML5先行者学习网
      if(pwd == null || pwd.length <= 0) {    JpBHTML5中文学习网 - HTML5先行者学习网
        alert("Please enter a password with which to encrypt the message.");    JpBHTML5中文学习网 - HTML5先行者学习网
          return null;    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      var prand = "";    JpBHTML5中文学习网 - HTML5先行者学习网
      for(var I=0; I<pwd.length; I++) {    JpBHTML5中文学习网 - HTML5先行者学习网
        prand += pwd.charCodeAt(I).toString();    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      var sPos = Math.floor(prand.length / 5);    JpBHTML5中文学习网 - HTML5先行者学习网
      var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));    JpBHTML5中文学习网 - HTML5先行者学习网
      var incr = Math.ceil(pwd.length / 2);    JpBHTML5中文学习网 - HTML5先行者学习网
      var modu = Math.pow(2, 31) - 1;    JpBHTML5中文学习网 - HTML5先行者学习网
      if(mult < 2) {    JpBHTML5中文学习网 - HTML5先行者学习网
        alert("Algorithm cannot find a suitable hash. Please choose a different password. /nPossible considerations are to choose a more complex or longer password.");    JpBHTML5中文学习网 - HTML5先行者学习网
        return null;    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      var salt = Math.round(Math.random() * 1000000000) % 100000000;    JpBHTML5中文学习网 - HTML5先行者学习网
      prand += salt;    JpBHTML5中文学习网 - HTML5先行者学习网
      while(prand.length > 10) {    JpBHTML5中文学习网 - HTML5先行者学习网
        prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      prand = (mult * prand + incr) % modu;    JpBHTML5中文学习网 - HTML5先行者学习网
    var enc_chr = "";    JpBHTML5中文学习网 - HTML5先行者学习网
    var enc_str = "";    JpBHTML5中文学习网 - HTML5先行者学习网
    for(var I=0; I<str.length; I++) {    JpBHTML5中文学习网 - HTML5先行者学习网
        enc_chr = parseInt(str.charCodeAt(I) ^ Math.floor((prand / modu) * 255));    JpBHTML5中文学习网 - HTML5先行者学习网
        if(enc_chr < 16) {    JpBHTML5中文学习网 - HTML5先行者学习网
            enc_str += "0" + enc_chr.toString(16);    JpBHTML5中文学习网 - HTML5先行者学习网
        }else    JpBHTML5中文学习网 - HTML5先行者学习网
            enc_str += enc_chr.toString(16);    JpBHTML5中文学习网 - HTML5先行者学习网
        prand = (mult * prand + incr) % modu;    JpBHTML5中文学习网 - HTML5先行者学习网
    }    JpBHTML5中文学习网 - HTML5先行者学习网
      salt = salt.toString(16);    JpBHTML5中文学习网 - HTML5先行者学习网
      while(salt.length < 8)salt = "0" + salt;    JpBHTML5中文学习网 - HTML5先行者学习网
    enc_str += salt;    JpBHTML5中文学习网 - HTML5先行者学习网
    return enc_str;    JpBHTML5中文学习网 - HTML5先行者学习网
}    JpBHTML5中文学习网 - HTML5先行者学习网
function Decrypt(str, pwd) {    JpBHTML5中文学习网 - HTML5先行者学习网
    if(str=="")return "";    JpBHTML5中文学习网 - HTML5先行者学习网
    if(!pwd || pwd==""){ var pwd="1234"; }    JpBHTML5中文学习网 - HTML5先行者学习网
    pwd = escape(pwd);    JpBHTML5中文学习网 - HTML5先行者学习网
      if(str == null || str.length < 8) {    JpBHTML5中文学习网 - HTML5先行者学习网
        alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");    JpBHTML5中文学习网 - HTML5先行者学习网
        return;    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      if(pwd == null || pwd.length <= 0) {    JpBHTML5中文学习网 - HTML5先行者学习网
        alert("Please enter a password with which to decrypt the message.");    JpBHTML5中文学习网 - HTML5先行者学习网
        return;    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      var prand = "";    JpBHTML5中文学习网 - HTML5先行者学习网
      for(var I=0; I<pwd.length; I++) {    JpBHTML5中文学习网 - HTML5先行者学习网
        prand += pwd.charCodeAt(I).toString();    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      var sPos = Math.floor(prand.length / 5);    JpBHTML5中文学习网 - HTML5先行者学习网
      var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));    JpBHTML5中文学习网 - HTML5先行者学习网
      var incr = Math.round(pwd.length / 2);    JpBHTML5中文学习网 - HTML5先行者学习网
      var modu = Math.pow(2, 31) - 1;    JpBHTML5中文学习网 - HTML5先行者学习网
      var salt = parseInt(str.substring(str.length - 8, str.length), 16);    JpBHTML5中文学习网 - HTML5先行者学习网
      str = str.substring(0, str.length - 8);    JpBHTML5中文学习网 - HTML5先行者学习网
      prand += salt;    JpBHTML5中文学习网 - HTML5先行者学习网
      while(prand.length > 10) {    JpBHTML5中文学习网 - HTML5先行者学习网
        prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();    JpBHTML5中文学习网 - HTML5先行者学习网
      }    JpBHTML5中文学习网 - HTML5先行者学习网
      prand = (mult * prand + incr) % modu;    JpBHTML5中文学习网 - HTML5先行者学习网
      var enc_chr = "";    JpBHTML5中文学习网 - HTML5先行者学习网
      var enc_str = "";    JpBHTML5中文学习网 - HTML5先行者学习网
    for(var I=0; I<str.length; I+=2) {    JpBHTML5中文学习网 - HTML5先行者学习网
        enc_chr = parseInt(parseInt(str.substring(I, I+2), 16) ^ Math.floor((prand / modu) * 255));    JpBHTML5中文学习网 - HTML5先行者学习网
        enc_str += String.fromCharCode(enc_chr);    JpBHTML5中文学习网 - HTML5先行者学习网
        prand = (mult * prand + incr) % modu;    JpBHTML5中文学习网 - HTML5先行者学习网
    }    JpBHTML5中文学习网 - HTML5先行者学习网
    return unescape(enc_str);    JpBHTML5中文学习网 - HTML5先行者学习网
}    JpBHTML5中文学习网 - HTML5先行者学习网
//  End -->    JpBHTML5中文学习网 - HTML5先行者学习网
</script>   JpBHTML5中文学习网 - HTML5先行者学习网
JpBHTML5中文学习网 - HTML5先行者学习网

以后碰到加密解密问题,直接将上述代码写成一个js文件,就搞定。省事了。。。。JpBHTML5中文学习网 - HTML5先行者学习网

JpBHTML5中文学习网 - HTML5先行者学习网

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助