PHP code encryption class
- /*
- * @auther:wangyaofeng
- * @time:2014.11.6
- * @action: Encrypt the php project. Note that if there is a framework directory in the project or not Please move out the directories that need to be encrypted in advance
- * */
- class Encryption{
- private $c='';//Storage ciphertext
- private $s='',$q1,$q2,$q3,$q4,$ q5,$q6;//Storage the generated encrypted file content
- //If you do not set a value, isset will indicate that it does not exist;
- private $file='';//The path to read the file
- private $source= '',$target='';
- //Constructor, called to initialize global variables during instantiation;
- public function __construct(){
- //Initialize global variables
- $this->initialVar();
- //echo "hello n";
- }
- /*
- *@input $property_name,$value
- *@output
- * Magic method to set the value of variables; it can be processed according to needs. If the if judgment is directly removed, it means that the value of any attribute can be set, including attributes that do not exist;
- */
- public function __set($property_name,$value){
- //defined variables;
- if(isset($this- >$property_name)){
- $this->$property_name = $value;
- }else{
- //Exception handling, processing undeclared variable assignment; can be processed according to needs.
- throw new Exception("property does not exist");
- }
- }
- //Magic method to get the value of the variable;
- public function __get($property_name){
- if(isset($this->$property_name)) {
- return $this->$property_name;
- }else{
- //throw new Exception("property does not exist");
- return NULL;
- }
- }
- //Get random order
- private function RandAbc($ length=""){//Retrieve in random order
- $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- return str_shuffle($str);
- }
- //Encrypt the plain text content
- private function ciphertext($filename){
- / /$filename='index.php';
- $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;
- }
- //Initialization Variable
- private function initialVar(){
- $this->q1="O00O0O";//base64_decode
- $this->q2="O0O000";//$c (the original ciphertext after strtr replacement, by Target character + replacement character + base64_encode('original content'))
- $this->q3="O0OO00";//strtr
- $this->q4="OO0O00";//substr
- $this-> ;q5="OO0000";//52
- $this->q6="O00OO0";//urldecode parsed string (n1zb/ma5vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)
-
- }
- //Generate Encrypted template (complex version);
- private function model(){
- //$c = $this->c;
- //$this->initialVar();
- $this->s=' 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.'Xiaobei.$'.$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.'="'.$this->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;
- }
- //Create encrypted file
- private function build($target){
- //$this->encodes("./index. php");
- //$this->model();
- $fpp1 = fopen($target,'w');
- fwrite($fpp1,$this->s) or die('write is fail!');
- fclose($fpp1);
- return $this;
- }
- //Encryption processing coherent operation
- public function encode($file,$target){
- //$file = "index.php";
- / /The coherent operation is actually to use the function to return to itself after processing
- $this->ciphertext($file)->model()->build($target);
- echo 'encode------'. $target.'-----ok
';
- }
- //Decrypt
- public function decode($file,$target=''){
- //Read the file to be decrypted
- $fpp1 = file_get_contents($file);
- $this->decodeMode($fpp1)->build($target);
- echo 'decode------'.$target.'-----ok< br/>';
- }
- //Decrypt the template and get the decrypted text
- private function decodeMode($fpp1){
- //Use eval as the flag to intercept it into an array, the first half is the replaced function name in the ciphertext , the second half is the ciphertext
- $m = explode('eval',$fpp1);
- //Execute the replacement part of the system function and get the system variable
- $varStr = substr($m[0],strpos( $m[0],'$'));
- //After execution, the replaced system function name can be used later
- eval($varStr);
- //Determine whether there is ciphertext
- if(!isset($ m[1])){
- return $this;
- }
-
- //Intercept the ciphertext {$this->q4} substr
- $star = strripos($m[1],'(');
- $end = strpos($m[1],')');
- $str = ${$this->q4}($m[1],$star,$end);
- //Decrypt the ciphertext {$this->q1} base64_decode
- $str = ${$this->q1}($str);
- //Intercept the decrypted core ciphertext
- $evallen = strpos($str,'eval' );
- $str = substr($str,0,$evallen);
- //Execute the core ciphertext so that the system variable is assigned the value $O0O000
- eval($str);
- //The following paragraph cannot be encapsulated because ${$this->qn} does not work in the whole text
- $this->s = ${$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;
- }
- //Recursively read and create the target directory structure
- private function targetDir($target){
- if(!empty($target ) ) {
- if(!file_exists($target)){
- mkdir($target,0777,true);
- }else{
- chmod($target,0777);
- }
-
- }
- }
-
- //Recursion Decrypt Decrypt php files in the specified folder
- public function decodeDir($source,$target=""){
- if(is_dir($source)){
- $this->targetDir($target);
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
- //List all files and remove '.' and '..' The example used here is the thinkphp framework, Therefore, the Thinkphp directory is excluded by default. Users can set it according to their own needs
- if($file!='.' && $file!='..' && $file !='ThinkPHP')
- {
- $path = $target .DIRECTORY_SEPARATOR.$file;
- $sourcePath = $source.DIRECTORY_SEPARATOR.$file;
- $this->decodeDir($sourcePath,$path);
- }
- }
-
- }else if(is_file($source)){
- $extension=substr($source,strrpos($source,'.')+1);
- if(strtolower($extension)=='php'){
- $this->decode($source,$target );
- }else{
- //Files that are not PHP will not be processed
- copy($source, $target);
- }
- //return;
- }
- }
- //Recursive encryption encrypts php files in the specified folder
- public function encodeDir($source,$target){
- if(is_dir($source)){
- $this->targetDir($target);
- $ dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
- //List all files and remove '.' and '..'
- if($file!=' .' && $file!='..' && $file !='ThinkPHP')
- {
- $path = $target.DIRECTORY_SEPARATOR.$file;
- $sourcePath = $source.DIRECTORY_SEPARATOR.$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 = "/var/www/bookReservation";
- $ob->target = "/var/www/jiami/bookReservation";
- // Decrypt the specified file
- //$ob->decode('D:\php\WWW\workspace\weixin2\Application\Home\Controller\IndexController.class.php');
-
- //$ob->decode( 'jiami.php');
- //$ob->decode('dam6.php');
- //Encrypt a specified file directory
- $ob->encodeDir($ob->source, $ob->target);
- //Decrypt a specified file directory
- $ob->decodeDir($ob->target,"/var/www/jiami/bookReservationD");
Copy Code
|