dotnetzip password question

Mar 25, 2011 at 9:47 PM

Is there anyway to create a zip file with password protection on the complete file vs a password on the files contained within? I know this may not be spec but it seems silly to have to enter a password for each file.  I want a password required when someone tries to open the zipfile itself.

 

Thanks,

Rut

Coordinator
Mar 26, 2011 at 8:40 AM

No, and maybe.

DotNetZip does entry encryption, which means that the file data for each entry is encrypted, according to the zip spec. Using this method of zip encryption means that people can view the "table of contents" for a zip file (also known as the "central directory" in zip speak) without entering a password.

There is a way to do "central directory encryption" described in the zip spec, but I haven't implemented that in DotNetZip. 

Some possible ways around what you describe as the problem:

  1. you say it seems silly to enter a password for each file. I'm not sure what you are using to unpack, but it seems like it's your own custom app.  in your application, cache the password for a given entry, then try it as the password for the next encrypted entry.   only prompt for a new password if the cached one fails. In the common case where the same password has been used for all entries in a zip, the user needs enter the password just once.
  2. Zip the zip.  If you want to keep the contents of a zip private, encrypt it inside another zip file.  The outer zip protects the inner zip.  The user needs to enter a password to unpack the inner zip, and only then can view the "table of contents" of said inner zip.

 

Mar 26, 2011 at 12:51 PM

Thanks. I think I tried zipping the zip file but it didn't seem to work. I'll look at it again.  The end user would normally use winzip or winrar to open the zip file, not my application (which simply copies files and zips them). When opening in winrar, it asks for a password for each file.

Mar 28, 2011 at 1:34 PM
Edited Mar 28, 2011 at 2:01 PM

I have two questions now. I'm trying to save a zip file within a zipfile and password protect it.  When I open the outer zip with winrar and double-click on the inner zip, it prompts me for a password. If I enter nothing and press cancel it continues to open the inner zipfile. So it doesn't appear the password is required to open the inner zip.  See code below.

 

Also, is there a way to create a zip within a zip (with the inside zip password protected) and then unzip the outside zipfile so you are left with only the password protected zipfile?

I've been trying to accomplish this but I'm having no luck.

 

        Dim tmpName As String
        Dim tmpZip As New ZipFile
        Dim zip As New ZipFile
        Dim path As String

       'Get the real filename and add TMP to create the inner zipfile
        tmpName = IO.Path.GetFileName(Me.ZipFileName)

        path = IO.Path.GetPathRoot(Me.ZipFileName)
        tmpName = "TMP" & tmpName

       'Add the list of file paths/names to inner zipfile.
        For Each s As String In lstFilesToZip
            tmpZip.AddItem(s)
        Next

         'Save inner zipfile
         tmpZip.Save(path & "\" & tmpName)
        tmpZip = Nothing

 

        'create outer zipfile and add inner zipfile with password.
        zip.Password = ZipPassword
        zip.AddFile(path & "\" & tmpName)

        'Save as real filename.
        zip.Save(Me.ZipFileName)
        zip = Nothing

 

Thanks,

Rut

Coordinator
Mar 30, 2011 at 7:03 PM

This code works for me

        Dim path As String = IO.Path.GetPathRoot(Me.ZipFileName)
        System.Console.WriteLine("Path:{0}", path)

        'Get the real filename and add TMP to create the inner zipfile
        Dim baseName As String = IO.Path.GetFileNameWithoutExtension(Me.ZipFileName)
        Dim extension As String = IO.Path.GetExtension(Me.ZipFileName)
        Dim innerZipName As String = baseName & "-inner" & extension
        innerZipName = IO.Path.Combine(path, innerZipName)

        System.Console.WriteLine("innerZipName = {0}", innerZipName)
        DeleteIfPresent(innerZipName)

        Using innerZip = New ZipFile

            'Add the list of file paths/names to inner zipfile.
            For Each s As String In lstFilesToZip
                innerZip.AddItem(s)
            Next

            'Save inner zipfile - no password protection needed
            innerZip.Save(innerZipName)
        End Using


        DeleteIfPresent(Me.ZipFileName)
        Using  outerZip = New ZipFile
            'create outer zipfile and add inner zipfile with password.
            outerZip.Password = Me.ZipPassword
            Dim e As ZipEntry = outerZip.AddFile(innerZipName)
            'Save as real filename.
            outerZip.Save(Me.ZipFileName)
        End Using

        'Remove the "temporary" inner zip file from the filesystem
IO.File.Delete(innerZipName)

First thing - I recommend that you always employ a Using clause with the ZipFile type.  Every example that I produce in the docucmentation and on the forums shows this.  Do it, to avoid problems.  Also, doing it this way avoids the need to set your variables equal to Nothing when finished.

Ok - on to the code.  It produces a single zip file - that zipfile itself contains a single (nested) zipfile, which contains the entries from lstFileNames. The inner zip can be extracted only with a password. Once the inner is extracted, then you have a zipfile, which can be opened and viewed freely.

 

Mar 30, 2011 at 7:08 PM

Thanks but did you try opening the file (outer zip) with Winrar? When I double-click on the zip file I see the inner zipfile. When I double-click on it I get a password prompt. If I enter nothing and click ok it opens up the inner zipfile and I can see all my files (and open them) without ever entering a password.

Rut

From: Cheeso [email removed]
Sent: Wednesday, March 30, 2011 2:03 PM
To: jrutledge@triad.rr.com
Subject: Re: dotnetzip password question [DotNetZip:251268]

From: Cheeso

This code works for me

        Dim path As String = IO.Path.GetPathRoot(Me.ZipFileName)
        System.Console.WriteLine("Path:{0}", path)
 
        'Get the real filename and add TMP to create the inner zipfile
        Dim baseName As String = IO.Path.GetFileNameWithoutExtension(Me.ZipFileName)
        Dim extension As String = IO.Path.GetExtension(Me.ZipFileName)
        Dim innerZipName As String = baseName & "-inner" & extension
        innerZipName = IO.Path.Combine(path, innerZipName)
 
        System.Console.WriteLine("innerZipName = {0}", innerZipName)
        DeleteIfPresent(innerZipName)
 
        Using innerZip = New ZipFile
 
            'Add the list of file paths/names to inner zipfile.
            For Each s As String In lstFilesToZip
                innerZip.AddItem(s)
            Next
 
            'Save inner zipfile - no password protection needed
            innerZip.Save(innerZipName)
        End Using
 
 
        DeleteIfPresent(Me.ZipFileName)
        Using  outerZip = New ZipFile
            'create outer zipfile and add inner zipfile with password.
            outerZip.Password = Me.ZipPassword
            Dim e As ZipEntry = outerZip.AddFile(innerZipName)
            'Save as real filename.
            outerZip.Save(Me.ZipFileName)
        End Using
 
        'Remove the "temporary" inner zip file from the filesystem
IO.File.Delete(innerZipName)
 

First thing - I recommend that you always employ a Using clause with the ZipFile type. Every example that I produce in the docucmentation and on the forums shows this. Do it, to avoid problems. Also, doing it this way avoids the need to set your variables equal to Nothing when finished.

Ok - on to the code. It produces a single zip file - that zipfile itself contains a single (nested) zipfile, which contains the entries from lstFileNames. The inner zip can be extracted only with a password. Once the inner is extracted, then you have a zipfile, which can be opened and viewed freely.

Coordinator
Mar 30, 2011 at 8:29 PM
Edited Mar 30, 2011 at 8:29 PM

Hmm, I don't have WinRar, so no, I haven't tried that. I tried Windows Explorer to open them, and in that case, I got prompted for the password when I tried extracting the inner zip.  If I enter the password, then it extracts; if I don't enter the password, then it does not extract.  Once extracted, the inner zip opens and I can view and extract those files just fine.

I also tried the same thing in DotNetZip (using the graphical tool that is built on DotNetZip) and got essentially the same behavior.

update: ok I just downloaded winRar 4.0, and ran it on a zipfile produced with the code that I showed above.  It did exactly what I described for Windows Explorer and for the DotNetZip tool.

If I press "Cancel" when prompted for the password, it extracts no files.  If I click OK with a blank password, it gives me the dialog box shown above.  This is with the latest WinRar. 

So, I think you must be doing something wrong. Or you're making a bad assumption, somewhere.