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,加密完成!"
>
第⼆个⽅法
<?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}.$ $s = '<?php '."\n".$s."\n".' ?>';
//echo $s;
// ⽣成加密后的PHP⽂件
$fpp1 = fopen('temp_'.$filename, 'w');
fwrite($fpp1, $s) or die('写⽂件错误');
>
第三个⽅法
<?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);
}
}
php文件下载源码}
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");
$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");
$filename = S_ROOT . './456.php';
file_put_contents($filename, $str);
以上这篇PHP给源代码加密的⼏种⽅法汇总(推荐)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论