Home > Backend Development > PHP Tutorial > php使用异或实现的加密解密实例_PHP

php使用异或实现的加密解密实例_PHP

WBOY
Release: 2016-06-01 12:03:47
Original
1072 people have browsed it

若a=b ^ c;则b=a ^ c (^是异或的意思),php在处理异或的字符时先把字符转化为二进制的ascii值,对这些值进行异或,获取结果后在将ascii值转化为字符,原理说晚了直接贴实现的代码:
复制代码 代码如下:
echo '';
  $str='世界,你好';  
  function jiami($str,$key){
   $key=md5($key);
   $k=md5(rand(0,100));//相当于动态密钥
   $k=substr($k,0,3);
   $tmp="";
   for($i=0;$i    $tmp.=substr($str,$i,1) ^ substr($key,$i,1);
   }
   return base64_encode($k.$tmp);
  }  
  function jiemi($str,$key){
   $len=strlen($str);
   $key=md5($key);
   $str=base64_decode($str);
   $str=substr($str,3,$len-3);
   $tmp="";
   for($i=0;$i    $tmp.=substr($str,$i,1) ^ substr($key,$i,1);
   }    
   return $tmp;
  }  
  $key='cc';
  $jh=jiami($str, $key);
  echo '加密前:'.$str.'
';
  echo '加密后:'.$jh.'
';
  echo '解密后:'.jiemi($jh, $key).'
';

有机会在完善下,本函数实现的是简单的加密解密

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template