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

字符串的加密解密在数据的处理中应该是经常使用的,但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).'
';


有机会在完善下,本函数实现的是简单的加密解密 ,
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools