首页 > 网站 > WEB开发 > 正文

一段有用的javascript加密解密

2024-04-27 14:17:47
字体:
来源:转载
供稿:网友

一段有用的javascript加密解密

今天在做一个老项目时,遇到一个需求,在Javascript将url中的参数加密解密,从网上找发现了这段有用的代码:

[javascript]view plaincopy
  1. <SCRIPTLANGUAGE="JavaScript">
  2. <!--Begin
  3. functionEncrypt(str,pwd){
  4. if(str=="")return"";
  5. str=escape(str);
  6. if(!pwd||pwd==""){varpwd="1234";}
  7. pwd=escape(pwd);
  8. if(pwd==null||pwd.length<=0){
  9. alert("PleaseenterapassWordwithwhichtoencryptthemessage.");
  10. returnnull;
  11. }
  12. varPRand="";
  13. for(varI=0;I<pwd.length;I++){
  14. prand+=pwd.charCodeAt(I).toString();
  15. }
  16. varsPos=Math.floor(prand.length/5);
  17. varmult=parseInt(prand.charAt(sPos)+prand.charAt(sPos*2)+prand.charAt(sPos*3)+prand.charAt(sPos*4)+prand.charAt(sPos*5));
  18. varincr=Math.ceil(pwd.length/2);
  19. varmodu=Math.pow(2,31)-1;
  20. if(mult<2){
  21. alert("Algorithmcannotfindasuitablehash.Pleasechooseadifferentpassword./nPossibleconsiderationsaretochooseamorecomplexorlongerpassword.");
  22. returnnull;
  23. }
  24. varsalt=Math.round(Math.random()*1000000000)%100000000;
  25. prand+=salt;
  26. while(prand.length>10){
  27. prand=(parseInt(prand.substring(0,10))+parseInt(prand.substring(10,prand.length))).toString();
  28. }
  29. prand=(mult*prand+incr)%modu;
  30. varenc_chr="";
  31. varenc_str="";
  32. for(varI=0;I<str.length;I++){
  33. enc_chr=parseInt(str.charCodeAt(I)^Math.floor((prand/modu)*255));
  34. if(enc_chr<16){
  35. enc_str+="0"+enc_chr.toString(16);
  36. }else
  37. enc_str+=enc_chr.toString(16);
  38. prand=(mult*prand+incr)%modu;
  39. }
  40. salt=salt.toString(16);
  41. while(salt.length<8)salt="0"+salt;
  42. enc_str+=salt;
  43. returnenc_str;
  44. }
  45. functionDecrypt(str,pwd){
  46. if(str=="")return"";
  47. if(!pwd||pwd==""){varpwd="1234";}
  48. pwd=escape(pwd);
  49. if(str==null||str.length<8){
  50. alert("Asaltvaluecouldnotbeextractedfromtheencryptedmessagebecauseit'slengthistooshort.Themessagecannotbedecrypted.");
  51. return;
  52. }
  53. if(pwd==null||pwd.length<=0){
  54. alert("Pleaseenterapasswordwithwhichtodecryptthemessage.");
  55. return;
  56. }
  57. varprand="";
  58. for(varI=0;I<pwd.length;I++){
  59. prand+=pwd.charCodeAt(I).toString();
  60. }
  61. varsPos=Math.floor(prand.length/5);
  62. varmult=parseInt(prand.charAt(sPos)+prand.charAt(sPos*2)+prand.charAt(sPos*3)+prand.charAt(sPos*4)+prand.charAt(sPos*5));
  63. varincr=Math.round(pwd.length/2);
  64. varmodu=Math.pow(2,31)-1;
  65. varsalt=parseInt(str.substring(str.length-8,str.length),16);
  66. str=str.substring(0,str.length-8);
  67. prand+=salt;
  68. while(prand.length>10){
  69. prand=(parseInt(prand.substring(0,10))+parseInt(prand.substring(10,prand.length))).toString();
  70. }
  71. prand=(mult*prand+incr)%modu;
  72. varenc_chr="";
  73. varenc_str="";
  74. for(varI=0;I<str.length;I+=2){
  75. enc_chr=parseInt(parseInt(str.substring(I,I+2),16)^Math.floor((prand/modu)*255));
  76. enc_str+=String.fromCharCode(enc_chr);
  77. prand=(mult*prand+incr)%modu;
  78. }
  79. returnunescape(enc_str);
  80. }
  81. //End-->
  82. </script>

以后碰到加密解密问题,直接将上述代码写成一个js文件,就搞定。省事了。。。。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表