网站地图    收藏   

主页 > php专栏 > php函数大全 >

PHP加密由javascript解密的例子 - php函数

来源:自学PHP网    时间:2014-11-25 00:26 作者: 阅读:次

[导读] 一般情况我们都是php加密再由php解密了,但是我的工作就碰到了必须由php加密然后由js来解密了,这种情况我找到一站长写的例子,非常的优秀下面我们一起来看看.PHP加密函数,代码如下:?p...

PHP加密由javascript解密的例子

一般情况我们都是php加密再由php解密了,但是我的工作就碰到了必须由php加密然后由js来解密了,这种情况我找到一站长写的例子,非常的优秀下面我们一起来看看.

PHP加密函数,代码如下:

  1. <?php 
  2.  
  3. function strencode($string) { 
  4.     $string = base64_encode($string); 
  5.     $key = md5('just a test'); 
  6.     $len = strlen($key); 
  7.     $code = ''; 
  8.     for ($i = 0; $i < strlen($string); $i++) { 
  9.         $k = $i % $len; 
  10.         $code .= $string [$i] ^ $key [$k]; 
  11.     } 
  12.     return base64_encode($code); 
  13. } 
  14. //开源代码phpfensi.com 
  15. echo strencode('just a test'); 
  16. ?> 

JS:解密,代码如下:

  1. <script src="md5.js"></script> 
  2. <script src="base64.js"></script> 
  3. <script> 
  4.  
  5.     function strencode(string) { 
  6.         key =md5('just a test'); 
  7.         string = Base64.decode(string);  
  8.         len = key.length;    
  9.         code = '';    
  10.         for (i = 0; i < string.length; i++) {    
  11.             k = i % len;    
  12.             code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k));    
  13.         }    
  14.         return Base64.decode(code);    
  15.     } 
  16.     alert(strencode('U1s1TFN3IQ0reTZbBgJlCA===='));    
  17. </script>  

js MD5,代码如下:

  1. /* 
  2.  * Configurable variables. You may need to tweak these to be compatible with 
  3.  * the server-side, but the defaults work in most cases. 
  4.  */ 
  5. var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */ 
  6. var b64pad  = "";  /* base-64 pad character. "=" for strict RFC compliance   */ 
  7.  
  8. /* 
  9.  * These are the functions you'll usually want to call 
  10.  * They take string arguments and return either hex or base-64 encoded strings 
  11.  */ 
  12. function md5(s)    { 
  13.     return rstr2hex(rstr_md5(str2rstr_utf8(s))); 
  14. } 
  15. function b64_md5(s)    { 
  16.     return rstr2b64(rstr_md5(str2rstr_utf8(s))); 
  17. } 
  18. function any_md5(s, e) { 
  19.     return rstr2any(rstr_md5(str2rstr_utf8(s)), e); 
  20. } 
  21. function hex_hmac_md5(k, d) 
  22. { 
  23.     return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); 
  24. } 
  25. function b64_hmac_md5(k, d) 
  26. { 
  27.     return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); 
  28. } 
  29. function any_hmac_md5(k, d, e) 
  30. { 
  31.     return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); 
  32. } 
  33.  
  34. /* 
  35.  * Perform a simple self-test to see if the VM is working 
  36.  */ 
  37. function md5_vm_test() 
  38. { 
  39.     return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72"; 
  40. } 
  41.  
  42. /* 
  43.  * Calculate the MD5 of a raw string 
  44.  */ 
  45. function rstr_md5(s) 
  46. { 
  47.     return binl2rstr(binl_md5(rstr2binl(s), s.length * 8)); 
  48. } 
  49.  
  50. /* 
  51.  * Calculate the HMAC-MD5, of a key and some data (raw strings) 
  52.  */ 
  53. function rstr_hmac_md5(key, data) 
  54. { 
  55.     var bkey = rstr2binl(key); 
  56.     if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8); 
  57.  
  58.     var ipad = Array(16), opad = Array(16); 
  59.     for(var i = 0; i < 16; i++) 
  60.     { 
  61.         ipad[i] = bkey[i] ^ 0x36363636; 
  62.         opad[i] = bkey[i] ^ 0x5C5C5C5C; 
  63.     } 
  64.  
  65.     var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8); 
  66.     return binl2rstr(binl_md5(opad.concat(hash), 512 + 128)); 
  67. } 
  68.  
  69. /* 
  70.  * Convert a raw string to a hex string 
  71.  */ 
  72. function rstr2hex(input) 
  73. { 
  74.     try { 
  75.         hexcase 
  76.     } catch(e) { 
  77.         hexcase=0; 
  78.     } 
  79.     var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 
  80.     var output = ""; 
  81.     var x; 
  82.     for(var i = 0; i < input.length; i++) 
  83.     { 
  84.         x = input.charCodeAt(i); 
  85.         output += hex_tab.charAt((x >>> 4) & 0x0F) 
  86.         +  hex_tab.charAt( x        & 0x0F); 
  87.     } 
  88.     return output; 
  89. } 
  90.  
  91. /* 
  92.  * Convert a raw string to a base-64 string 
  93.  */ 
  94. function rstr2b64(input) 
  95. { 
  96.     try { 
  97.         b64pad 
  98.     } catch(e) { 
  99.         b64pad=''; 
  100.     } 
  101.     var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
  102.     var output = ""; 
  103.     var len = input.length; 
  104.     for(var i = 0; i < len; i += 3) 
  105.     { 
  106.         var triplet = (input.charCodeAt(i) << 16) 
  107.         | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0) 
  108.         | (i + 2 < len ? input.charCodeAt(i+2)      : 0); 
  109.         for(var j = 0; j < 4; j++) 
  110.         { 
  111.             if(i * 8 + j * 6 > input.length * 8) output += b64pad; 
  112.             else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F); 
  113.         } 
  114.     } 
  115.     return output; 
  116. } 
  117.  
  118. /* 
  119.  * Convert a raw string to an arbitrary string encoding 
  120.  */ 
  121. function rstr2any(input, encoding) 
  122. { 
  123.     var divisor = encoding.length; 
  124.     var i, j, q, x, quotient; 
  125.  
  126.     /* Convert to an array of 16-bit big-endian values, forming the dividend */ 
  127.     var dividend = Array(Math.ceil(input.length / 2)); 
  128.     for(i = 0; i < dividend.length; i++) 
  129.     { 
  130.         dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1); 
  131.     } 
  132.  
  133.     /* 
  134.    * Repeatedly perform a long division. The binary array forms the dividend, 
  135.    * the length of the encoding is the divisor. Once computed, the quotient 
  136.    * forms the dividend for the next step. All remainders are stored for later 
  137.    * use. 
  138.    */ 
  139.     var full_length = Math.ceil(input.length * 8 / 
  140.         (Math.log(encoding.length) / Math.log(2))); 
  141.     var remainders =&nb

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论