Php code encryption
- /*
- * @ Auther: wangyaofeng
- * @ Time: 2014.11.6
- * @ Action: encrypt the php project. Note that if there is a framework directory in the project or there is no need to encrypt the Directory, remove it in advance.
- **/
- Class Encryption {
- Private $ c = ''; // stores ciphertext
- Private $ s = '', $ q1, $ q2, $ q3, $ q4, $ q5, $ q6; // stores the encrypted file content.
- // If no value is set, isset indicates that the value does not exist;
- Private $ file = ''; // path of the file to be read
- Private $ source = '', $ target = '';
- // Constructor, which calls global variable initialization during instantiation;
- Public function _ construct (){
- // Initialize global variables
- $ This-> initialVar ();
- // Echo "hello \ n ";
- }
- /*
- * @ Input $ property_name, $ value
- * @ Output
- * Magic method: set the value of the variable. it can be processed as needed. If the if judgment is directly removed, it indicates that any attribute values 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. handle the value assignment of undeclared variables. you can handle the exception as needed.
- Throw new Exception ("property does not exist ");
- }
- }
- // Use the magic method to retrieve the variable value;
- Public function _ get ($ property_name ){
- If (isset ($ this-> $ property_name )){
- Return $ this-> $ property_name;
- } Else {
- // Throw new Exception ("property does not exist ");
- Return NULL;
- }
- }
- // Random sorting
- Private function RandAbc ($ length = "") {// random sort retrieval
- $ Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
- Return str_shuffle ($ str );
- }
- // Encrypt the plaintext 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;
- }
- // Initialize the variable
- Private function initialVar (){
- $ This-> q1 = "O00O0O"; // base64_decode
- $ This-> q2 = "O0O000"; // $ c (ciphertext after the original text is replaced by strtr, which consists of 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 resolved 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 =' Q6. '= urldecode ("% 6E1% 7A % 62% 2F % 6D % 615% 5C % 76% 740% 2D % 6928% 70% 78% 75% 2A6% 6C % 71% 6B % 79% 72% 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. Bay. $ '. $ 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 an encrypted file
- Private function build ($ target ){
- // $ This-> encodes ("./index. php ");
- // $ This-> model ();
- $ Fpp1 = fopen ($ target, 'w ');
- Fwrite ($ fpp1, $ this-> s) or die ('write is failed! ');
- Fclose ($ fpp1 );
- Return $ this;
- }
- // Encrypt and process consistent operations
- Public function encode ($ file, $ target ){
- // $ File = "index. php ";
- // A coherent operation is actually to use the function to return 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 'code ------ '. $ target.' ----- OK
';
- }
- // Decrypt the template to obtain the decrypted text
- Private function decodeMode ($ fpp1 ){
- // Uses eval as the marker to intercept as an array. the first half is the replaced function name in the ciphertext, and the second half is the ciphertext.
- $ M = explode ('eval', $ fpp1 );
- // Execute the replacement part of the system function to obtain the system variable
- $ VarStr = substr ($ m [0], strpos ($ m [0], '$ '));
- // After execution, you can use the replaced system function name.
- Eval ($ varStr );
- // Determine whether the ciphertext exists
- 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 );
- // Extract the decrypted core ciphertext
- $ Evallen = strpos ($ str, 'eval ');
- $ Str = substr ($ str, 0, $ evallen );
- // Execute the core ciphertext to assign the system variable value $ O0O000
- Eval ($ str );
- // The following sections cannot be encapsulated, because $ {$ this-> qn} does not play a role 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;
- }
- // 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 );
- }
-
- }
- }
-
- // Recursive decryption decrypts the php file 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 the files and remove '.' and '.'. the instance used here is the thinkphp framework. Therefore, the Thinkphp directory is excluded by default. you can set it as needed.
- 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 {
- // Php files are not processed
- Copy ($ source, $ target );
- }
- // Return;
- }
- }
- // Recursive encryption encrypts the php file 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 objects 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 ");
|