标签:
这两天突发奇想想要用php写一个对日常项目加密以及解密的功能,经过努力简单的封装了一个对php代码进行加密解密的类,一些思想也是来自于网络,初步测试用着还行,可以实现对指定项目的加密以及解密(只针对本类中加密的解密)过程,在这里分享给大家,水平比较有限那里有错误还希望指出,共同提高,一下会给大家列出来实现的思想以及源码。
加密过程:
读取源文件,base64_encode进行加密,利用混排得到的52个大小写字母作为秘钥进行替换
$c=strtr(密文,对应待替换的字母,要替换成的字母);
将两个秘钥和密文链接起来形成要加密文件的主要内容
最后根据提前写好的模板格式,分别对base64_decode,strtr,substr几个命令进行组合,并将组和好的密文放入模板并在此base64_encode加密,
写入要加密的文件。
解密过程:
读取要解密的文件,截取出以eval开头的字符串,之后层次截取得到加密模板中生成的密文
base64_decode解密得到解密后的明文
截取得到的名文,将源文件形成的 秘钥+密文那段的字符截取出来,通过eval执行截取的字符使密文赋值给预定义的变量($O0O000)
通过执行base64_decode(strtr(substr($O0O000,52*2),substr($O0O000,52,52),substr($O0O000,0,52)));
进行解密,将解密过的内容(明文)写入文件。
总结:其实整个加密解密的过程都是互逆的,加密后的php运行是通过eval()这个函数实现的,解密的过程就是加密的逆过程。
- <?php
- class Encryption{
- private $c=‘‘;
- private $s=‘‘,$q1,$q2,$q3,$q4,$q5,$q6;
-
- private $file=‘‘;
- private $source=‘‘,$target=‘‘;
-
- public function __construct(){
-
- $this->initialVar();
-
- }
-
- public function __set($property_name,$value){
-
- if(isset($this->$property_name)){
- $this->$property_name = $value;
- }else{
-
- throw new Exception("property does not exist");
- }
- }
-
- public function __get($property_name){
- if(isset($this->$property_name)){
- return $this->$property_name;
- }else{
-
- return NULL;
- }
- }
-
- private function RandAbc($length=""){
- $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- return str_shuffle($str);
- }
-
- private function ciphertext($filename){
-
- $T_k1=$this->RandAbc();
- $T_k2=$this->RandAbc();
- $vstr=file_get_contents($filename);
- $v1=base64_encode($vstr);
- $c=strtr($v1,$T_k1,$T_k2);
-
-
- $this->c=$T_k1.$T_k2.$c;
- return $this;
- }
-
- private function modelEasy(){
- $this->s=‘<?php
- ‘.‘$c=‘
- .‘"‘.$this->c.‘";‘."\n".
- ‘$m=‘
- .
- ‘strtr(substr($c,52*2),substr($c,52,52),substr($c,0,52));‘."\n"
- .
- ‘$s=base64_decode(‘
- .
- ‘$m‘
- .
- ‘);‘.
- ‘eval("‘.‘?>".$s‘.‘);‘.
- ‘
- ?>‘;
- return $this;
- }
-
- private function initialVar(){
- $this->q1="O00O0O";
- $this->q2="O0O000";
- $this->q3="O0OO00";
- $this->q4="OO0O00";
- $this->q5="OO0000";
- $this->q6="O00OO0";
- }
-
- private function model(){
- $c = $this->c;
-
- $this->s=‘<?php $‘.$this->q6.‘=urldecode("%6E1%7A%62%2F%6D%615%5C%76%740%6928%2D%70%78%75%71%79%2A6%6C%72%6B%64%679%5F%65%68%63%73%77%6F4%2B%6637%6A");$‘.
- $this->q1.‘=$‘.$this->q6.‘{3}.$‘.$this->q6.‘{6}.$‘.$this->q6.‘{33}.$‘.$this->q6.‘{30};$‘.$this->q3.‘=$‘.$this->q6.‘{33}.$‘.$this->q6.‘{10}.$‘
- .$this->q6.‘{24}.$‘.$this->q6.‘{10}.$‘.$this->q6.‘{24};$‘.$this->q4.‘=$‘.$this->q3.‘{0}.$‘.$this->q6.‘{18}.$‘.$this->q6.‘{3}.$‘.$this->q3.‘{0}
- .$‘.$this->q3.‘{1}.$‘.$this->q6.‘{24};$‘.$this->q5.‘=$‘.$this->q6.‘{7}.$‘.$this->q6.‘{13};$‘.$this->q1.‘.=$‘.$this->q6.‘{22}.$‘.$this->q6.‘{36}
- .$‘.$this->q6.‘{29}.$‘.$this->q6.‘{26}.$‘.$this->q6.‘{30}.$‘.$this->q6.‘{32}.$‘.$this->q6.‘{35}.$‘.$this->q6.‘{26}.$‘.$this->q6.‘{30};
- eval($‘.$this->q1.‘("‘.base64_encode(‘$‘.$this->q2.‘="‘.$c.‘";
- eval(\‘?>\‘.$‘.$this->q1.‘($‘.$this->q3.‘($‘.$this->q4.‘($‘.$this->q2.‘,$‘.$this->q5.‘*2),$‘.$this->q4.‘($‘.$this->q2.‘,$‘.$this->q5.‘,$‘.$this->q5.‘),
- $‘.$this->q4.‘($‘.$this->q2.‘,0,$‘.$this->q5.‘))));‘).‘"));?>‘;
- return $this;
- }
-
- private function build($file,$target){
-
-
- $fpp1 = fopen($target,‘w‘);
- fwrite($fpp1,$this->s) or die(‘写入是失败!‘);
- return $this;
- }
-
- public function encode($file,$target){
-
-
- $this->ciphertext($file)->model()->build($file,$target);
- echo ‘encode------‘.$target.‘-----ok<br/>‘;
- }
-
- public function decode($file,$target=‘‘){
-
-
-
-
- $fpp1 = file_get_contents($file);
-
- preg_match(‘[eval.*]‘, $fpp1,$m);
- if(!isset($m[0])){
- return;
- }
- $len = strlen($m[0]);
-
- $str = substr($m[0],strlen(‘eval($O00O0O(‘),$len-18);
-
- $str = base64_decode($str);
-
- $str = substr($str,0 ,strlen($str)-120);
-
- eval($str);
-
- $s = base64_decode(strtr(substr($O0O000,52*2),substr($O0O000,52,52),substr($O0O000,0,52)));
-
- $fpp1 = fopen($file,‘w‘);
- fwrite($fpp1,$s) or die(‘写入失败!‘);
- echo ‘decode------‘.$target.‘-----ok<br/>‘;
- }
-
- public function decodeDir($source,$target=""){
-
- if(is_dir($source)){
-
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
-
- if($file!=‘.‘ && $file!=‘..‘ && $file!=‘ThinkPHP‘)
- {
-
- $sourcePath = $source.‘\\‘.$file;
- $this->decodeDir($sourcePath);
- }
- }
-
- }else if(is_file($source)){
- $extension=substr($source,strrpos($source,‘.‘)+1);
- if(strtolower($extension)==‘php‘){
- $this->decode($source);
- }else{
-
-
- }
-
- }
- }
-
- public function encodeDir($source,$target){
-
- if(is_dir($source)){
- @mkdir($target);
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
-
- if($file!=‘.‘ && $file!=‘..‘ && $file!=‘ThinkPHP‘)
- {
- $path = $target.‘\\‘.$file;
- $sourcePath = $source.‘\\‘.$file;
- $this->encodeDir($sourcePath,$path);
- }
- }
-
- }else if(is_file($source)){
- $extension=substr($source,strrpos($source,‘.‘)+1);
- if(strtolower($extension)==‘php‘){
- $this->encode($source,$target);
- }else{
- copy($source, $target);
- }
-
- }
- }
-
- }
- $ob = new Encryption();
- $ob->source = "D:\php\WWW\workspace\weixin1";
- $ob->target = "D:\php\WWW\workspace\weixin2";
-
php加密解密功能类
标签:
原文地址:http://www.cnblogs.com/waw/p/5983022.html