使用PHP创建加密的zip存档

时间:2009-03-14 16:24:56

标签: php encryption zip

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

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

5 个答案:

答案 0 :(得分:17)

  

注意:此答案建议使用已知的加密方法   不安全,即使有良好的密码。请see link from comments   和the Winzip QA on AES。支持in-php AES zip加密   以php 7.2(和libzip 1.2.0)到达,这意味着这一点   答案很快就会过时。在此之前see this answer for how to call out to 7z instead of the zip command, which supports winzip's AES encryption

您可以使用:

<?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

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

答案 1 :(得分:11)

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

但您也可以按索引而不是名称设置加密方法,并且可以基于每个文件设置每个密码...以及使用newly supported encryption options.

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

<?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才能运行这段代码......希望这个注释能够在这个答案“昙花一现”中迸发

答案 2 :(得分:4)

虽然PHP是一种成熟的语言,但是没有足够的方法(不包括自定义扩展或类似的东西)来实现纯PHP的简单任务。
您还可以做的是等到PHP 7.2可用于生产(cuz ZipArchive::setEncryptionName已实施(感谢Pierre和Remi))。
但是,在此之前,您还可以尝试将php_zip&gt; = 1.14.0移植到PHP&lt; 7.2,但目前没有可用的已编译二进制文件,所以你必须自己编译并尝试它是否可行(我相信它是)。
附:我会尝试一下,但现在我的电脑上没有VS2015 +。

答案 3 :(得分:1)

越来越多的工具支持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 release中提供。

答案 4 :(得分:-2)

这就是我做的。 这是一个excel,但它是一样的。

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

  • 通过网络访问retrieve.php?codename = [代号]

  • 让php检查代号是否正确。 (甚至可能是它的年龄)。
  • 从需要发送给您的数据中创建内存中的excel / textfile。
  • 通过使用代号添加本地密码(只有您知道)来创建加密代码。(我说添加但也可以混合或xor-ed或substr ...)
  • 使用创建的加密代码即时加密。
  • 从db或config文件中删除代号。
  • 将此加密文档以邮件形式返回(压缩或非大小)。
  • 也许在邮件中添加两个来自代号的第一个字符,以了解要使用的本地完整代号。

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

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

相关问题