Home > php教程 > php手册 > php使用异或实现的加密解密实例

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

WBOY
Release: 2016-06-06 20:27:45
Original
1133 people have browsed it

字符串的加密解密在数据的处理中应该是经常使用的,但MD5加密是不可逆的,所以自能自己动手写一个加密解密函数,思路很。网上看了几个用异或实现的思路,看懂后

若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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template