怎样给PHP源代码加密?PHP二进制加密与解密的解决办法
程序员文章站
2022-09-10 10:19:12
分享2种php的源码加密方式,此加密方法支持任意php版。注意,加密后的php代码无需第三方工具解密,像往常一样,直接运行即可。复制代码 代码如下:
分享2种php的源码加密方式,此加密方法支持任意php版。
注意,加密后的php代码无需第三方工具解密,像往常一样,直接运行即可。
<?php
function encode_file_contents($filename) {
$type=strtolower(substr(strrchr($filename,'.'),1));
if ('php' == $type && is_file($filename) && is_writable($filename)) { // 如果是php文件 并且可写 则进行压缩编码
$contents = file_get_contents($filename); // 判断文件是否已经被编码处理
$contents = php_strip_whitespace($filename);
// 去除php头部和尾部标识
$headerpos = strpos($contents,'<?php');
$footerpos = strrpos($contents,'?>');
$contents = substr($contents, $headerpos + 5, $footerpos - $headerpos);
$encode = base64_encode(gzdeflate($contents)); // 开始编码
$encode = '<?php'."\n eval(gzinflate(base64_decode("."'".$encode."'".")));\n\n?>";
return file_put_contents($filename, $encode);
}
return false;
}
//调用函数
$filename = 'dam.php';
encode_file_contents($filename);
echo "ok,加密完成!"
?>
加密方式2:
<?php
function randabc($length = "") { // 返回随机字符串
$str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
return str_shuffle($str);
}
$filename = 'index.php'; //要加密的文件
$t_k1 = randabc(); //随机密匙1
$t_k2 = randabc(); //随机密匙2
$vstr = file_get_contents($filename);
$v1 = base64_encode($vstr);
$c = strtr($v1, $t_k1, $t_k2); //根据密匙替换对应字符。
$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%79%2a6%6c%72%6b%64%679%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;
// 生成 加密后的php文件
$fpp1 = fopen('temp_'.$filename, 'w');
fwrite($fpp1, $s) or die('写文件错误');
?>
其实,php加密源码方式有很多,譬如,免费的微盾php加密,还有 www.phpjm.net 搞的在线加密,只是phpjm更复杂点而已。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
另外,分享一个 php类,它能对 文本的内容进行 二进制加密 与 解密,代码如下:
<?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 = '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, 16, 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, 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 (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($i1, $i2)
{
$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;
}
}
?>
使用方法参考如下:
// 加密过程
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'; // 加密后的文本为二进制,普通的文本编辑器无法正常查看
file_put_contents($filename, $str);
// 解密过程
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);
注意,加密后的php代码无需第三方工具解密,像往常一样,直接运行即可。
复制代码 代码如下:
<?php
function encode_file_contents($filename) {
$type=strtolower(substr(strrchr($filename,'.'),1));
if ('php' == $type && is_file($filename) && is_writable($filename)) { // 如果是php文件 并且可写 则进行压缩编码
$contents = file_get_contents($filename); // 判断文件是否已经被编码处理
$contents = php_strip_whitespace($filename);
// 去除php头部和尾部标识
$headerpos = strpos($contents,'<?php');
$footerpos = strrpos($contents,'?>');
$contents = substr($contents, $headerpos + 5, $footerpos - $headerpos);
$encode = base64_encode(gzdeflate($contents)); // 开始编码
$encode = '<?php'."\n eval(gzinflate(base64_decode("."'".$encode."'".")));\n\n?>";
return file_put_contents($filename, $encode);
}
return false;
}
//调用函数
$filename = 'dam.php';
encode_file_contents($filename);
echo "ok,加密完成!"
?>
加密方式2:
复制代码 代码如下:
<?php
function randabc($length = "") { // 返回随机字符串
$str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
return str_shuffle($str);
}
$filename = 'index.php'; //要加密的文件
$t_k1 = randabc(); //随机密匙1
$t_k2 = randabc(); //随机密匙2
$vstr = file_get_contents($filename);
$v1 = base64_encode($vstr);
$c = strtr($v1, $t_k1, $t_k2); //根据密匙替换对应字符。
$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%79%2a6%6c%72%6b%64%679%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;
// 生成 加密后的php文件
$fpp1 = fopen('temp_'.$filename, 'w');
fwrite($fpp1, $s) or die('写文件错误');
?>
其实,php加密源码方式有很多,譬如,免费的微盾php加密,还有 www.phpjm.net 搞的在线加密,只是phpjm更复杂点而已。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
另外,分享一个 php类,它能对 文本的内容进行 二进制加密 与 解密,代码如下:
复制代码 代码如下:
<?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 = '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, 16, 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, 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 (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($i1, $i2)
{
$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;
}
}
?>
使用方法参考如下:
// 加密过程
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'; // 加密后的文本为二进制,普通的文本编辑器无法正常查看
file_put_contents($filename, $str);
// 解密过程
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);
下一篇: 人工智能新项目为女孩赋能