码迷,mamicode.com
首页 > 编程语言 > 详细

一段有用的javascript加密解密

时间:2014-10-21 11:53:13      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   java   for   strong   sp   

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

 

[javascript] view plaincopy
 
  1. <SCRIPT LANGUAGE="JavaScript">    
  2.   
  3. <!-- Begin    
  4.   
  5.      
  6.   
  7. function Encrypt(str, pwd) {    
  8.   
  9.     if(str=="")return "";    
  10.   
  11.     str = escape(str);    
  12.   
  13.     if(!pwd || pwd==""){ var pwd="1234"; }    
  14.   
  15.     pwd = escape(pwd);    
  16.   
  17.       if(pwd == null || pwd.length <= 0) {    
  18.   
  19.         alert("Please enter a password with which to encrypt the message.");    
  20.   
  21.           return null;    
  22.   
  23.       }    
  24.   
  25.       var prand = "";    
  26.   
  27.       for(var I=0; I<pwd.length; I++) {    
  28.   
  29.         prand += pwd.charCodeAt(I).toString();    
  30.   
  31.       }    
  32.   
  33.       var sPos = Math.floor(prand.length / 5);    
  34.   
  35.       var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));    
  36.   
  37.      
  38.   
  39.       var incr = Math.ceil(pwd.length / 2);    
  40.   
  41.       var modu = Math.pow(2, 31) - 1;    
  42.   
  43.       if(mult < 2) {    
  44.   
  45.         alert("Algorithm cannot find a suitable hash. Please choose a different password. /nPossible considerations are to choose a more complex or longer password.");    
  46.   
  47.         return null;    
  48.   
  49.       }    
  50.   
  51.       var salt = Math.round(Math.random() * 1000000000) % 100000000;    
  52.   
  53.       prand += salt;    
  54.   
  55.       while(prand.length > 10) {    
  56.   
  57.         prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();    
  58.   
  59.       }    
  60.   
  61.       prand = (mult * prand + incr) % modu;    
  62.   
  63.     var enc_chr = "";    
  64.   
  65.     var enc_str = "";    
  66.   
  67.     for(var I=0; I<str.length; I++) {    
  68.   
  69.         enc_chr = parseInt(str.charCodeAt(I) ^ Math.floor((prand / modu) * 255));    
  70.   
  71.         if(enc_chr < 16) {    
  72.   
  73.             enc_str += "0" + enc_chr.toString(16);    
  74.   
  75.         }else    
  76.   
  77.             enc_str += enc_chr.toString(16);    
  78.   
  79.         prand = (mult * prand + incr) % modu;    
  80.   
  81.     }    
  82.   
  83.       salt = salt.toString(16);    
  84.   
  85.       while(salt.length < 8)salt = "0" + salt;    
  86.   
  87.     enc_str += salt;    
  88.   
  89.     return enc_str;    
  90.   
  91. }    
  92.   
  93.      
  94.   
  95. function Decrypt(str, pwd) {    
  96.   
  97.     if(str=="")return "";    
  98.   
  99.     if(!pwd || pwd==""){ var pwd="1234"; }    
  100.   
  101.     pwd = escape(pwd);    
  102.   
  103.       if(str == null || str.length < 8) {    
  104.   
  105.         alert("A salt value could not be extracted from the encrypted message because it‘s length is too short. The message cannot be decrypted.");    
  106.   
  107.         return;    
  108.   
  109.       }    
  110.   
  111.       if(pwd == null || pwd.length <= 0) {    
  112.   
  113.         alert("Please enter a password with which to decrypt the message.");    
  114.   
  115.         return;    
  116.   
  117.       }    
  118.   
  119.       var prand = "";    
  120.   
  121.       for(var I=0; I<pwd.length; I++) {    
  122.   
  123.         prand += pwd.charCodeAt(I).toString();    
  124.   
  125.       }    
  126.   
  127.       var sPos = Math.floor(prand.length / 5);    
  128.   
  129.       var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));    
  130.   
  131.       var incr = Math.round(pwd.length / 2);    
  132.   
  133.       var modu = Math.pow(2, 31) - 1;    
  134.   
  135.       var salt = parseInt(str.substring(str.length - 8, str.length), 16);    
  136.   
  137.       str = str.substring(0, str.length - 8);    
  138.   
  139.       prand += salt;    
  140.   
  141.       while(prand.length > 10) {    
  142.   
  143.         prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();    
  144.   
  145.       }    
  146.   
  147.       prand = (mult * prand + incr) % modu;    
  148.   
  149.       var enc_chr = "";    
  150.   
  151.       var enc_str = "";    
  152.   
  153.     for(var I=0; I<str.length; I+=2) {    
  154.   
  155.         enc_chr = parseInt(parseInt(str.substring(I, I+2), 16) ^ Math.floor((prand / modu) * 255));    
  156.   
  157.         enc_str += String.fromCharCode(enc_chr);    
  158.   
  159.         prand = (mult * prand + incr) % modu;    
  160.   
  161.     }    
  162.   
  163.     return unescape(enc_str);    
  164.   
  165. }    
  166.   
  167. //  End -->    
  168.   
  169. </script>   

 

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

一段有用的javascript加密解密

标签:blog   http   io   os   ar   java   for   strong   sp   

原文地址:http://www.cnblogs.com/xiaochao12345/p/4039822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!