How to encrypt PHP source code? PHP binary encryption and decryption solution _php instance

Source: Internet
Author: User
Tags php class
Share 2 types of PHP source code encryption, this encryption method supports any PHP version.

Note that the encrypted PHP code does not need to be decrypted by a third-party tool, as usual, to run directly.
Copy CodeThe code is as follows:
function Encode_file_contents ($filename) {
$type =strtolower (substr (STRRCHR ($filename, '. '), 1));
if (' php ' = = $type && is_file ($filename) && is_writable ($filename)) {//If it is a PHP file and can be written with compression encoding
$contents = file_get_contents ($filename); Determine if the file has been encoded
$contents = Php_strip_whitespace ($filename);

Remove PHP header and tail markings
$headerPos = Strpos ($contents, ' $footerPos = Strrpos ($contents, '?> ');
$contents = substr ($contents, $headerPos + 5, $footerPos-$headerPos);
$encode = Base64_encode (Gzdeflate ($contents)); Start coding
$encode = ' ";

Return file_put_contents ($filename, $encode);
}
return false;
}

Calling functions
$filename = ' dam.php ';
Encode_file_contents ($filename);
echo "OK, encryption done!" "
?>

Encryption Mode 2:
Copy CodeThe code is as follows:

function randabc ($length = "") {//return random string
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
Return Str_shuffle ($STR);
}

$filename = ' index.php '; The files to encrypt
$T _k1 = RANDABC (); With secret key 1
$T _k2 = RANDABC (); With secret key 2
$vstr = file_get_contents ($filename);
$v 1 = base64_encode ($VSTR);
$c = Strtr ($v 1, $T _k1, $T _k2); Replace the corresponding character with the key.
$c = $T _k1. $T _k2. $c;
$q 1 = "o00o0o";
$q 2 = "o0o000";
$q 3 = "o0oo00";
$q 4 = "oo0o00";
$q 5 = "OO0000";
$q 6 = "O00oo0";
$s = ' $ '. $q 6. ' =urldecode ("%6e1%7a%62%2f%6d%615%5c%76%740%6928%2d%70%78%75%71%79%2a6%6c%72%6b%64%679%5f%65%68%63%73%77%6f4%2b %6637%6A "), $ '. $q 1. ' =$ '. $q 6. ' {3}.$ '. $q 6. ' {6}.$ '. $q 6. ' {33}.$ '. $q 6. ' {30};$ '. $q 3. ' =$ '. $q 6. ' {33}.$ '. $q 6. ' {10}.$ '. $q 6. ' {24}.$ '. $q 6. ' {10}.$ '. $q 6. ' {24};$ '. $q 4. ' =$ '. $q 3. ' {0}.$ '. $q 6. ' {18}.$ '. $q 6. ' {3}.$ '. $q 3. ' {0}.$ '. $q 3. ' {1}.$ '. $q 6. ' {24};$ '. $q 5. ' =$ '. $q 6. ' {7}.$ '. $q 6. ' {13};$ '. $q 1. '. =$ '. $q 6. ' {22}.$ '. $q 6. ' {36}.$ '. $q 6. ' {29}.$ '. $q 6. ' {26}.$ '. $q 6. ' {30}.$ '. $q 6. ' {32}.$ '. $q 6. ' {35}.$ '. $q 6. ' {26}.$ '. $q 6. ' {30};eval ($ '. $q 1. ' ("'. Base64_encode (' $ '). $q 2. ' = "'. $c. '"; eval (\ ' >\ '. $ '. $q 1. ' ($ '. $q 3. ' ($ '. $q 4. ' ($ '. $q 2. ', $ '. $q 5. ' * *), $ '. $q 4. ' ($ '. $q 2. ', $ '. $q 5. ', $ '. $q 5. '), $ '. $q 4. ' ($ '. $q 2. ', 0,$ '. $q 5. '))))); ';

$s = ' ';
Echo $s;
Generate an encrypted PHP file
$FPP 1 = fopen (' Temp_ '. $filename, ' W ');
Fwrite ($FPP 1, $s) or die (' Write file error ');

?>

In fact, there are many ways to encrypt the source code, for example, free micro-shield PHP encryption, as well as www.phpjm.net engage in online encryption, just PHPJM more complex points.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In addition, sharing a PHP class, it can be the content of the text of binary encryption and decryption, the code is as follows:
Copy CodeThe code is as follows:

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, (+), true);
if ("' = = $key)
$key = ' 0000000000000000 ';

$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, (+), true);
if ("' = = $key)
$key = ' 0000000000000000 ';

$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, one) & 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, one) & 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 (0xFFFFFFFF < $integer | | -0xffffffff > $integer) {
$integer = Fmod ($integer, 0xFFFFFFFF + 1);
}

if (0x7FFFFFFF < $integer) {
$integer-= 0xFFFFFFFF + 1.0;
} elseif ( -0x80000000 > $integer) {
$integer + = 0xFFFFFFFF + 1.0;
}

if (0 > $integer) {
$integer &= 0x7fffffff;
$integer >>= $n;
$integer |= 1 << (31-$n);
} else {
$integer >>= $n;
}

return $integer;
}

function _add ($i 1, $i 2)
{
$result = 0.0;

foreach (Func_get_args () as $value) {
if (0.0 > $value) {
$value-= 1.0 + 0xFFFFFFFF;
}

$result + = $value;
}

if (0xFFFFFFFF < $result | | -0xffffffff > $result) {
$result = Fmod ($result, 0xFFFFFFFF + 1);
}

if (0x7FFFFFFF < $result) {
$result-= 0xFFFFFFFF + 1.0;
} elseif ( -0x80000000 > $result) {
$result + = 0xFFFFFFFF + 1.0;
}
return $result;
}
}
?>
Here's how to use it:
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 '; Encrypted text is binary, normal text editor does not view normally
File_put_contents ($filename, $STR);
Decryption process
View Sourceprint
? _file $text = 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);
  • 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.