加密:
<?php /** * 加密原理: * eval()函数:把字符串按照PHP代码来计算,该字符串必须是合法的PHP代码,且必须以分号结尾。 * strtr()字符替换函数:把字符串中的字符"o"替换为"0",例如:strtr("Hello World","o","0"); */ function T_rndstr($length = "") { //返回随机字符串 $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; if ($length == "") { return str_shuffle($str); } else { return substr(str_shuffle($str), -$length); } } $T_k1 = T_rndstr();//随机密匙1 $T_k2 = T_rndstr();//随机密匙2 /* test.php为要加密的原文件 */ $vstr = file_get_contents("./test.php"); $v1 = base64_encode($vstr); $c = strtr($v1, $T_k1, $T_k2);//根据密匙替换对应字符 $c = $T_k1 . $T_k2 . $c;//$qn变量功能下面会讲解 $isqs = 3; if ($isqs == "1") { // 1 取随机字符串为变量名 $q = T_rndstr(); // 随机字符串 $q1 = substr($q, 2, 3); $q2 = substr($q, 10, 10); $q3 = substr($q, 20, 12); $q4 = substr($q, 30, 10); $q5 = substr($q, 40, 8); $q6 = substr($q, 5, 5); } else if ($isqs == "2") { // 2 为小写l和1组成,开头必须是字母 $q1 = "ll11l1l1"; $q2 = "l1lll11l"; $q3 = "ll1l1lll"; $q4 = "l1lll1l1"; $q5 = "l1l1ll11"; $q6 = "ll111l1l"; } else { // 大写O与数字0组成基本变量 $q1 = "O00O0O"; $q2 = "O0O000"; $q3 = "O0OO00"; $q4 = "OO0O00"; $q5 = "OO0000"; $q6 = "O00OO0"; } $keystr = 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"); /** * 字符串,里面必须要有变量所需的字符,比如 base64_decode密钥:n1zb/ma5\vt0i28-pxuqy*6lrkdg9_ehcswo4+f37j * $q1 = base * $q3 = strtr * $q4 = substr * $q5 = 52 -> ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 长度 * $q1 .= 64_decode -> base64_decode */ $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 . '))));') . '"));'; echo $s; /* test_encode.php为生成的已加密文件 */ file_put_contents('./test_encode.php', '<?php '.$s.'?>'); ?>
加密后的文件如下图:
解密:
<?php /* test_encode.php为要解密的原文件 */ $encode_sourcecode = file_get_contents("./test_encode.php"); // 去掉php标识 $encode_sourcecode = str_replace('<?php','', $encode_sourcecode); $encode_sourcecode = str_replace('?>','', $encode_sourcecode); // 提取第一次需要解密的内容 // 即JE8wTzAwMD0iTmV5SElCamZRdk......1UYVNuUUpnY21UYVVoOCtoTm89IjtldmFsKCc/==echo '</br></br>------------------------------$encode_sourcecode_content</br>'; $start = strripos($encode_sourcecode, '("') + 2; $end = strripos($encode_sourcecode, '")'); $encode_sourcecode_content = substr($encode_sourcecode, $start, $end - $start); // 解密加密部分的代码后的内容 // $O0O000="NeyHIBjfQvDMwo......JoSptgMdl3M3JgSKQTiB0nuh8+hNo="; eval('......OO0000)))); $decode_sourcecode_content = base64_decode($encode_sourcecode_content); // 解密后还是一个加密的代码,需要再次解码,所以要再次提取需要被解密的内容出来 // 即NeyHIBjfQvDMwo......JoSptgMdl3M3JgSKQTiB0nuh8+hNo=echo '</br></br>------------------------------$decode_sourcecode_content_encode_content</br>'; $start = stripos($decode_sourcecode_content, '"') + 1; $end = strripos($decode_sourcecode_content, '"') ; $decode_sourcecode_content_encode_content = substr($decode_sourcecode_content, $start, $end - $start); // 根据加密规则,替换字符并解码,即可得到原文件echo '</br></br>------------------------------$decode_sourcecode_content_encode_content</br>'; $origin_content = base64_decode(strtr(substr($decode_sourcecode_content_encode_content, 104),substr($decode_sourcecode_content_encode_content, 52, 52),substr($decode_sourcecode_content_encode_content, 0, 52))); //输出解密结果: echo '<fieldset style="witdh:100%;border: 1px solid #ccc;color: #666;padding: 10px;margin: 10px;"><legend>解密结果:</legend><pre>'; echo htmlentities($origin_content); echo '</fieldset>'; /* test_encode.php为生成的已解密文件 */ file_put_contents('./test_origin.php', $origin_content);
请登录之后再进行评论