PHP code based on the implementation of encryption and decryption class complete example _php skills

Source: Internet
Author: User
Tags decrypt eval php class php code php programming php regular expression

This article illustrates the PHP code encryption and decryption class based on PHP implementation. Share to everyone for your reference, specific as follows:

PHP code encryption class, you can change according to their needs, the original class such as, the example in Ubuntu under the Test no problem.

<?php 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 not set
    A value that the isset will indicate does not exist; private $file = ';//Read the path of the file private $source = ', $target = ';
      constructor that initializes the global variable when instantiated, Public function __construct () {//initializing 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 the requirements. If the direct removal of the If judgment indicates that the value of any property can be set, including the nonexistent attribute; */Public Function __set ($property _name, $value) {//defined variable; if (Isset ($t
      his-> $property _name)) {$this-> $property _name = $value;
        }else{//exception handling, which handles undeclared variable assignments, and can be processed according to requirements.
      throw new Exception ("Property does not exist"); }//Magical method to remove the value of the variable; public function __get ($property _name) {if (isset-> $property _name)) {R
      Eturn $this-> $property _name;
        }else{//throw New Exception ("Property does not exist");
      return NULL; }//Take a randomSort Private Function randabc ($length = "") {//random sort fetch $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Return Str_shuffle ($STR);
      ///Encrypt plaintext content 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= $T _k1. $T _k2. $c;
    return $this; }//Initialize variable private function Initialvar () {$this->q1= "o00o0o";//base64_decode $this->q2= "o0o000"; /$c (text after STRTR replacement, 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_eh
    CSWO4+F37J)}//Generate the encrypted template (complex version);
      Private 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. ' {n}. $ '. $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 the Encrypted file Private function build ($target) {//$this->encodes ("./index.php");
      $this->model ();
      $FPP 1 = fopen ($target, ' w '); Fwrite ($FPP 1, $this->s) or Die (' Write is failed!
      ');
      Fclose ($FPP 1);
    return $this;
      }//Encrypt process coherent operation public function encode ($file, $target) {//$file = "index.php";
      The coherent operation is actually the USE function processing to return oneself $this->ciphertext ($file)->model ()->build ($target);
    echo ' encode------'. $target. '-----ok<br/> ';
      //Decrypt Public function decode ($file, $target = ') {//Read the file to decrypt $FPP 1 = file_get_contents ($file);
      $this->decodemode ($FPP 1)->build ($target);
    Echo ' decode------'. $target. '-----ok<br/> '; //Decrypt template, get decrypted text private function Decodemode ($FPP 1) {//EvaL intercepts the array for the symbol, the first half is the replaced function name in the ciphertext, the latter part is ciphertext $m = explode (' eval ', $FPP 1);
      The replacement part of the system function is executed to get the system variable $VARSTR = substr ($m [0],strpos ($m [0], ' $ '));
      After execution, the replacement system function name eval ($VARSTR) can be used later;
      Determine if there is a redaction if (!isset ($m [1])) {return $this;
      //The redaction is intercepted {$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);
      Intercepts the decrypted core cipher $evallen = Strpos ($str, ' eval ');
      $str = substr ($str, 0, $evallen);
      The implementation of the core ciphertext enables the system variable to be assigned a value $O 0o000 eval ($str); The following paragraph cannot be encapsulated because the ${$this->qn} does not work in the full-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; ///recursive read and create target directory structure Private function TargetDir ($target) {if (!empty ($target)) {if (!file_exists) ($targe
        T)) {mkdir ($target, 0777,true);
        }else{chmod ($target, 0777);
        Decrypts the public function Decodedir ($source, $target = "") {if (Is_dir) {($source)) for the PHP file under the specified folder {
        $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, so the default exclude thinkphp directory, the user can set 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{//Not PHP file does not handle copy ($source, $target);
      }//return; }///Recursive encryption encrypts the PHP file in the specified folder public function Encodedir ($source, $target) {if (Is_dir ($source)) {$thi
        S->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 ');
Encrypts a specified file directory $ob->encodedir ($ob->source, $ob->target);

 Decrypts a specified file directory $ob->decodedir ($ob->target, "/var/www/jiami/bookreservationd");

PS: About encryption and decryption interested friends can also refer to the site online tools:

Password Security online detection:
Http://tools.jb51.net/password/my_password_safe

High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword

MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL encryption/decryption tool:
Http://tools.jb51.net/password/urlrethunder

Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

For more information on PHP related content readers can view the site topics: "PHP Encryption Method Summary", "PHP code and transcoding Operation Skills Summary", "PHP object-oriented Program Design Introductory Course", "PHP Mathematical Arithmetic Skills summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, PHP data structure and algorithm tutorial, PHP programming algorithm Summary, PHP Regular Expression usage summary, and PHP Common database operation Skills Summary

I hope this article will help you with your PHP programming.

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.