javascript將url中的參數加密解密代碼,javascript加密解密

來源:互聯網
上載者:User

javascript將url中的參數加密解密代碼,javascript加密解密

今天在做一個老項目時,遇到一個需求,在javascript將url中的參數加密解密,從網上找發現了這段有用的代碼:

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

以後碰到加密解密問題,直接將上述代碼寫成一個js檔案,就搞定。省事了。。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.