Complete php code encryption and decryption instance based on php

Source: Internet
Author: User
This article mainly introduces the php code encryption and decryption class based on php, and analyzes the complete php code encryption and decryption process in the form of a complete example, for more information about how to use the encryption and decryption class, see the example in this article to describe the php code encryption and decryption class based on php. We will share this with you for your reference. The details are as follows:

Php code encryption class. you can modify it according to your needs. The original class is as follows. This instance is tested in ubuntu.

 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, the values of any attributes 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 {// handle exception handling and assign values to undeclared variables; it can be processed as needed. Throw new Exception ("property does not exist");} // Obtain the variable value using the magic method; 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 sorting retrieval $ 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 ); $ 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 (ciphertext after the original text is replaced by strtr, with 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 ='
 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 }. $ '. $ t His-> 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;} // create the 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;} // public function encode ($ file, $ target) for consistent encryption processing operations {// $ file = "index. php "; // The coherent operation is actually to use the function to return itself after processing $ this-> ciphertext ($ file)-> model ()-> build ($ target ); echo 'encode ------'. $ target. '----- OK
';} // Decrypt the 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) {// truncates the image as an array using the eval mark, the first half is the replaced function name in the ciphertext, and the second half is the ciphertext $ m = explode ('eval', $ fpp1); // the replacement part of the system function is executed, get the system variable $ varStr = substr ($ m [0], strpos ($ m [0], '$'); // after execution, later, you can use the replaced system function name eval ($ varStr); // you can determine whether there is a 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 ); // extract 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 sections cannot be encapsulated, because $ {$ this-> qn} does not play a role in the full text $ this-> s =$ {$ this-> q1} ($ {$ t His-> 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 );}}} // recursively decrypt the public function decodeDir ($ source, $ target = "") {if (is_dir ($ source) to decrypt the php file in 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. Therefore, the Thinkphp directory is excluded by default. you 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 a php file does not process copy ($ source, $ target) ;}// return ;}// recursive encryption public function encodeD is encrypted for the php file in the specified folder Ir ($ 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 ");

I hope this article will help you with PHP programming.

For more articles on php-based complete php code encryption and decryption instances, please follow the PHP Chinese network!

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.