使用 PHP 创建加密的 zip 存档

问题描述 投票:0回答:5

我正在寻找一种将 .txt 文件加密为 zip 的方法,但采用安全密码保护的方式。我的目标是通过电子邮件将此文件发送给我,而任何人都无法阅读附件的内容。

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

php encryption zip
5个回答
24
投票

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

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

<?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";
}
?>

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

此示例练习了这些更复杂的选项。

<?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 才能运行这个代码...希望这个注释会在这个答案“很快”上变得粗糙


21
投票

注意:这个答案推荐了一种已知的加密方法 不安全,即使有好的密码。请查看评论中的链接 以及AES 上的 Winzip QA。支持 PHP 内 AES zip 加密 附带 php 7.2 (和 libzip 1.2.0),这意味着 答案也很快就会过时。在那之前请参阅此答案以了解如何进行 调用 7z 而不是 zip 命令,它支持 winzip AES 加密

您可以使用这个:

<?php echo system('zip -P pass file.zip file.txt'); ?>

其中pass是密码,file.txt将被压缩成file.zip。这应该适用于 Windows 和 Linux,您只需要获取适用于 Windows 的免费版本 zip ( http://www.info-zip.org/Zip.html#Win32 )

这种安全性可以通过暴力攻击、字典攻击等来破解。但这并不那么容易,特别是如果你选择了一个长且难以猜测的密码。


4
投票

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

您还可以做的是等到PHP 7.2可用于生产(因为ZipArchive::setEncryptionName已实现(感谢 Pierre 和 Remi))。

但是,在那之前您也可以尝试将 php_zip >= 1.14.0 移植到 PHP < 7.2, but there is currently no compiled binaries available, so you have to compile it yourself and try if it is possible at all (I believe it is).

PS 我想尝试一下,但现在我的电脑上没有 VS2015+。


0
投票

越来越多的工具支持 AES 加密的 ZIP 文件。它有效,而且安全。

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

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

<?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 属性读/写,并且必须使其可通过 COM 调用。此更改首先在 v1.8.2.3 版本中可用。


-4
投票

我就是这样做的。 虽然是用excel,但是都是一样的。

  • 让 php 创建一个随机代号。
  • 将代码名保存在数据库或要包含在retrieve.php中的文件中。
  • 将代号邮寄给自己。

  • 通过网页访问retrieve.php?codename=[codename]

  • 让 php 检查代号是否正确。 (也许甚至是年龄)。
  • 根据需要发送给您的数据在内存中创建 Excel/文本文件。
  • 通过添加本地密码(只有你知道)和代号来创建加密代码。(我说添加,但也可以混合或异或或子串...)
  • 使用创建的加密代码即时加密。
  • 从数据库或配置文件中删除代号。
  • 通过邮件返回此加密文档(压缩或不压缩)。
  • 也许可以在邮件中添加代号的前两个字符,以了解要使用的本地完整代号。

  • 使用本地解密脚本对下载的文件进行解码。使用相同的代号/密码算法创建解密密钥。

我使用 gibberish-aes-php。 (https://github.com/ivantcholakov/gibberish-aes-php
因为这样我就可以在客户端解码器上使用 https://github.com/mdp/gibberish-aes 作为 javascript(对于我想在浏览器中快速查看的内容)。

© www.soinside.com 2019 - 2024. All rights reserved.