今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码: 复制代码 代码如下: <br /> <!-- Begin <br /> function Encrypt(str, pwd) { <br /> if(str=="")return ""; <br /> str = escape(str); <br /> if(!pwd || pwd==""){ var pwd="1234"; } <br /> pwd = escape(pwd); <br /> if(pwd == null || pwd.length <= 0) { <br /> alert("Please enter a password with which to encrypt the message."); <br /> return null; <br /> } <br /> var prand = ""; <br /> for(var I=0; I<pwd.length; I++) { <br /> prand += pwd.charCodeAt(I).toString(); <br /> } <br /> var sPos = Math.floor(prand.length / 5); <br /> var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); <br /> var incr = Math.ceil(pwd.length / 2); <br /> var modu = Math.pow(2, 31) - 1; <br /> if(mult < 2) { <br /> alert("Algorithm cannot find a suitable hash. Please choose a different password. /nPossible considerations are to choose a more complex or longer password."); <br /> return null; <br /> } <br /> var salt = Math.round(Math.random() * 1000000000) % 100000000; <br /> prand += salt; <br /> while(prand.length > 10) { <br /> prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString(); <br /> } <br /> prand = (mult * prand + incr) % modu; <br /> var enc_chr = ""; <br /> var enc_str = ""; <br /> for(var I=0; I<str.length; I++) { <br /> enc_chr = parseInt(str.charCodeAt(I) ^ Math.floor((prand / modu) * 255)); <br /> if(enc_chr < 16) { <br /> enc_str += "0" + enc_chr.toString(16); <br /> }else <br /> enc_str += enc_chr.toString(16); <br /> prand = (mult * prand + incr) % modu; <br /> } <br /> salt = salt.toString(16); <br /> while(salt.length < 8)salt = "0" + salt; <br /> enc_str += salt; <br /> return enc_str; <br /> } <br /> function Decrypt(str, pwd) { <br /> if(str=="")return ""; <br /> if(!pwd || pwd==""){ var pwd="1234"; } <br /> pwd = escape(pwd); <br /> if(str == null || str.length < 8) { <br /> alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted."); <br /> return; <br /> } <br /> if(pwd == null || pwd.length <= 0) { <br /> alert("Please enter a password with which to decrypt the message."); <br /> return; <br /> } <br /> var prand = ""; <br /> for(var I=0; I<pwd.length; I++) { <br /> prand += pwd.charCodeAt(I).toString(); <br /> } <br /> var sPos = Math.floor(prand.length / 5); <br /> var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); <br /> var incr = Math.round(pwd.length / 2); <br /> var modu = Math.pow(2, 31) - 1; <br /> var salt = parseInt(str.substring(str.length - 8, str.length), 16); <br /> str = str.substring(0, str.length - 8); <br /> prand += salt; <br /> while(prand.length > 10) { <br /> prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString(); <br /> } <br /> prand = (mult * prand + incr) % modu; <br /> var enc_chr = ""; <br /> var enc_str = ""; <br /> for(var I=0; I<str.length; I+=2) { <br /> enc_chr = parseInt(parseInt(str.substring(I, I+2), 16) ^ Math.floor((prand / modu) * 255)); <br /> enc_str += String.fromCharCode(enc_chr); <br /> prand = (mult * prand + incr) % modu; <br /> } <br /> return unescape(enc_str); <br /> } <br /> // End --> <br /> 以后碰到加密解密问题,直接将上述代码写成一个js文件,就搞定。省事了。。。。