phpmyadmin里面有个zip.lib.php,可以生成zip文件,可是不能加密。那位大哥知道如何加密?(我说的加密是指在解压该文件的时候回要求输入解压密码)。
网上找了好多,基本上都是借用winrar或者zip通过命令行加密,有没有通过php生成加密的zip文件的?现在怎么不能选专家了,还想请唠叨大哥看看呢

解决方案 »

  1.   

    1 现有的操作zip的php提供的函数分为两大类,一是ZipArchive 一是zip,没有发现有对文件加密码的功能.
    2 最简单的方式就是用winrar或者zip通过命令行加密,这个你还不选择.
    3 另外就是分析加密和未加密后的文件,通过文件读写的方式操作,这个可行性不太高,而且对实现技术要求很高.
      

  2.   

    对,我就想采用第3中方法。也对比了不加密和加密后的文件,可是看一个一个的16进制的文件看不懂。就是想知道,如果要加密,加密规则是什么?既然winrar和zip都能加密,我想php也能加密吧
      

  3.   

    加密規則自己定議啊,最簡單的有字符轉換了,比如一個"aaaaa",加密後你想讓它變"bbbbb"。
    <?php
    /**   
      *   Passport   加密函数   
      *   
      *   @param string 等待加密的原字串   
      *   @param string 私有密匙(用于解密和加密)   
      *   
      *   @return string 原字串经过私有密匙加密后的结果   
      */   
      function   passport_encrypt($txt,   $key)   
      {   
        
    //   使用随机数发生器产生   0~32000   的值并   MD5()   
    srand((double)microtime()   *   1000000);   
    $encrypt_key   =   md5(rand(0,   32000));   
        
    //   变量初始化   
    $ctr   =   0;   
    $tmp   =   '';   
        
    //   for   循环,$i   为从   0   开始,到小于   $txt   字串长度的整数   
    for($i   =   0;   $i   <   strlen($txt);   $i++)   
    {   
    //   如果   $ctr   =   $encrypt_key   的长度,则   $ctr   清零   
    $ctr   =   $ctr   ==   strlen($encrypt_key)   ?   0   :   $ctr;   
    //   $tmp   字串在末尾增加两位,其第一位内容为   $encrypt_key   的第   $ctr   位,   
    //   第二位内容为   $txt   的第   $i   位与   $encrypt_key   的   $ctr   位取异或。然后   $ctr   =   $ctr   +   1   
    $tmp   .=   $encrypt_key[$ctr].($txt[$i]   ^   $encrypt_key[$ctr++]);   
    }   
        
    //   返回结果,结果为   passport_key()   函数返回值的   base65   编码结果   
    return   base64_encode(passport_key($tmp,   $key));       
      }   
        
      /**   
      *   Passport   解密函数   
      *   
      *   @param string 加密后的字串   
      *   @param string 私有密匙(用于解密和加密)   
      *   
      *   @return string 字串经过私有密匙解密后的结果   
      */   
      function   passport_decrypt($txt,   $key)   
      {       
    //   $txt   的结果为加密后的字串经过   base64   解码,然后与私有密匙一起,   
    //   经过   passport_key()   函数处理后的返回值   
    $txt   =   passport_key(base64_decode($txt),   $key);   

    //   变量初始化   
    $tmp   =   ''; 
    //   for   循环,$i   为从   0   开始,到小于   $txt   字串长度的整数   
    for   ($i   =   0;   $i   <   strlen($txt);   $i++)   
    {   
    //   $tmp   字串在末尾增加一位,其内容为   $txt   的第   $i   位,   
    //   与   $txt   的第   $i   +   1   位取异或。然后   $i   =   $i   +   1   
    $tmp   .=   $txt[$i]   ^   $txt[++$i];   
    }   

    //   返回   $tmp   的值作为结果   
    return   $tmp;       
      }   
        
      /**   
      *   Passport   密匙处理函数   
      *   
      *   @param string 待加密或待解密的字串   
      *   @param string 私有密匙(用于解密和加密)   
      *   
      *   @return string 处理后的密匙   
      */   
      function   passport_key($txt,   $encrypt_key)   
      {       
    //   将   $encrypt_key   赋为   $encrypt_key   经   md5()   后的值   
    $encrypt_key   =   md5($encrypt_key);   
        
    //   变量初始化   
    $ctr   =   0;   
    $tmp   =   '';   
        
    //   for   循环,$i   为从   0   开始,到小于   $txt   字串长度的整数   
    for($i   =   0;   $i   <   strlen($txt);   $i++)   
    {   
    //   如果   $ctr   =   $encrypt_key   的长度,则   $ctr   清零   
    $ctr   =   $ctr   ==   strlen($encrypt_key)   ?   0   :   $ctr;   
    //   $tmp   字串在末尾增加一位,其内容为   $txt   的第   $i   位,   
    //   与   $encrypt_key   的第   $ctr   +   1   位取异或。然后   $ctr   =   $ctr   +   1   
    $tmp   .=   $txt[$i]   ^   $encrypt_key[$ctr++];   
    }   
        
    //   返回   $tmp   的值作为结果   
    return   $tmp;      
      }   
        
      /**   
      *   Passport   信息(数组)编码函数   
      *   
      *   @param array 待编码的数组   
      *   
      *   @return string 数组经编码后的字串   
      */   
      function   passport_encode($array)   
      {       
    //   数组变量初始化   
    $arrayenc   =   array();       
    //   遍历数组   $array,其中   $key   为当前元素的下标,$val   为其对应的值   
    foreach($array   as   $key   =>   $val)   
    {   
    //   $arrayenc   数组增加一个元素,其内容为   "$key=经过   urlencode()   后的   $val   值"   
    $arrayenc[]   =   $key.'='.urlencode($val);   
    }   
        
    //   返回以   "&"   连接的   $arrayenc   的值(implode),例如   $arrayenc   =   array('aa',   'bb',   'cc',   'dd'),   
    //   则   implode('&',   $arrayenc)   后的结果为   ”aa&bb&cc&dd"   
    return   implode('&',   $arrayenc);   
        
    }
    ?>以上這段是抄的,希望對你有用
      

  4.   

    找了,有个DotNetZip,支持加密。可是需要.net环境支持
      

  5.   

    我推荐用命令行的方式,否则,把ZIP的加密写成PHP的扩展库文件,进行加载,呵呵
      

  6.   

    继续搜索
    如果知道加密的zip文件格式定义,是不是就可以做到呢??
      

  7.   

    有空看下这个吧
    http://www.w3school.com.cn/php/php_ref_zip.asp
      

  8.   

    Creating ZIP files with PHP
    As part of my previously mentioned OpenVPN CA I want to deliver keys, certs and config files to users in a single zip file that they can just extract onto their computers. PHP's own ZIP File Functions only supports reading zip files and not making them.Some Googling discovered an article by John Coggeshall that can create zip files. It does this by creating the binary data on the fly and can output the zip files directly to the browser from memory or by writing it to disk.I had some troubles getting hold of a usable version of this code since all these PHP code collection sites have this annoying habit of only showing the syntax highlighted versions of the code rather than give a download link. Eventually got one though and I figured I'll host a mirror of it here to help people out.Using it is very simple, this is a quick sample that will create a ZIP file and add one directory and one file into then send it directly to the client.<?
            require ("incl/zipfile.inc.php");        $zipfile = new zipfile();
            
            $filedata = implode("", file("incl/zipfile.inc.php"));        $zipfile->add_dir("incl/");
            $zipfile->add_file($filedata, "incl/zipfile.inc.php");        header("Content-type: application/octet-stream");
            header("Content-disposition: attachment; filename=zipfile.zip");
            echo $zipfile->file();
    ?>
      

  9.   

    http://www.devco.net/code/zipfile.inc.txt<?php  
    /* 
    Zip file creation class 
    makes zip files on the fly... use the functions add_dir() and add_file() to build the zip file; 
    see example code below by Eric Mueller 
    http://www.themepark.com v1.1 9-20-01 
      - added comments to example v1.0 2-5-01 initial version with: 
      - class appearance 
      - add_file() and file() methods 
      - gzcompress() output hacking 
    by Denis O.Philippov, [email protected], http://www.atlant.ru */  // official ZIP file format: http://www.pkware.com/appnote.txt class zipfile   
    {       var $datasec = array(); // array to store compressed data 
        var $ctrl_dir = array(); // central directory    
        var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record 
        var $old_offset = 0;      function add_dir($name)        // adds "directory" to archive - do this before putting any files in directory! 
        // $name - name of directory... like this: "path/" 
        // ...then you can add files using add_file with names like "path/file.txt" 
        {   
            $name = str_replace("\\", "/", $name);           $fr = "\x50\x4b\x03\x04";  
            $fr .= "\x0a\x00";    // ver needed to extract 
            $fr .= "\x00\x00";    // gen purpose bit flag 
            $fr .= "\x00\x00";    // compression method 
            $fr .= "\x00\x00\x00\x00"; // last mod time and date         $fr .= pack("V",0); // crc32 
            $fr .= pack("V",0); //compressed filesize 
            $fr .= pack("V",0); //uncompressed filesize 
            $fr .= pack("v", strlen($name) ); //length of pathname 
            $fr .= pack("v", 0 ); //extra field length 
            $fr .= $name;   
            // end of "local file header" segment         // no "file data" segment for path         // "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));          // ext. file attributes mirrors MS-DOS directory attr byte, detailed 
            // at http://support.microsoft.com/support/kb/articles/Q125/0/19.asp         // now add to central record 
            $cdrec = "\x50\x4b\x01\x02";  
            $cdrec .="\x00\x00";    // version made by 
            $cdrec .="\x0a\x00";    // version needed to extract 
            $cdrec .="\x00\x00";    // gen purpose bit flag 
            $cdrec .="\x00\x00";    // compression method 
            $cdrec .="\x00\x00\x00\x00"; // last mod time & date 
            $cdrec .= pack("V",0); // crc32 
            $cdrec .= pack("V",0); //compressed filesize 
            $cdrec .= pack("V",0); //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 
            $ext = "\x00\x00\x10\x00";  
            $ext = "\xff\xff\xff\xff";   
            $cdrec .= pack("V", 16 ); //external file attributes  - 'directory' 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 array 
            $this -> ctrl_dir[] = $cdrec;             
        }  
        function add_file($data, $name)        // adds "file" to archive    
        // $data - file contents 
        // $name - name of file in archive. Add path if your want     {   
            $name = str_replace("\\", "/", $name);   
            //$name = str_replace("\\", "\\\\", $name);         $fr = "\x50\x4b\x03\x04";  
            $fr .= "\x14\x00";    // ver needed to extract 
            $fr .= "\x00\x00";    // gen purpose bit flag 
            $fr .= "\x08\x00";    // compression method 
            $fr .= "\x00\x00\x00\x00"; // last mod time and date         $unc_len = strlen($data);   
            $crc = crc32($data);   
            $zdata = gzcompress($data);   
            $zdata = substr( substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug 
            $c_len = strlen($zdata);   
            $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;   
            // end of "local file header" segment 
              
            // "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 = "\x50\x4b\x01\x02";  
            $cdrec .="\x00\x00";    // version made by 
            $cdrec .="\x14\x00";    // version needed to extract 
            $cdrec .="\x00\x00";    // gen purpose bit flag 
            $cdrec .="\x08\x00";    // compression method 
            $cdrec .="\x00\x00\x00\x00"; // 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 
    //        echo "old offset is ".$this->old_offset.", new offset is $new_offset<br>"; 
            $this -> old_offset = $new_offset;          $cdrec .= $name;   
            // optional extra field, file comment goes here 
            // save to central directory 
            $this -> ctrl_dir[] = $cdrec;   
        }      function file() { // dump out 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 
                "\x00\x00";                             // .zip file comment length 
        }  
    }   ?>
      

  10.   

    目前看来只能使用系统命令了<?php
    $filename = "filename.zip";
    $pdf = "document.pdf";
    echo system("zip -P 1234 -j $filename \"$pdf\"");
    ?>
      

  11.   

    不多这个网站也有很多http://www.111cn.cn
      

  12.   

    1 现有的操作zip的php提供的函数分为两大类,一是ZipArchive 一是zip,没有发现有对文件加密码的功能.
    2 最简单的方式就是用winrar或者zip通过命令行加密,这个你还不选择.
    3 另外就是分析加密和未加密后的文件,通过文件读写的方式操作,这个可行性不太高,而且对实现技术要求很高.
      

  13.   

    以前倒没想过这个问题,突然有一个想法:可以试试用php调用7-zip命令行工具。也不知道行不行。
      

  14.   

    给你一个加密算法http://www.111cn.net/phper/22/33137.htm
      

  15.   

    从某个版本找到了rar这个扩展,不过不稳定:(
    高版本的没有这个扩展了。