PHP服务器端加密,javascript客户端解密

来源:互联网 发布:休闲网游 知乎 编辑:程序博客网 时间:2024/04/27 15:04

有时需要对客户端的输出的一些内容加密,但在页面脚本中又需要用到这些内容,找了些代码,测试可以。但不支持中文。一般简单加密还是OK的。





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>


 <body>
<script type="text/javascript">
<!--
function strencode(string) {   
        key = '123456';   
        string = base64_decode(string);   
        len = key.length;   
  
        code = '';   
  
        for (i = 0; i < string.length; i++) {   
  
            k = i % len;   
  
            code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k));   
  
        }   
  
        return base64_decode(code);   
  
    }   
  
function base64_decode (data) {


  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
    ac = 0,
    dec = "",
    tmp_arr = [];


  if (!data) {
    return data;
  }


  data += '';


  do { // unpack four hexets into three octets using index points in b64
    h1 = b64.indexOf(data.charAt(i++));
    h2 = b64.indexOf(data.charAt(i++));
    h3 = b64.indexOf(data.charAt(i++));
    h4 = b64.indexOf(data.charAt(i++));


    bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;


    o1 = bits >> 16 & 0xff;
    o2 = bits >> 8 & 0xff;
    o3 = bits & 0xff;


    if (h3 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1);
    } else if (h4 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1, o2);
    } else {
      tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
    }
  } while (i < data.length);


  dec = tmp_arr.join('');


  return dec;
}


//-->
</script>
  


<?php

function strencode($string) {   
    $string = base64_encode ( $string );   
       
    $key = '123456';   
       
    $len = strlen ( $key );   
       
    $code = '';   
       
    for($i = 0; $i < strlen ( $string ); $i ++) {   
           
        $k = $i % $len;   
           
        $code .= $string [$i] ^ $key [$k];   
       
    }   
       
    return base64_encode ( $code );   
  
}   

?>
<input type="button" value="Click me" onclick="javascript:alert(strencode('<?php echo strencode("http://www.ssejej.com/aASaa.php?333") ?>'));" >


 </body>
</html>
原创粉丝点击