首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用PHP创建加密的zip压缩文件

用PHP创建加密的zip压缩文件
EN

Stack Overflow用户
提问于 2009-03-14 16:24:56
回答 3查看 40K关注 0票数 26

我正在寻找一种方法来加密压缩成一个.txt文件,但在一个安全的密码保护的方式。我的目标是在没有任何人能够阅读附件内容的情况下,将此文件通过电子邮件发送给我。

有没有人知道一种简单的,最重要的,安全的方法来实现这一点?我可以创建zip归档,但我不知道如何加密它们,或者这是多么安全。

EN

回答 3

Stack Overflow用户

发布于 2017-12-01 16:49:15

从php7.2(一个小时前发布)开始,正确的方法是使用ZipArchive原生php代码中包含的附加功能。(感谢abraham-tugalov指出这一变化即将到来)

现在简单的答案看起来像这样:

代码语言:javascript
复制
<?php
$zip = new ZipArchive();
if ($zip->open('test.zip', ZipArchive::CREATE) === TRUE) {
    $zip->setPassword('secret_used_as_default_for_all_files'); //set default password

    $zip->addFile('thing1.txt'); //add file
    $zip->setEncryptionName('thing1.txt', ZipArchive::EM_AES_256); //encrypt it

    $zip->addFile('thing2.txt'); //add file
    $zip->setEncryptionName('thing2.txt', ZipArchive::EM_AES_256); //encrypt it

    $zip->close();

    echo "Added thing1 and thing2 with the same password\n";
} else {
    echo "KO\n";
}
?>

但是你也可以通过索引而不是名称来设置加密方法,并且你可以在每个文件的基础上设置每个密码...以及指定较弱的加密选项,使用newly supported encryption options.

此示例执行这些更复杂的选项。

代码语言:javascript
复制
<?php
$zip = new ZipArchive();
if ($zip->open('test.zip', ZipArchive::CREATE) === TRUE) { 
     //being here means that we were able to create the file..

     //setting this means that we do not need to pass in a password to every file, this will be the default
    $zip->addFile('thing3.txt');

    //$zip->setEncryptionName('thing3.txt', ZipArchive::EM_AES_128);
    //$zip->setEncryptionName('thing3.txt', ZipArchive::EM_AES_192);
    //you should just use ZipArchive::EM_AES_256 unless you have super-good reason why not. 
    $zip->setEncryptionName('thing3.txt', ZipArchive::EM_AES_256, 'password_for_thing3');

     $zip->addFile('thing4.txt');
    //or you can also use the index (starting at 0) of the file...
    //which means the following line should do the same thing...
    //but just referencing the text.txt by index instead of name..
    //$zip->setEncryptionIndex(1, ZipArchive::EM_AES_256, 'password_for_thing_4'); //encrypt thing4, using its index instead of its name...

    $zip->close();
    echo "Added thing3 and thing4 with two different passwords\n";
} else {
    echo "KO\n";
}
?>

启用对zip加密的底层支持是因为libzip 1.2.0引入了对加密的支持。所以你需要有php 7.2和libzip 7.2才能运行这段代码...希望这篇笔记能“很快”地回答这个问题。

票数 19
EN

Stack Overflow用户

发布于 2017-07-20 06:16:41

虽然PHP是一种成熟的语言,但没有足够的方法(不包括自定义扩展或类似的东西)来用纯PHP完成这样简单的任务。

您还可以做的是等待PHP 7.2可用于生产(因为实现了ZipArchive::setEncryptionName (多亏了Pierre和Remi))。

但是,在此之前,您也可以尝试将php_zip >= 1.14.0移植到PHP < 7.2,但目前还没有编译的二进制文件可用,因此您必须自己编译它,并尝试是否可行(我相信是可行的)。

另外,我会尝试一下,但现在我的电脑上没有VS2015+。

票数 4
EN

Stack Overflow用户

发布于 2009-03-22 10:17:02

越来越多的工具支持AES加密的ZIP文件。它起作用了,它很安全。

EDIT2:您可以在PHP中使用DotNetZip来动态地从PHP中生成AES加密的zip存档。DotNetZip是为.NET语言(C#、VB等)设计的.NET库。它只能在Windows上运行:(.但是DotNetZip做的是AES,而且是免费的,而且它可以在PHP上工作。

这是我使用的代码。(Win32上的PHPV5.2.9)

代码语言:javascript
复制
<?php
try
{
  $fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
  $zipOutput = "c:\\temp\\" . $fname;
  $zipfact = new COM("Ionic.Zip.ZipFile");
  $zip->Name = $zipOutput;
  $dirToZip= "c:\\temp\\psh";
  # Encryption:  3 => 256-bit AES.  
  #     2 => 128-bit AES.  
  #     1 => PKZIP (Weak).  
  #     0 => None
  $zip->Encryption = 3;
  $zip->Password = "AES-Encryption-Is-Secure";
  $zip->AddDirectory($dirToZip);
  $zip->Save();
  $zip->Dispose();

  if (file_exists($zipOutput))
  {
    header('Cache-Control: no-cache, must-revalidate');
    header('Content-Type: application/x-zip'); 
    header('Content-Disposition: attachment; filename=' . $fname);
    header('Content-Length: ' . filesize($zipOutput));
    readfile($zipOutput);
    unlink($zipOutput);
  }
  else 
  {
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '</body>';
    echo '</html>';
  } 
}
catch (Exception $e) 
{
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '<p>Caught exception: ',  $e->getMessage(), '</p>', "\n";
    echo '<pre>';
    echo $e->getTraceAsString(), "\n";
    echo '</pre>';
    echo '</body>';
    echo '</html>';
}

?>

我必须修改DotNetZip以使其与PHP一起工作:我必须使Name属性可读/写,并且必须使其可被调用。此更改首先在v1.8.2.3 release中可用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/646195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档