php数据压缩加密,php文件加密解密

php怎样实现对zip文件的加密和解密

使用PHPZip类就可以解决的。以下是网上找到的例子。

在兰陵等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都网站建设、外贸营销网站建设 网站设计制作按需网站设计,公司网站建设,企业网站建设,高端网站设计,网络营销推广,外贸网站建设,兰陵网站建设费用合理。

$zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt");

$z = new PHPZip();

//$randomstr = random(8);

$zipfile = TEMP."/photocome_".$groupid.".zip";

$z-Zip($zipfiles, $zipfile);

?php

# PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18

#     (Changed: 2003-03-01)

# Makes zip archive

#

# Based on "Zip file creation class", uses zLib

#

#

class PHPZip

{

function Zip($dir, $zipfilename)

{

if (@function_exists('gzcompress'))

{    

$curdir = getcwd();

if (is_array($dir)) 

{

$filelist = $dir;

}

else 

{

$filelist = $this - GetFileList($dir);

}

if ((!empty($dir))(!is_array($dir))(file_exists($dir))) chdir($dir);

else chdir($curdir);

if (count($filelist)0)

{

foreach($filelist as $filename)

{

if (is_file($filename))

{

$fd = fopen ($filename, "r");

$content = fread ($fd, filesize ($filename));

fclose ($fd);

if (is_array($dir)) $filename = basename($filename);

$this - addFile($content, $filename);

}

}

$out = $this - file();

chdir($curdir);

$fp = fopen($zipfilename, "w");

fwrite($fp, $out, strlen($out));

fclose($fp);

}

return 1;

else return 0;

}

function GetFileList($dir)

{

if (file_exists($dir))

{

$args = func_get_args();

$pref = $args[1];

$dh = opendir($dir);

while($files = readdir($dh))

{

if (($files!=".")($files!="..")) 

{

if (is_dir($dir.$files)) 

{

$curdir = getcwd();

chdir($dir.$files);

$file = array_merge($file, $this - GetFileList("", "$pref$files/"));

chdir($curdir);

}

else $file[]=$pref.$files;

}

}

closedir($dh);

}

return $file;

}

var $datasec      = array();

var $ctrl_dir     = array();

var $eof_ctrl_dir = "x50x4bx05x06x00x00x00x00";

var $old_offset   = 0;

/**

* Converts an Unix timestamp to a four byte DOS date and time format (date

* in high two bytes, time in low two bytes allowing magnitude comparison).

*

* @param  integer  the current Unix timestamp

*

* @return integer  the current date in a four byte DOS format

*

* @access private

*/

function unix2DosTime($unixtime = 0) {

$timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);

if ($timearray['year']  1980) {

$timearray['year']    = 1980;

$timearray['mon']     = 1;

$timearray['mday']    = 1;

$timearray['hours']   = 0;

$timearray['minutes'] = 0;

$timearray['seconds'] = 0;

} // end if

return (($timearray['year'] - 1980)  25) | ($timearray['mon']  21) | ($timearray['mday']  16) |

($timearray['hours']  11) | ($timearray['minutes']  5) | ($timearray['seconds']  1);

} // end of the 'unix2DosTime()' method

/**

* Adds "file" to archive

*

* @param  string   file contents

* @param  string   name of the file in the archive (may contains the path)

* @param  integer  the current timestamp

*

* @access public

*/

function addFile($data, $name, $time = 0)

{

$name     = str_replace('', '/', $name);

$dtime    = dechex($this-unix2DosTime($time));

$hexdtime = 'x' . $dtime[6] . $dtime[7]

. 'x' . $dtime[4] . $dtime[5]

. 'x' . $dtime[2] . $dtime[3]

. 'x' . $dtime[0] . $dtime[1];

eval('$hexdtime = "' . $hexdtime . '";');

$fr   = "x50x4bx03x04";

$fr   .= "x14x00";            // ver needed to extract

$fr   .= "x00x00";            // gen purpose bit flag

$fr   .= "x08x00";            // compression method

$fr   .= $hexdtime;             // last mod time and date

// "local file header" segment

$unc_len = strlen($data);

$crc     = crc32($data);

$zdata   = gzcompress($data);

$c_len   = strlen($zdata);

$zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug

$fr      .= pack('V', $crc);             // crc32

$fr      .= pack('V', $c_len);           // compressed filesize

$fr      .= pack('V', $unc_len);         // uncompressed filesize

$fr      .= pack('v', strlen($name));    // length of filename

$fr      .= pack('v', 0);                // extra field length

$fr      .= $name;

// "file data" segment

$fr .= $zdata;

// "data descriptor" segment (optional but necessary if archive is not

// served as file)

$fr .= pack('V', $crc);                 // crc32

$fr .= pack('V', $c_len);               // compressed filesize

$fr .= pack('V', $unc_len);             // uncompressed filesize

// add this entry to array

$this - datasec[] = $fr;

$new_offset        = strlen(implode('', $this-datasec));

// now add to central directory record

$cdrec = "x50x4bx01x02";

$cdrec .= "x00x00";                // version made by

$cdrec .= "x14x00";                // version needed to extract

$cdrec .= "x00x00";                // gen purpose bit flag

$cdrec .= "x08x00";                // compression method

$cdrec .= $hexdtime;                 // last mod time  date

$cdrec .= pack('V', $crc);           // crc32

$cdrec .= pack('V', $c_len);         // compressed filesize

$cdrec .= pack('V', $unc_len);       // uncompressed filesize

$cdrec .= pack('v', strlen($name) ); // length of filename

$cdrec .= pack('v', 0 );             // extra field length

$cdrec .= pack('v', 0 );             // file comment length

$cdrec .= pack('v', 0 );             // disk number start

$cdrec .= pack('v', 0 );             // internal file attributes

$cdrec .= pack('V', 32 );            // external file attributes - 'archive' bit set

$cdrec .= pack('V', $this - old_offset ); // relative offset of local header

$this - old_offset = $new_offset;

$cdrec .= $name;

// optional extra field, file comment goes here

// save to central directory

$this - ctrl_dir[] = $cdrec;

} // end of the 'addFile()' method

/**

* Dumps out file

*

* @return  string  the zipped file

*

* @access public

*/

function file()

{

$data    = implode('', $this - datasec);

$ctrldir = implode('', $this - ctrl_dir);

return

$data .

$ctrldir .

$this - eof_ctrl_dir .

pack('v', sizeof($this - ctrl_dir)) .  // total # of entries "on this disk"

pack('v', sizeof($this - ctrl_dir)) .  // total # of entries overall

pack('V', strlen($ctrldir)) .           // size of central dir

pack('V', strlen($data)) .              // offset to start of central dir

"x00x00";                             // .zip file comment length

} // end of the 'file()' method

} // end of the 'PHPZip' class

?

如何对PHP文件进行加密

php加密平台自开通以来已加密了670367个php文件,总数据大小5823.04 MB2015-04-18更新:PHP在线加密Beta版支持命名空间加密,即php源码中有namespace的文件加密。2015-01-13维护:修复PHP5.5及以上php版本更好支持php加密(免费版已不更新,不修复)

php授权用户动态简化,只记录授权变化状态2014-12-09新增自定义授权错误信息提示(以前为白屏)2014-06-22新增支持heredoc和nowdoc语法结构的字符串2014-03-24修复部份代码中使用__FILE__不兼容的BUG2013-09-02修改:php免费加密后版权信息中增加中文声明2013-08-01新增功能:授权版可锁定php文件加密的文件名,php文件加密后不允许更改文件名。此功能配合锁定域名功能效果更佳。

修复:部份环境中获取不到服务器IP地址2013-06-20维护:常规更新维护,PHP加密代码小调整。2013-05-12修改:重写授权版的PHP加密算法,提高对代码的保护。2013-04-11调整:发现有个别用户使用我们免费的php加密平台对php源码加密后作为非法用途,因此把免费版对上传的php文件大小调整为50K。2013-03-11修改:为了加强付费授权版的使用安全,把免费版和付费授权版的算法分开处理。以后升级过程中主要针对付费授权版进行升级,免费版只做BUG修复处理。2013-02-01修改:免费版的版权信息中不再显示内核更新日期。

修改:编码加强混淆程度2013-01-07修改:php加密后的代码中去除“PHPJM_”标志性字符。

新增:先对php源码压缩后再加密,减小加密后的php文件大小

修复:php源代码中的php标签只有?而没有?时的BUG2012-12-11新增功能:授权版可锁定在某个日期段内运行,可将您的代码以试用方式进行发布。

修改:不再限制加密后的文件以.php为扩展名运行,支持以任何扩展名保存加密后的文件。

修复:自定义版权信息乱码BUG2012-11-14PHP在线加密平台上线2012-11-06完成PHP加密核心算法代码。

PHP 实现文件压缩解压zip格式

在php中,有时我们需要使用到压缩文件操作,压缩文件可以节省磁盘空间;且压缩文件更小,便于网络传输,效率高,下面我们就来了解php的压缩解压相关操作

在PHP中有一个ZipArchive类,专门用于文件的压缩解压相关操作

在ZipArchive类中主要使用到了如下方法:

第一个参数:要打开的压缩包文件

第二个参数:

ZIPARCHIVE::OVERWRITE 总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉

ZIPARCHIVE::CREATE 如果指定的zip文件不存在,则新建一个

ZIPARCHIVE::EXCL 如果指定的zip文件存在,则会报错

ZIPARCHIVE::CHECKCONS 对指定的zip执行其他一致性测试

上面就是ZipArchive的一些常用方法,下面来一些简单示例

zip密码 php

用PHP的zip模块进行压缩加密.

开始

$zipArc = new \ZipArchive();if ($zipArc-open('/home/test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {    //设置密码 注意此处不是加密,仅仅是设置密码

if (!$zipArc-setPassword('password')) {        throw new RuntimeException('Set password failed');

}    //往压缩包内添加文件

$zipArc-addFile('/home/test.png', '1/test.png');    //加密文件 此处文件名及路径是压缩包内的

if (!$zipArc-setEncryptionName('1/test.png', ZipArchive::EM_AES_256)) {        throw new RuntimeException('Set encryption failed');

}

}

$zipArc-close();

注意事项

1 PHP7.2以下不支持加密

php7.2 以下是不支持加密的,我们看一下php官方文档中的解释

从PHP 7.2.0和libzip 1.2.0开始,密码用于解压缩归档,也是ZipArchive :: setEncryptionName() 和ZipArchive :: setEncryptionIndex()的默认密码。

以前,此功能仅设置用于解压缩存档的密码; 它没有将非密码保护的ZipArchive 变成受密码保护的ZipArchive。

也就是说php7.2之前,setPassword('password')这个方法仅仅是设置setEncryptionName()和setEncryptionIndex()的默认密码,却没有进行加密操作!!!,就问你坑不坑!!

2 方法找不到

提示没有setEncryptionName和setEncryptionIndex方法时,请编译时zip模块时用以下参数

--with-libzip

--enable-zip

3 目录结构问题

待压缩的文件目录,比说说是/home/test/a.png

压缩后,你发现压缩包内的目录结构是/home/test/a.png,

也就是说压缩包原封不动的保持了原来文件的目录.可是我们想自定义压缩包目录怎么办呢?

$a = '/home/test.png';

$b = '1/test.png';//$a是待添加的文件路径  $b是压缩包内的路径$zipArc-addFile($a, $b);

名称栏目:php数据压缩加密,php文件加密解密
当前路径:https://www.cdcxhl.com/article30/phcipo.html

成都网站建设公司_创新互联,为您提供网站设计网站内链微信小程序网站策划网站排名用户体验

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设