PHP code to implement encryption and decryption class sharing

Source: Internet
Author: User
Tags decrypt eval exception handling php file php code

PHP code Encryption class, we can according to their own needs to modify the original category, such as the next, hope to share to everyone. This is no problem with Ubuntu testing.

<?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 contents
If you do not set a value, Isset will indicate that it does not exist;
Private $file = ';//Read the path to the file
Private $source = ', $target = ';
constructor, which invokes the initialization of global variables when instantiated;
Public Function __construct () {
Initializing global variables
$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 any property can be set to a value, including a nonexistent attribute;
*/
Public Function __set ($property _name, $value) {
defined variables;
if (Isset ($this-> $property _name)) {
$this-> $property _name = $value;
}else{
Exception handling, which handles undeclared variable assignment, and can be processed according to requirements.
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 a random sort
Private Function randabc ($length = "") {//random sort Fetch
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
Return Str_shuffle ($STR);
}
Encrypt the contents of clear text
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;
}
Initializing variables
Private Function Initialvar () {
$this->q1= "o00o0o";//base64_decode
$this->q2= "o0o000";//$c (text after the STRTR replacement of the ciphertext, by the target character + replacement character +base64_encode (' original content ') composition)
$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);
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. ' {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;
}
Creating Encrypted files
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;
}
Encrypting and processing a coherent operation
Public function encode ($file, $target) {
$file = "index.php";
A coherent operation is actually a function to return to itself after processing
$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 the template and get the decrypted text
Private Function Decodemode ($FPP 1) {
Intercepts an array with an Eval flag, the first half being the substituted function name in the ciphertext, and the second part is ciphertext
$m = explode (' eval ', $FPP 1);
Execute the replacement part of the system function to get the system variable
$VARSTR = substr ($m [0],strpos ($m [0], ' $ '));
After execution, the replacement system function name can be used later
eval ($VARSTR);
To determine if there are 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);
Intercepts the core cipher after decryption
$evallen = Strpos ($str, ' eval ');
$str = substr ($str, 0, $evallen);
Execute the core ciphertext to give the system variable a value $O 0o000
eval ($STR);
The following paragraph cannot be encapsulated because ${$this-&GT;QN} does not work in 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;
}
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);
}

}
}

Decrypt the PHP file in the specified folder recursively
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, so the default exclude thinkphp directory, users can set according to their own requirements
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 not processed
Copy ($source, $target);
}
Return
}
}
Encrypt the PHP file in the specified folder with recursive encryption
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);
Decrypts a specified file directory
$ob->decodedir ($ob->target, "/var/www/jiami/bookreservationd");

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.