<?php
/**
* Created by PhpStorm.
* User: Yang
* Date: 2019/10/16
* Time: 10:25
*/
class Encipher {
private $_sourceFile = '';
private $_encodedFile = '';
private $_comments = array(
'Author: Yang',
'Email: 1017836267@qq.com'
);
public function __construct($sourceFile, $encodeFile, $comments = array())
{
!empty($sourceFile) && $this->_sourceFile = $sourceFile;
!empty($encodeFile) && $this->_encodedFile = $encodeFile;
!empty($comments) && $this->comments = (array)$comments;
if (empty($this->_sourceFile) || !file_exists($this->_sourceFile)) {
exit("Source file does not exist.");
}
if (empty($this->_encodedFile) || !file_exists($this->_encodedFile)) {
//如果源文件不存在,则创建
fopen($this->_encodedFile, "w");
}
}
/**
* 返回随机字符串
* @return string
*/
private function createRandKey()
{ // 返回随机字符串
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return str_shuffle($str);
}
/**
* 加密函数
* @return bool
*/
public function encode() {
//随机密匙1
$k1 = $this->createRandKey();
//随机密匙2
$k2 = $this->createRandKey();
// 获取源文件内容
$sourceContent = file_get_contents($this->_sourceFile);
//base64加密
$base64 = base64_encode($sourceContent);
//根据密匙替换对应字符。
$c = strtr($base64, $k1, $k2);
$c = $k1 . $k2 . $c;
$q1 = "O00O0O";
$q2 = "O0O000";
$q3 = "O0OO00";
$q4 = "OO0O00";
$q5 = "OO0000";
$q6 = "O00OO0";
$encodeContent = '$' . $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 . '))));') . '"));';
$headers = array_map('trim', array_merge(array('/*'), $this->_comments, array('*/')));
$enCode = "<?php"."\r\n".implode("\r\n", $headers) . "\r\n".$encodeContent."\r\n"."?>";
$file = fopen($this->_encodedFile, 'w');
return fwrite($file, $enCode) or die('写文件错误');
}
}
$e = new Encipher("element.php", "element_1.php");
$e ->encode();
echo "加密成功";