php加密解密功能类
这两天突发奇想想要用php写一个对日常项目加密以及解密的功能,经过努力简单的封装了一个对php代码进行加密解密的类,一些思想也是来自于网络,初步测试用着还行,可以实现对指定项目的加密以及解密(只针对本类中加密的解密)过程,在这里分享给大家,水平比较有限那里有错误还希望指出,共同提高,一下会给大家列出来实现的思想以及源码。
加密过程:
读取源文件,base64_encode进行加密,利用混排得到的52个大小写字母作为秘钥进行替换
$c=strtr(密文,对应待替换的字母,要替换成的字母);
将两个秘钥和密文链接起来形成要加密文件的主要内容
最后根据提前写好的模板格式,分别对base64_decode,strtr,substr几个命令进行组合,并将组和好的密文放入模板并在此base64_encode加密,
写入要加密的文件。
解密过程:
读取要解密的文件,截取出以eval开头的字符串,之后层次截取得到加密模板中生成的密文
base64_decode解密得到解密后的明文
截取得到的名文,将源文件形成的 秘钥+密文那段的字符截取出来,通过eval执行截取的字符使密文赋值给预定义的变量($O0O000)
通过执行base64_decode(strtr(substr($O0O000,52*2),substr($O0O000,52,52),substr($O0O000,0,52)));
进行解密,将解密过的内容(明文)写入文件。
总结:其实整个加密解密的过程都是互逆的,加密后的php运行是通过eval()这个函数实现的,解密的过程就是加密的逆过程。
- <?php
- /*
- * @auther:wangyaofeng
- * @time:2014.11.6
- * @action:对php项目进行加密处理
- * @Example:
- *
- * $ob = new Encryption();
- * $ob->source = "D:\php\WWW\workspace\weixin1";
- * $ob->target = "D:\php\WWW\workspace\weixin2";
- * $ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');
- * $ob->decode('jiami.php');
- * $ob->decode('dam6.php');
- * $ob->encodeDir($ob->source,$ob->target);
- * $ob->decodeDir($ob->target);
- *
- * */
- class Encryption{
- private $c='';//存储密文
- private $s='',$q1,$q2,$q3,$q4,$q5,$q6;//存储生成的加密后的文件内容
- //如果不设置一个值,isset会表示不存在;
- private $file='';//读取文件的路径
- private $source='',$target='';
- //构造函数,实例化时调用初始化全局变量;
- public function __construct(){
- //初始化全局变量
- $this->initialVar();
- //echo "hello \n";
- }
- /*
- *@input $property_name,$value
- *@output
- * 魔法方法,对变量进行设置值;可根据需求进行处理。直接取出if判断表示可用设置任何属性的值,包括不存在的属性;
- */
- public function __set($property_name,$value){
- //定义过的变量;
- if(isset($this->$property_name)){
- $this->$property_name = $value;
- }else{
- //异常处理,处理未声明的变量赋值;可根据需求进行处理。
- throw new Exception("property does not exist");
- }
- }
- //魔法方法 取出变量的值;
- public function __get($property_name){
- if(isset($this->$property_name)){
- return $this->$property_name;
- }else{
- //throw new Exception("property does not exist");
- return NULL;
- }
- }
- //取随机排序
- private function RandAbc($length=""){//随机排序取回
- $str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- return str_shuffle($str);
- }
- //加密处理
- private function ciphertext($filename){
- //$filename='index.php';
- $T_k1=$this->RandAbc();
- $T_k2=$this->RandAbc();
- $vstr=file_get_contents($filename);
- $v1=base64_encode($vstr);
- $c=strtr($v1,$T_k1,$T_k2);
- //$this->$c 为错误写法 会报错Cannot access empty property
- //正确写法 $this->c;
- $this->c=$T_k1.$T_k2.$c;
- return $this;
- }
- //生成加密后的模板(简单版本);
- private function modelEasy(){
- $this->s='<?php
- '.'$c='
- .'"'.$this->c.'";'."\n".
- '$m='
- .
- 'strtr(substr($c,52*2),substr($c,52,52),substr($c,0,52));'."\n"
- .
- '$s=base64_decode('
- .
- '$m'
- .
- ');'.
- 'eval("'.'?>".$s'.');'.
- '
- ?>';
- return $this;
- }
- //初始化变量
- private function initialVar(){
- $this->q1="O00O0O";//base64_decode
- $this->q2="O0O000";//$c(原文经过strtr置换后的密文,由 目标字符+替换字符+base64_encode(‘原文内容’)构成)
- $this->q3="O0OO00";//strtr
- $this->q4="OO0O00";//substr
- $this->q5="OO0000";//52
- $this->q6="O00OO0";//urldecode解析过的字符串(n1zb/ma5\vt0i28-pxuqy*6%6Crkdg9_ehcswo4+f37j)
- }
- //生成加密后的模板(复杂版本);
- private function model(){
- $c = $this->c;
- //$this->initialVar();
- $this->s='<?php $'.$this->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");$'.
- $this->q1.'=$'.$this->q6.'{3}.$'.$this->q6.'{6}.$'.$this->q6.'{33}.$'.$this->q6.'{30};$'.$this->q3.'=$'.$this->q6.'{33}.$'.$this->q6.'{10}.$'
- .$this->q6.'{24}.$'.$this->q6.'{10}.$'.$this->q6.'{24};$'.$this->q4.'=$'.$this->q3.'{0}.$'.$this->q6.'{18}.$'.$this->q6.'{3}.$'.$this->q3.'{0}
- .$'.$this->q3.'{1}.$'.$this->q6.'{24};$'.$this->q5.'=$'.$this->q6.'{7}.$'.$this->q6.'{13};$'.$this->q1.'.=$'.$this->q6.'{22}.$'.$this->q6.'{36}
- .$'.$this->q6.'{29}.$'.$this->q6.'{26}.$'.$this->q6.'{30}.$'.$this->q6.'{32}.$'.$this->q6.'{35}.$'.$this->q6.'{26}.$'.$this->q6.'{30};
- eval($'.$this->q1.'("'.base64_encode('$'.$this->q2.'="'.$c.'";
- eval(\'?>\'.$'.$this->q1.'($'.$this->q3.'($'.$this->q4.'($'.$this->q2.',$'.$this->q5.'*2),$'.$this->q4.'($'.$this->q2.',$'.$this->q5.',$'.$this->q5.'),
- $'.$this->q4.'($'.$this->q2.',0,$'.$this->q5.'))));').'"));?>';
- return $this;
- }
- //创建加密文件
- private function build($file,$target){
- //$this->encodes("./index.php");
- //$this->model();
- $fpp1 = fopen($target,'w');
- fwrite($fpp1,$this->s) or die('写入是失败!');
- return $this;
- }
- //加密处理 连贯操作
- public function encode($file,$target){
- //$file = "index.php";
- //连贯操作其实就是利用函数处理完后返回自身
- $this->ciphertext($file)->model()->build($file,$target);
- echo 'encode------'.$target.'-----ok<br/>';
- }
- //解密
- public function decode($file,$target=''){
- //导入文件(类名不应该和导入文件的类名相同)
- //$file = ($target=='')?$file:$target;
- /*require_once("$file");
- //写入文件中的解析后的值(简单版的解密直接是$s)
- //fwrite($fpp1,$s) or die('写入失败!');
- //初始化系统常量
- //$this->initialVar();
- //
- if(${$this->q2}){
- $s = ${$this->q1}(${$this->q3}(${$this->q4}(${$this->q2},${$this->q5}*2),${$this->q4}(${$this->q2},${$this->q5},${$this->q5}),${$this->q4}(${$this->q2},0,${$this->q5})));
- }else{
- $s = file_get_contents($file);
- }
- //打开文件
- $fpp1 = fopen($file,'w');
- //写入文件
- fwrite($fpp1,$s) or die('写入失败!');*/
- //读取要解密的文件
- $fpp1 = file_get_contents($file);
- //截取以 eval开头的部分
- preg_match('[eval.*]', $fpp1,$m);
- if(!isset($m[0])){
- return;
- }
- $len = strlen($m[0]);
- //将eval开头的字符串中间的加密字符截取出来
- $str = substr($m[0],strlen('eval($O00O0O('),$len-18);
- //对密文进行解密
- $str = base64_decode($str);
- //截取出解密后的 核心密文
- $str = substr($str,0 ,strlen($str)-120);
- //执行核心密文
- eval($str);
- //解密
- $s = base64_decode(strtr(substr($O0O000,52*2),substr($O0O000,52,52),substr($O0O000,0,52)));
- //写入文件
- $fpp1 = fopen($file,'w');
- fwrite($fpp1,$s) or die('写入失败!');
- echo 'decode------'.$target.'-----ok<br/>';
- }
- //递归解密 对指定文件夹下的php文件解密
- public function decodeDir($source,$target=""){
- //$target = $source;
- if(is_dir($source)){
- //mkdir($target);
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
- //列出所有文件并去掉'.'和'..' Thinkphp 表示项目目录中的Thinkphp框架,默认是不进行递归加密,可以根据需求修改
- if($file!='.' && $file!='..' && $file!='ThinkPHP')
- {
- //$path = $target.'\\'.$file;
- $sourcePath = $source.'\\'.$file;
- $this->decodeDir($sourcePath);
- }
- }
- }else if(is_file($source)){
- $extension=substr($source,strrpos($source,'.')+1);
- if(strtolower($extension)=='php'){
- $this->decode($source);
- }else{
- //不是php的文件不处理
- //copy($source, $target);
- }
- //return;
- }
- }
- //递归加密 对指定文件夹下的php文件加密
- public function encodeDir($source,$target){
- if(is_dir($source)){
- @mkdir($target);
- $dir = opendir($source);
- while(false!=$file=readdir($dir))
- {
- //列出所有文件并去掉'.'和'..'
- if($file!='.' && $file!='..' && $file!='ThinkPHP')
- {
- $path = $target.'\\'.$file;
- $sourcePath = $source.'\\'.$file;
- $this->encodeDir($sourcePath,$path);
- }
- }
- }else if(is_file($source)){
- $extension=substr($source,strrpos($source,'.')+1);
- if(strtolower($extension)=='php'){
- $this->encode($source,$target);
- }else{
- copy($source, $target);
- }
- //return;
- }
- }
- }
- $ob = new Encryption();
- $ob->source = "D:\php\WWW\workspace\weixin1";
- $ob->target = "D:\php\WWW\workspace\weixin2";
- //解密指定文件
- //$ob->decode('D:\\php\\WWW\\workspace\\weixin2\\Application\\Home\\Controller\\IndexController.class.php');
- //$ob->decode('jiami.php');
- //$ob->decode('dam6.php');
- //对一个指定的文件目录进行加密
- //$ob->encodeDir($ob->source,$ob->target);
- //对一个指定的文件目录进行解密
- //$ob->decodeDir($ob->target);
php加密解密功能类的更多相关文章
- C# MD5加密解密帮助类
/// <summary> /// MD5加密解密帮助类 /// </summary> public static class DESHelper { ...
- php加密解密处理类
[PHP]代码 <?php /*=========================================================== = 版权协议: = GPL (The GN ...
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- .Net(c#)加密解密工具类:
/// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelper { #region des实 ...
- Base64加密解密工具类
使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...
- php实现的三个常用加密解密功能函数示例
目录 算法一: 算法二: 算法三(改进第一个加密之后的算法) 本文实例讲述了php实现的三个常用加密解密功能函数.分享给大家供大家参考,具体如下: 算法一: //加密函数 function lock_ ...
- C#做的一个加密/解密的类
转自:http://www.16aspx.com/Article/3904 using System; using System.Security.Cryptography; using System ...
- [DEncrypt] C# DEncrypt加密/解密帮助类(转载)
点击下载 DEncrypt.rar 这个类是关于加密,解密的操作,文件的一些高级操作1.使用 缺省密钥字符串 加密/解密string2.使用 给定密钥字符串 加密/解密string3.使用 缺省密钥字 ...
- 加密解密工具类(Java,DES)
一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...
随机推荐
- ueditor .net版本上传不了图片问题
百度的Ueditor适合中国人,所以,选择了它,可是恶梦才刚刚开始,最头痛的就是图片死活就是上传不成功,悲剧中.. 现在来分析下为什么报错:什么也不说了,上图
- 1055: [HAOI2008]玩具取名
Description 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用"WING" ...
- linux下如何开启oracle服务和开启监听
su - oracle 切换到oracle用户模式下 sqlplus /nolog //登录sqlplus SQL> connect /as sysdba //连接oracle SQL&g ...
- 【树上莫队】【带修莫队】【权值分块】bzoj4129 Haruna’s Breakfast
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- 使用MyBatis Generator自动创建代码
SSM框架--使用MyBatis Generator自动创建代码 1. 目录说明 使用自动生成有很多方式,可以在eclipse中安装插件,但是以下将要介绍的这种方式我认为很轻松,最简单,不需要装插件, ...
- Ubuntu下制作ISO文件
利用Ubuntu自带的命令mkisofs就可以制作iso文件,具体方法如下: 1. 如果你是直接从cd压制iso文件的,执行 sudo umount /dev/cdromdd if=/dev/cd ...
- Install nutch
1. Install nutch on single node: $apt-get install subversion $apt-get install ant $svn co https://sv ...
- SQLSERVER复制优化之一《改变包大小》
SQLSERVER复制优化之一<改变包大小> 自从搭了复制之后以为可以安枕无忧了,谁不知问题接踵而来 这次遇到的问题是丢包,不知道情况的读者可以先看一下我之前写的一篇<SQLSERV ...
- 个性二维码开源专题<介绍篇>
由C#编写的个性二维码底层,已应用到 码晒客/疯狂创意二维码等项目上,并获得多项软件著作专利. 疯狂创意二维码 疯狂创意二维码是可用于生成风格独特的个性化二维码生成器,用户可以将目标信息输入到二维码生 ...
- 虚拟机Ubuntu14/15启用root用户登录
1. 为root用户设置密码 sudo passwd root 需要先输入一次当前用户的登陆密码,然后输入两次root用户的密码(自己设置). 2. 切换至root用户 sudo -s 3. 编辑登陆 ...