How to encrypt PHP source code? PHP binary encryption and decryption Solution

Source: Internet
Author: User
Tags php source code

Share two PHP source code encryption methods. This encryption method supports any PHP version.

Note: The encrypted PHP code does not need to be decrypted by a third-party tool. You can directly run it as usual.Copy codeThe Code is as follows: <? Php
Function encode_file_contents ($ filename ){
$ Type = strtolower (substr (strrchr ($ filename, '.'), 1 ));
If ('php' = $ type & is_file ($ filename) & is_writable ($ filename) {// compress the Code if the php file is writable.
$ Contents = file_get_contents ($ filename); // checks whether the file has been encoded.
$ 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 a function
$ Filename = 'dam. php ';
Encode_file_contents ($ filename );
Echo "OK, encryption complete! "
?>

Encryption Method 2:Copy codeThe Code is as follows: <? Php

Function RandAbc ($ length = "") {// returns a random string
$ Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
Return str_shuffle ($ str );
}

$ Filename = 'index. php'; // 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 ');

?>

In fact, there are many PHP source code encryption methods, such as free micro-shield PHP encryption and online Encryption By www.phpjm.net, which are more complex than phpjm.

++ ++ ++

In addition, share a PHP class that can encrypt and decrypt the text in binary format. The Code is as follows:Copy codeThe Code is as follows: <? 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 = '000000 ';

$ 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 = $ this-> _ 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]);
} Else {
$ 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 _ encipherLong ($ 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 [$ sum & 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 xffffffff + 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;
}

$ Result + = $ 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;
}
}
?>
The usage is as follows:
// Encryption process
View sourceprint?
$ 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 and cannot be viewed in a common text editor.
File_put_contents ($ filename, $ str );
// Decryption process
View sourceprint
? 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 );

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.