Encryption and decryption of php code

Source: Internet
Author: User
Encryption and decryption of php code
Php code encryption

  1. /*
  2. * @ Auther: wangyaofeng
  3. * @ Time: 2014.11.6
  4. * @ 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.
  5. **/
  6. Class Encryption {
  7. Private $ c = ''; // stores ciphertext
  8. Private $ s = '', $ q1, $ q2, $ q3, $ q4, $ q5, $ q6; // stores the encrypted file content.
  9. // If no value is set, isset indicates that the value does not exist;
  10. Private $ file = ''; // path of the file to be read
  11. Private $ source = '', $ target = '';
  12. // Constructor, which calls global variable initialization during instantiation;
  13. Public function _ construct (){
  14. // Initialize global variables
  15. $ This-> initialVar ();
  16. // Echo "hello \ n ";
  17. }
  18. /*
  19. * @ Input $ property_name, $ value
  20. * @ Output
  21. * 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;
  22. */
  23. Public function _ set ($ property_name, $ value ){
  24. // Defined variables;
  25. If (isset ($ this-> $ property_name )){
  26. $ This-> $ property_name = $ value;
  27. } Else {
  28. // Exception handling. handle the value assignment of undeclared variables. you can handle the exception as needed.
  29. Throw new Exception ("property does not exist ");
  30. }
  31. }
  32. // Use the magic method to retrieve the variable value;
  33. Public function _ get ($ property_name ){
  34. If (isset ($ this-> $ property_name )){
  35. Return $ this-> $ property_name;
  36. } Else {
  37. // Throw new Exception ("property does not exist ");
  38. Return NULL;
  39. }
  40. }
  41. // Random sorting
  42. Private function RandAbc ($ length = "") {// random sort retrieval
  43. $ Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
  44. Return str_shuffle ($ str );
  45. }
  46. // Encrypt the plaintext content
  47. Private function ciphertext ($ filename ){
  48. // $ Filename = 'index. php ';
  49. $ T_k1 = $ this-> RandAbc ();
  50. $ T_k2 = $ this-> RandAbc ();
  51. $ Vstr = file_get_contents ($ filename );
  52. $ V1 = base64_encode ($ vstr );
  53. $ C = strtr ($ v1, $ T_k1, $ T_k2 );
  54. $ This-> c = $ T_k1. $ T_k2. $ c;
  55. Return $ this;
  56. }
  57. // Initialize the variable
  58. Private function initialVar (){
  59. $ This-> q1 = "O00O0O"; // base64_decode
  60. $ 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)
  61. $ This-> q3 = "O0OO00"; // strtr
  62. $ This-> q4 = "OO0O00"; // substr
  63. $ This-> q5 = "OO0000"; // 52
  64. $ This-> q6 = "O00OO0"; // urldecode resolved string (n1zb/ma5 \ vt0i28-pxuqy * 6% 6Crkdg9_ehcswo4 + f37j)
  65. }
  66. // Generate the encrypted template (complex version );
  67. Private function model (){
  68. // $ C = $ this-> c;
  69. // $ This-> initialVar ();
  70. $ 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 "); $ '.
  71. $ This-> q1. '= $ '. $ this-> q6. '{3 }. $ '. $ this-> q6. '{6 }. $ '. $ this-> q6. '{33 }. $ '. $ this-> q6. '{30}; $ '. $ this-> q3. '= $ '. $ this-> q6. '{33 }. $ '. $ this-> q6. '{10 }. $'
  72. . $ This-> q6. '{24 }. $ '. $ this-> q6. '{10 }. $ '. $ this-> q6. '{24}; $ '. $ this-> q4. '= $ '. $ this-> q3. '{0 }. $ '. $ this-> q6. '{18 }. $ '. $ this-> q6. '{3 }. $ '. $ this-> q3. '{0}
  73. . $ '. $ This-> q3. Bay. $ '. $ this-> q6. '{24}; $ '. $ this-> q5. '= $ '. $ this-> q6. '{7 }. $ '. $ this-> q6. '{13}; $ '. $ this-> q1 .'. = $ '. $ this-> q6. '{22 }. $ '. $ this-> q6. '{36}
  74. . $ '. $ This-> q6. '{29 }. $ '. $ this-> q6. '{26 }. $ '. $ this-> q6. '{30 }. $ '. $ this-> q6. '{32 }. $ '. $ this-> q6. '{35 }. $ '. $ this-> q6. '{26 }. $ '. $ this-> q6. '{30 };
  75. Eval ($ '. $ this-> q1.' ("'. base64_encode (' $ '. $ this-> q2.' =" '. $ this-> c .'";
  76. Eval (\ '?> \'. $ '. $ This-> q1. '($ '. $ this-> q3. '($ '. $ this-> q4. '($ '. $ this-> q2. ', $ '. $ this-> q5. '* 2), $ '. $ this-> q4. '($ '. $ this-> q2. ', $ '. $ this-> q5. ', $ '. $ this-> q5 .'),
  77. $ '. $ This-> q4. '($ '. $ this-> q2. ', 0, $ '. $ this-> q5 .'))));'). '");?> ';
  78. Return $ this;
  79. }
  80. // Create an encrypted file
  81. Private function build ($ target ){
  82. // $ This-> encodes ("./index. php ");
  83. // $ This-> model ();
  84. $ Fpp1 = fopen ($ target, 'w ');
  85. Fwrite ($ fpp1, $ this-> s) or die ('write is failed! ');
  86. Fclose ($ fpp1 );
  87. Return $ this;
  88. }
  89. // Encrypt and process consistent operations
  90. Public function encode ($ file, $ target ){
  91. // $ File = "index. php ";
  92. // A coherent operation is actually to use the function to return itself after processing.
  93. $ This-> ciphertext ($ file)-> model ()-> build ($ target );
  94. Echo 'encode ------ '. $ target.' ----- OK
    ';
  95. }
  96. // Decrypt
  97. Public function decode ($ file, $ target = ''){
  98. // Read the file to be decrypted
  99. $ Fpp1 = file_get_contents ($ file );
  100. $ This-> decodeMode ($ fpp1)-> build ($ target );
  101. Echo 'code ------ '. $ target.' ----- OK
    ';
  102. }
  103. // Decrypt the template to obtain the decrypted text
  104. Private function decodeMode ($ fpp1 ){
  105. // 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.
  106. $ M = explode ('eval', $ fpp1 );
  107. // Execute the replacement part of the system function to obtain the system variable
  108. $ VarStr = substr ($ m [0], strpos ($ m [0], '$ '));
  109. // After execution, you can use the replaced system function name.
  110. Eval ($ varStr );
  111. // Determine whether the ciphertext exists
  112. If (! Isset ($ m [1]) {
  113. Return $ this;
  114. }
  115. // Intercept the ciphertext {$ this-> q4} substr
  116. $ Star = strripos ($ m [1], '(');
  117. $ End = strpos ($ m [1], ');
  118. $ Str =$ {$ this-> q4} ($ m [1], $ star, $ end );
  119. // Decrypt the ciphertext {$ this-> q1} base64_decode
  120. $ Str =$ {$ this-> q1} ($ str );
  121. // Extract the decrypted core ciphertext
  122. $ Evallen = strpos ($ str, 'eval ');
  123. $ Str = substr ($ str, 0, $ evallen );
  124. // Execute the core ciphertext to assign the system variable value $ O0O000
  125. Eval ($ str );
  126. // The following sections cannot be encapsulated, because $ {$ this-> qn} does not play a role in the full text.
  127. $ This-> s =$ {$ this-> q1 }(
  128. $ {$ This-> q3 }(
  129. $ {$ This-> q4 }(
  130. $ {$ This-> q2}, $ {$ this-> q5} * 2
  131. ),
  132. $ {$ This-> q4 }(
  133. $ {$ This-> q2}, $ {$ this-> q5}, $ {$ this-> q5}
  134. ),
  135. $ {$ This-> q4 }(
  136. $ {$ This-> q2}, 0, $ {$ this-> q5}
  137. )
  138. )
  139. );
  140. Return $ this;
  141. }
  142. // Recursively read and create the target directory structure
  143. Private function targetDir ($ target ){
  144. If (! Empty ($ target )){
  145. If (! File_exists ($ target )){
  146. Mkdir ($ target, 0777, true );
  147. } Else {
  148. Chmod ($ target, 0777 );
  149. }
  150. }
  151. }
  152. // Recursive decryption decrypts the php file in the specified folder
  153. Public function decodeDir ($ source, $ target = ""){
  154. If (is_dir ($ source )){
  155. $ This-> targetDir ($ target );
  156. $ Dir = opendir ($ source );
  157. While (false! = $ File = readdir ($ dir ))
  158. {
  159. // 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.
  160. If ($ file! = '.' & $ File! = '..' & $ File! = 'Thinkphp ')
  161. {
  162. $ Path = $ target. DIRECTORY_SEPARATOR. $ file;
  163. $ SourcePath = $ source. DIRECTORY_SEPARATOR. $ file;
  164. $ This-> decodeDir ($ sourcePath, $ path );
  165. }
  166. }
  167. } Else if (is_file ($ source )){
  168. $ Extension = substr ($ source, strrpos ($ source, '.') + 1 );
  169. If (strtolower ($ extension) = 'php '){
  170. $ This-> decode ($ source, $ target );
  171. } Else {
  172. // Php files are not processed
  173. Copy ($ source, $ target );
  174. }
  175. // Return;
  176. }
  177. }
  178. // Recursive encryption encrypts the php file in the specified folder
  179. Public function encodeDir ($ source, $ target ){
  180. If (is_dir ($ source )){
  181. $ This-> targetDir ($ target );
  182. $ Dir = opendir ($ source );
  183. While (false! = $ File = readdir ($ dir ))
  184. {
  185. // List all objects and remove '.' and '..'
  186. If ($ file! = '.' & $ File! = '..' & $ File! = 'Thinkphp ')
  187. {
  188. $ Path = $ target. DIRECTORY_SEPARATOR. $ file;
  189. $ SourcePath = $ source. DIRECTORY_SEPARATOR. $ file;
  190. $ This-> encodeDir ($ sourcePath, $ path );
  191. }
  192. }
  193. } Else if (is_file ($ source )){
  194. $ Extension = substr ($ source, strrpos ($ source, '.') + 1 );
  195. If (strtolower ($ extension) = 'php '){
  196. $ This-> encode ($ source, $ target );
  197. } Else {
  198. Copy ($ source, $ target );
  199. }
  200. }
  201. }
  202. }
  203. $ Ob = new Encryption ();
  204. $ Ob-> source = "/var/www/bookReservation ";
  205. $ Ob-> target = "/var/www/jiami/bookReservation ";
  206. // Decrypt the specified file
  207. // $ Ob-> decode ('d: \ php \ WWW \ workspace \ weixin2 \ Application \ Home \ Controller \ IndexController. class. php ');
  208. // $ Ob-> decode ('jiami. php ');
  209. // $ Ob-> decode ('dam6. php ');
  210. // Encrypt a specified file directory
  211. $ Ob-> encodeDir ($ ob-> source, $ ob-> target );
  212. // Decrypt a specified file directory
  213. $ Ob-> decodeDir ($ ob-> target, "/var/www/jiami/bookReservationD ");



Encryption and decryption, php

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.