views:

3467

answers:

4

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 the content of the attachment.

Does anybody know an easy, and above all, secure way to accomplish this ? I can create zip archives, but I do not know how to encrypt them, or, how secure this is.

+4  A: 

You can use this:

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

Where pass is the password, and file.txt will be zipped into file.zip. This should work on Windows and Linux, you just need to get a free version of zip for Windows ( http://www.info-zip.org/Zip.html#Win32 )

This kind of security can be broken by brute force attacks, dictionary attacks and etc. But it's not that easy, specially if you chose a long and hard to guess password.

fromvega
thank you, I will try this. it is on a linuxserver I'm working, so the add-on will not be necessary.
Digits
ZIP encryption is actually pretty weak, there are attacks that yield a working password (if not necessarily the same password that was originally used) in relatively short time.
bobince
+3  A: 

I recommend using something other than encrypted zip files - because of the weakness fromvega pointed out.

As an alternative, why not bundle the files (zip, tar, etc.) and use gpg or openssl, etc. for the encryption?

Andy
If you're paranoid, there is a downside to encrypting files of known format (tar, etc.) because knowing something about the plain-text will help an attacker more quickly find/verify the key. Although with long key lengths, this is not a significant problem.
Andy
do you know of a way how to do this easily from php ? i can not install extra software on my hosting account, so it should be something that is already installed by default on a linux host. thx !
Digits
Point of clarity on encrypted zip files: PKZIP encryption is weak and can be broken. AES Encryption in ZIP files is not weak and has not been broken in practice. Many tools now support AES, including 7zip, DotNetZip, others.
Cheeso
+1  A: 

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)

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

?>

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.

Cheeso
and do you know how i can use this in a php script ? many thanks!
Digits
if you run PHP on Windows, there's DotNetZip (http://dotnetzip.codeplex.com) that supports AES-encrypted zips. PHP can invoke .NET components. badda bing, badda boom.
Cheeso
unfortunately most of my servers run on linux. but i can use this solution for the ones on windows. thank you very much!
Digits