PHP summarizes several methods for source code encryption (recommended), and php source code

Source: Internet
Author: User

PHP summarizes several methods for source code encryption (recommended), and php source code

Although sharing is a traditional virtue, sometimes we have worked so hard to write programs to sell small money, earn a little hard work, and prevent some unscrupulous people from selling them, so we have to encrypt our program. Here we will introduce how to encrypt our PHP source code through PHP custom functions.

First Method

<? Php function encode_file_contents ($ filename) {$ type = strtolower (substr (strrchr ($ filename ,'. '), 1); if ('php' = $ type & is_file ($ filename) & is_writable ($ filename )) {// If the PHP file is writable, compress the code $ contents = file_get_contents ($ filename); // checks whether the file has been encoded and processed $ contents = php_strip_whitespace ($ filename ); // remove the PHP header and tail identifiers $ headerPos = strpos ($ contents, '<? Php '); $ footerPos = strrpos ($ contents,'?> '); $ Contents = substr ($ contents, $ headerPos + 5, $ footerPos-$ headerPos); $ encode = base64_encode (gzdeflate ($ contents )); // start encoding $ encode = '<? Php '. "\ n eval (gzinflate (base64_decode (". "'". $ encode. "'". "); \ n?> "; Return file_put_contents ($ filename, $ encode);} return false;} // call the function $ filename = 'dam. php '; encode_file_contents ($ filename); echo "OK, encryption is complete! "?>

Method 2

<? Php function RandAbc ($ length = "") {// returns a random string $ str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; return str_shuffle ($ str);} $ filename = 'index. php '; // the file to be encrypted $ T_k1 = RandAbc (); // random key 1 $ T_k2 = RandAbc (); // random key 2 $ vstr = file_get_contents ($ filename); $ v1 = base64_encode ($ vstr); $ c = strtr ($ v1, $ T_k1, $ T_k2 ); // replace the corresponding character with the key. $ C = $ T_k1. $ T_k2. $ c; $ q1 = "O00O0O"; $ q2 = "O0O000"; $ q3 = "O0OO00"; $ q4 = "OO0O00"; $ q5 = "OO0000 "; $ q6 = "O00OO0"; $ s = '$ '. $ q6. '= urldecode ("% 6E1% 7A % 62% 2F % 6D % 615% 5C % 76% 740% 6928% 2D % 70% 78% 75% 71% 2A6% 6C % 79% 6B % 72% 5F % 65% 68% 63% 73% 77% 6F4% 2B % 6637% 6A "); $ '. $ q1. '= $ '. $ q6. '{3 }. $ '. $ q6. '{6 }. $ '. $ q6. '{33 }. $ '. $ q6. '{30}; $ '. $ q3. '= $ '. $ q6. '{33 }. $ '. $ q6. '{10 }. $ '. $ q6. '{24 }. $ '. $ q6. '{10 }. $ '. $ q6. '{24}; $ '. $ q4. '= $ '. $ q3. '{0 }. $ '. $ q6. '{18 }. $ '. $ q6. '{3 }. $ '. $ q3. '{0 }. $ '. $ q3. '{1 }. $ '. $ q6. '{24}; $ '. $ q5. '= $ '. $ q6. '{7 }. $ '. $ q6. '{13}; $ '. $ q1 .'. = $ '. $ q6. '{22 }. $ '. $ q6. '{36 }. $ '. $ q6. '{29 }. $ '. $ q6. '{26 }. $ '. $ q6. '{30 }. $ '. $ q6. '{32 }. $ '. $ q6. '{35 }. $ '. $ q6. '{26 }. $ '. $ q6. '{30}; eval ($ '. $ q1 .'("'. base64_encode ('$ '. $ q2. '= "'. $ c. '"; eval (\'?> \'. $ '. $ Q1. '($ '. $ q3. '($ '. $ q4. '($ '. $ q2. ', $ '. $ q5. '* 2), $ '. $ q4. '($ '. $ q2. ', $ '. $ q5. ', $ '. $ q5. '), $ '. $ q4. '($ '. $ q2. ', 0, $ '. $ q5 .'))));'). '");'; $ s = '<? Php '. "\ n". $ s. "\ n".'?> '; // Echo $ s; // generate the encrypted PHP File $ fpp1 = fopen ('temp _'. $ filename, 'w'); fwrite ($ fpp1, $ s) or die ('file writing error');?>

Third Method

<? Php class text_auth {var $ n_iter; function text_auth () {$ this-> setIter (32);} function setIter ($ n_iter) {$ this-> n_iter = $ n_iter ;} function getIter () {return $ this-> n_iter;} function encrypt ($ data, $ key) {$ n = $ this-> _ resize ($ data, 4 ); $ data_long [0] = $ n; $ n_data_long = $ this-> _ str2long (1, $ data, $ data_long); $ n = count ($ data_long ); if ($ n & 1) = 1) {$ data_long [$ n] = chr (0); $ n_data_long + + ;}$ This-> _ resize ($ key, 16, true); if (''= $ key) $ key = '123 '; $ n_key_long = $ this-> _ str2long (0, $ key, $ key_long); $ enc_data = ''; $ w = array (0, 0); $ j = 0; $ k = array (0, 0, 0, 0); for ($ I = 0; $ I <$ n_data_long; ++ $ I) {if ($ j + 4 <= $ n_key_long) {$ k [0] = $ key_long [$ j]; $ k [1] = $ key_long [$ j + 1]; $ k [2] = $ key_long [$ j + 2]; $ k [3] = $ key_long [$ j + 3];} else {$ k [0] = $ key_long [$ J % $ n_key_long]; $ k [1] = $ key_long [($ j + 1) % $ n_key_long]; $ k [2] = $ key_long [($ j + 2) % $ n_key_long]; $ k [3] = $ key_long [($ j + 3) % $ n_key_long] ;}$ j = ($ j + 4) % $ n_key_long; $ this-> _ encipherLong ($ data_long [$ I], $ data_long [++ $ I], $ w, $ k); $ enc_data. = $ this-> _ long2str ($ w [0]); $ enc_data. = $ this-> _ long2str ($ w [1]);} return $ enc_data;} function decrypt ($ enc_data, $ key) {$ n_enc_data_long = $ thi S-> _ str2long (0, $ enc_data, $ enc_data_long); $ this-> _ resize ($ key, 16, true); if (''= $ key) $ key = '000000'; $ n_key_long = $ this-> _ str2long (0, $ key, $ key_long); $ data = ''; $ w = array (0, 0); $ j = 0; $ len = 0; $ k = array (0, 0, 0, 0); $ pos = 0; for ($ I = 0; $ I <$ n_enc_data_long; $ I + = 2) {if ($ j + 4 <= $ n_key_long) {$ k [0] = $ key_long [$ j]; $ k [1] = $ key_long [$ j + 1]; $ k [2] = $ key_long [$ J + 2]; $ k [3] = $ key_long [$ j + 3];} else {$ k [0] = $ key_long [$ j % $ n_key_long]; $ k [1] = $ key_long [($ j + 1) % $ n_key_long]; $ k [2] = $ key_long [($ j + 2) % $ n_key_long]; $ k [3] = $ key_long [($ j + 3) % $ n_key_long];} $ j = ($ j + 4) % $ n_key_long; $ this-> _ decipherLong ($ enc_data_long [$ I], $ enc_data_long [$ I + 1], $ w, $ k); if (0 = $ I) {$ len = $ w [0]; if (4 <= $ len) {$ data. = $ this-> _ long2str ($ w [1]);} els E {$ data. = substr ($ this-> _ long2str ($ w [1]), 0, $ len % 4) ;}} else {$ pos = ($ I-1) * 4; if ($ pos + 4 <= $ len) {$ data. = $ this-> _ long2str ($ w [0]); if ($ pos + 8 <= $ len) {$ data. = $ this-> _ long2str ($ w [1]);} elseif ($ pos + 4 <$ len) {$ data. = substr ($ this-> _ long2str ($ w [1]), 0, $ len % 4) ;}} else {$ data. = substr ($ this-> _ long2str ($ w [0]), 0, $ len % 4) ;}} return $ data;} function _ encipherLon G ($ y, $ z, & $ w, & $ k) {$ sum = (integer) 0; $ delta = 0x9E3779B9; $ n = (integer) $ this-> n_iter; while ($ n --> 0) {$ y = $ this-> _ add ($ y, $ this-> _ add ($ z <4 ^ $ this-> _ rshift ($ z, 5), $ z) ^ $ this-> _ add ($ sum, $ k [$ sum & 3]); $ sum = $ this-> _ add ($ sum, $ delta ); $ z = $ this-> _ add ($ z, $ this-> _ add ($ y <4 ^ $ this-> _ rshift ($ y, 5 ), $ y) ^ $ this-> _ add ($ sum, $ k [$ this-> _ rshift ($ sum, 11) & 3]);} $ w [0] = $ y; $ w [1] = $ Z;} function _ decipherLong ($ y, $ z, & $ w, & $ k) {$ sum = 0xC6EF3720; $ delta = 0x9E3779B9; $ n = (integer) $ this-> n_iter; while ($ n --> 0) {$ z = $ this-> _ add ($ z, -($ this-> _ add ($ y <4 ^ $ this-> _ rshift ($ y, 5), $ y) ^ $ this-> _ add ($ sum, $ k [$ this-> _ rshift ($ sum, 11) & 3]); $ sum = $ this-> _ add ($ sum,-$ delta); $ y = $ this-> _ add ($ y, -($ this-> _ add ($ z <4 ^ $ this-> _ rshift ($ z, 5), $ z) ^ $ this-> _ add ($ sum, $ k [$ su M & 3]);} $ w [0] = $ y; $ w [1] = $ z;} function _ resize (& $ data, $ size, $ nonull = false) {$ n = strlen ($ data); $ nmod = $ n % $ size; if (0 = $ nmod) $ nmod = $ size; if ($ nmod> 0) {if ($ nonull) {for ($ I = $ n; $ I <$ n-$ nmod + $ size; ++ $ I) {$ data [$ I] = $ data [$ I % $ n] ;}} else {for ($ I = $ n; $ I <$ n-$ nmod + $ size; ++ $ I) {$ data [$ I] = chr (0) ;}} return $ n ;} function _ hex2bin ($ str) {$ len = Strlen ($ str); return pack ('H '. $ len, $ str);} function _ str2long ($ start, & $ data, & $ data_long) {$ n = strlen ($ data ); $ tmp = unpack ('n' * ', $ data); $ j = $ start; foreach ($ tmp as $ value) $ data_long [$ j ++] = $ value; return $ j;} function _ long2str ($ l) {return pack ('n', $ l);} function _ rshift ($ integer, $ N) {if (0 xffffffff <$ integer |-0 xffffffff> $ integer) {$ integer = fmod ($ integer, 0 xffffffffff + 1 );} If (0x7fffffff <$ integer) {$ integer-= 0 xffffffff + 1.0;} elseif (-0x80000000> $ integer) {$ integer + = 0 xffffffff + 1.0;} if (0> $ integer) {$ integer & = 0x7fffffff; $ integer >>=$ n; $ integer | = 1 <(31-$ n);} else {$ integer >>=$ n;} return $ integer;} function _ add ($ i1, $ i2) {$ result = 0.0; foreach (func_get_args () as $ value) {if (0.0> $ value) {$ value-= 1.0 + 0 xffffffff;} $ re Sult + = $ value;} if (0 xffffffff <$ result |-0 xffffffff> $ result) {$ result = fmod ($ result, 0 xffffffff + 1 );} if (0x7fffffff <$ result) {$ result-= 0 xffffffff + 1.0;} elseif (-0x80000000> $ result) {$ result + = 0 xffffffff + 1.0 ;} return $ result ;}?> For how to use it, see // view sourceprint in the encryption process? $ Text_file = S_ROOT. '. /456. php '; $ str = @ file_get_contents ($ text_file); require_once S_ROOT. ". /text_auth.php "; $ text_auth = new text_auth (64); $ str = $ text_auth-> encrypt ($ str," qianyunlai.com "); $ filename = S_ROOT. '. /789. php '; // The encrypted text is binary. A common text editor cannot view file_put_contents ($ filename, $ str); // view sourceprint during decryption? 01 $ text_file = S_ROOT. '. /789. php '; $ str = @ file_get_contents ($ text_file); require_once S_ROOT. ". /text_auth.php "; $ text_auth = new text_auth (64); $ str = $ text_auth-> decrypt ($ str," qianyunlai.com "); $ filename = S_ROOT. '. /456. php '; file_put_contents ($ filename, $ str );

The above summary (recommended) of several PHP methods for source code encryption is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the customer's house.

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.