Php-based complete php code encryption and decryption instance, php encryption and decryption

Source: Internet
Author: User

Php-based complete php code encryption and decryption instance, php encryption and decryption

This article describes 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.

<? Phpclass Encryption {private $ c = ''; // store ciphertext private $ s ='', $ q1, $ q2, $ q3, $ q4, $ q5, $ q6; // store the generated encrypted file content // if no value is set, isset indicates that the file does not exist; private $ file = ''; // The path to the file to be read: private $ source = '', $ target =''; // constructor. The global variable is called during instantiation. public function _ construct () {// initialize the global variable $ this-> initialVar (); // echo "hello \ n";}/** @ input $ property_name, $ value * @ output * Magic method to 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 = '<? Php $ '. $ this-> q6. '= urldecode ("% 6E1% 7A % 62% 2F % 6D % 615% 5C % 76% 740% 2D % 6928% 70% 78% 75% 2A6% 6C % 71% 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;} // 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 <br/>';} // decrypt the public function decode ($ file, $ target = '') {// read the file to be decrypted $ fpp1 = file_get_contents ($ file); $ this-> decodeMode ($ fpp1)-> build ($ target); echo 'decode ------'. $ target. '----- OK <B R/> ';} // decrypt the template to obtain the decrypted text private function decodeMode ($ fpp1) {// use eval as the marker to intercept it 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); // 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 ");

PS: if you are interested in encryption and decryption, refer to the online tools on this site:

Online Password Security 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

Thunder, express, and Tornado URL encryption/Decryption tools:
Http://tools.jb51.net/password/urlrethunder

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

Related Article

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.