PHP Cryptographic decryption Function class

Source: Internet
Author: User
Tags decrypt

These two days of whim want to use PHP to write a daily project encryption and decryption function, after an effort to simply encapsulate a PHP code encryption and decryption of the class, some ideas are also from the network, the initial test is OK, you can implement the encryption and decryption of the specified items (only for encryption in this class decryption) process , here to share to everyone, the level is relatively limited there are errors also want to point out, together to improve, will give you a list to realize the idea and source code.

Encryption process:
Read the source file, Base64_encode to encrypt, replace with 52 uppercase and lowercase letters in the mixed row as secret keys
$c =strtr (ciphertext, corresponding to the letter to be replaced, the letter to be replaced);
Link two keys and ciphertext to form the main content of the file to be encrypted
Finally, according to the pre-written template format, base64_decode,strtr,substr several commands are combined, and the group and good ciphertext into the template and this base64_encode encryption,
Write the file you want to encrypt.


Decryption process:
Reads the file to be decrypted, intercepts the string that begins with eval, and then intercepts the ciphertext generated in the cryptographic template.
Base64_decode decryption to get decrypted plaintext
Intercept the resulting name, the source file form the secret key + ciphertext that segment of the character intercepted, through the eval to execute the intercepted character to assign the ciphertext to the predefined variables ($O 0o000)
by executing Base64_decode (STRTR (substr ($O 0o000,52*2), substr ($O 0o000,52,52), substr ($O 0o000,0,52));
To decrypt and write the decrypted content (plaintext) to the file.


Summary: In fact, the whole process of encryption and decryption is reciprocal, the encrypted PHP run through the function of eval (), the decryption process is the inverse process of encryption.

<?php/* * @auther: Wangyaofeng * @time: 2014.11.6 * @action: Encrypt the PHP project * @Example: * * $ob = new encryption (); * $ob->source = "D:\php\WWW\workspace\weixin1"; * $ob->target = "D:\php\WWW\workspace\weixin2"; * $ob->decode (' d:\\php\\www\\workspace\\weixin2\\application\\home\\controller\\indexcontroller.class.php '); * $ob->decode (' jiami.php '); * $ob->decode (' dam6.php '); * $ob->encodedir ($ob->source, $ob->target); * $ob->decodedir ($ob->target); * * */class encryption{private $c = ';//store ciphertext private $s = ', $q 1, $q 2, $q 3, $q 4, $q 5, $q 6;//store generated encrypted file content//If you do not set a value, Isset will Private $file = ';//the path of the file is read private $source = ', $target = ';//constructor, initialize global variable is called when instantiating; public function __construct () {/ /Initialize global variable $this->initialvar ();//echo "Hello \ n";} /** @input $property _name, $value * @output * Magic method, set the value of the variable, can be processed according to requirements. Direct out if judgment indicates the value of any property that can be set, including non-existent attributes, */public function __set ($property _name, $value) {//defined variables; if (Isset ($this->$ Property_name) {$this, $property _name = $value;} else{//exception handling,Handles undeclared variable assignments and can be processed on demand. throw new Exception ("Property does not exist");}} The Magic method takes out 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;}}       Take random sort Private function randabc ($length = "") {//random sort fetch $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";    Return Str_shuffle ($STR);        }//encryption handles Private function ciphertext ($filename) {//$filename = ' index.php ';        $T _k1= $this->randabc ();        $T _k2= $this->randabc ();        $vstr =file_get_contents ($filename);        $v 1=base64_encode ($VSTR);        $c =strtr ($v 1, $T _k1, $T _k2);        $this-$c Error Cannot access empty property//correct wording $this->c;        $this->c= $T _k1. $T _k2. $c;    return $this;    }//Generate the encrypted template (simple version); 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; }//initialization Variable Private function Initialvar () {$this->q1= "o00o0o";//base64_decode$this->q2= "o0o000";//$c ( Text after STRTR replacement of ciphertext, by the 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/ma5\vt0i28-pxuqy*6%6crkdg9_ehcswo4+ F37J)}//generate the encrypted template (complex version);p rivate function model () {$c = $this->c;//$this->initialvar (); $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. ' * *), $ '. $this->q4. ' ($ '. $this->q2. ', $ '. $this->q5. ', $ '. $this->q5. '), $ '. $this->q4. ' ($ '. $this->q2. ', 0,$ '. $this->q5. '))))); > '; return $this;}        Create an encrypted file Private function build ($file, $target) {//$this->encodes ("./index.php");        $this->model ();        $FPP 1 = fopen ($target, ' w '); Fwrite ($FPP 1, $this->s) or Die (' Write is failed!       '); return $this;        }//cryptographic processing of the public function encode ($file, $target) {//$file = "index.php";        Coherent operation is actually the use of functions to return to their own $this->ciphertext ($file)->model ()->build ($file, $target);    echo ' encode------'. $target. '-----ok<br/> '; }//Decrypt Public function decode ($file, $target = ") {//import file (class name should not be the same as the class name of the imported file)//$file = ($target = =")? $file        : $target;                /*require_once ("$file"); The parsed value written in the file (the decryption of the simple version is directly $s)//fwrite ($FPP 1, $s) or die (' Write failed! ');//Initialize System constants//$this->initialvar ();//if (${$this->q2}) {$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})));} else{$s = file_get_contents ($file);} Open File $fpp1 = fopen ($file, ' w ');//write File fwrite ($FPP 1, $s) or die (' Write failed!    *////Read the file to be decrypted $FPP 1 = file_get_contents ($file);    Intercept part Preg_match (' [eval.*] ', $FPP 1, $m) beginning with eval;if (!isset ($m [0])) {return;    } $len = strlen ($m [0]);    Intercepts the cryptographic characters in the middle of the string beginning with eval $str = substr ($m [0],strlen (' eval ($O 00o0o ('), $len-18);    Decrypt the ciphertext $str = Base64_decode ($STR);//intercept the decrypted core cipher $str = substr ($str, 0, strlen ($STR)-120);//execute Core cipher eval ($STR);//Decrypt    $s = Base64_decode (Strtr (substr ($O 0o000,52*2), substr ($O 0o000,52,52), substr ($O 0o000,0,52));    Write file $FPP 1 = fopen ($file, ' w '); Fwrite ($FPP 1, $s) or die (' Write failed!    ');    Echo ' decode------'. $target. '-----ok<br/> ';    }//Recursively decrypt the PHP file under the specified folder to decrypt the public function Decodedir ($source, $target = "") {//$target = $source;    if (Is_dir ($source)) {//mkdir ($target);    $dir = Opendir ($source); while (false!= $file =readdir ($dir)) {//list all files and remove '. ' and ' ... '     Thinkphp represents the thinkphp framework in the project directory by default, without recursive encryption, and can modify if ($file! = '. ' && $file! = ': ' && $file! = ' thinkphp ') as required $path = $target. '    \ \ '. $file; $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{//Not PHP files are not processed//copy ($source, $target);    }//return; }}///recursive encryption to PHP files under the specified folder public function Encodedir ($source, $target) {if (Is_dir ($source)) {@mkdir ($t    Arget);    $dir = Opendir ($source); while (false!= $file =readdir ($dir)) {//list all files and remove '. '    and ' ... ' 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);    }//return; }}} $ob = new encryption (); $ob->source = "D:\php\WWW\workspace\weixin1"; $ob->target = "D:\php\WWW\workspace\w Eixin2 ";//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);// Decrypts a specified file directory//$ob->decodedir ($ob->target);



PHP Cryptographic decryption Function class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.