Create an encrypted zip archive with PHP

后端 未结 5 1444
闹比i
闹比i 2020-11-29 05:05

I am searching for a way to encrypt a .txt file into a zip, but in a secure password protected way. My goal is to email this file to me, without anyone being able to read th

5条回答
  •  甜味超标
    2020-11-29 05:48

    More and more tools are supporting AES-encrypted ZIP files. It works, it's secure.

    EDIT2: You can use DotNetZip from PHP to dynamically generate AES-encrypted zip archives from PHP. DotNetZip is a .NET library that is designed for .NET languages (C#, VB, etc). It runs only on Windows :(. But DotNetZip does AES, and it's free, and it works from PHP.

    This is the code I used. (PHP v5.2.9 on Win32)

    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 '';
        echo '  ';
        echo '  Calling DotNetZip from PHP through COM';
        echo '  ';
        echo '  ';
        echo '';
        echo '

    Whoops!

    ' . "
    \n"; echo '

    The file was not successfully generated.

    '; echo ''; echo ''; } } catch (Exception $e) { echo ''; echo ' '; echo ' Calling DotNetZip from PHP through COM'; echo ' '; echo ' '; echo ''; echo '

    Whoops!

    ' . "
    \n"; echo '

    The file was not successfully generated.

    '; echo '

    Caught exception: ', $e->getMessage(), '

    ', "\n"; echo '
    ';
        echo $e->getTraceAsString(), "\n";
        echo '
    '; echo ''; echo ''; } ?>

    I had to modify DotNetZip to make it work with PHP: I had to make the Name property read/write, and I had to make it COM-callable. This change is first available in the v1.8.2.3 release.

提交回复
热议问题