Encryption Property

DotNetZip

Ionic Zip Library v1.9.1.6 Encryption Property
ReferenceIonic.ZipZipEntryEncryption
Set this to specify which encryption algorithm to use for the entry when saving it to a zip archive.
Declaration Syntax
C# Visual Basic Visual C++
public EncryptionAlgorithm Encryption { get; set; }
Public Property Encryption As EncryptionAlgorithm
	Get
	Set
public:
property EncryptionAlgorithm Encryption {
	EncryptionAlgorithm get ();
	void set (EncryptionAlgorithm value);
}
Remarks

Set this property in order to encrypt the entry when the ZipFile is saved. When setting this property, you must also set a Password on the entry. If you set a value other than None on this property and do not set a Password then the entry will not be encrypted. The ZipEntry data is encrypted as the ZipFile is saved, when you call Save()()()() or one of its cousins on the containing ZipFile instance. You do not need to specify the Encryption when extracting entries from an archive.

The Zip specification from PKWare defines a set of encryption algorithms, and the data formats for the zip archive that support them, and PKWare supports those algorithms in the tools it produces. Other vendors of tools and libraries, such as WinZip or Xceed, typically support a subset of the algorithms specified by PKWare. These tools can sometimes support additional different encryption algorithms and data formats, not specified by PKWare. The AES Encryption specified and supported by WinZip is the most popular example. This library supports a subset of the complete set of algorithms specified by PKWare and other vendors.

There is no common, ubiquitous multi-vendor standard for strong encryption within zip files. There is broad support for so-called "traditional" Zip encryption, sometimes called Zip 2.0 encryption, as specified by PKWare, but this encryption is considered weak and breakable. This library currently supports the Zip 2.0 "weak" encryption, and also a stronger WinZip-compatible AES encryption, using either 128-bit or 256-bit key strength. If you want DotNetZip to support an algorithm that is not currently supported, call the author of this library and maybe we can talk business.

The ZipFile class also has a Encryption property. In most cases you will use that property when setting encryption. This property takes precedence over any Encryption set on the ZipFile itself. Typically, you would use the per-entry Encryption when most entries in the zip archive use one encryption algorithm, and a few entries use a different one. If all entries in the zip file use the same Encryption, then it is simpler to just set this property on the ZipFile itself, when creating a zip archive.

Some comments on updating archives: If you read a ZipFile, you can modify the Encryption on an encrypted entry: you can remove encryption from an entry that was encrypted; you can encrypt an entry that was not encrypted previously; or, you can change the encryption algorithm. The changes in encryption are not made permanent until you call Save() on the ZipFile. To effect changes in encryption, the entry content is streamed through several transformations, depending on the modification the application has requested. For example if the entry is not encrypted and the application sets Encryption to PkzipWeak, then at the time of Save(), the original entry is read and decompressed, then re-compressed and encrypted. Conversely, if the original entry is encrypted with PkzipWeak encryption, and the application sets the Encryption property to WinZipAes128, then at the time of Save(), the original entry is decrypted via PKZIP encryption and decompressed, then re-compressed and re-encrypted with AES. This all happens automatically within the library, but it can be time-consuming for large entries.

Additionally, when updating archives, it is not possible to change the password when changing the encryption algorithm. To change both the algorithm and the password, you need to Save() the zipfile twice. First set the Encryption to None, then call Save(). Then set the Encryption to the new value (not "None"), then call Save() once again.

The WinZip AES encryption algorithms are not supported on the .NET Compact Framework.

Examples

This example creates a zip archive that uses encryption, and then extracts entries from the archive. When creating the zip archive, the ReadMe.txt file is zipped without using a password or encryption. The other file uses encryption.

CopyC#
// Create a zip archive with AES Encryption.
using (ZipFile zip = new ZipFile())
{
    zip.AddFile("ReadMe.txt")
    ZipEntry e1= zip.AddFile("2008-Regional-Sales-Report.pdf");
    e1.Encryption= EncryptionAlgorithm.WinZipAes256;
    e1.Password= "Top.Secret.No.Peeking!";
    zip.Save("EncryptedArchive.zip");
}

// Extract a zip archive that uses AES Encryption.
// You do not need to specify the algorithm during extraction.
using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip"))
{
    // Specify the password that is used during extraction, for
    // all entries that require a password:
    zip.Password= "Top.Secret.No.Peeking!";
    zip.ExtractAll("extractDirectory");
}
CopyVB.NET
' Create a zip that uses Encryption.
Using zip As New ZipFile()
    zip.AddFile("ReadMe.txt")
    Dim e1 as ZipEntry
    e1= zip.AddFile("2008-Regional-Sales-Report.pdf")
    e1.Encryption= EncryptionAlgorithm.WinZipAes256
    e1.Password= "Top.Secret.No.Peeking!"
    zip.Save("EncryptedArchive.zip")
End Using

' Extract a zip archive that uses AES Encryption.
' You do not need to specify the algorithm during extraction.
Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip"))
    ' Specify the password that is used during extraction, for
    ' all entries that require a password:
    zip.Password= "Top.Secret.No.Peeking!"
    zip.ExtractAll("extractDirectory")
End Using
Exceptions
Exception Condition
InvalidOperationException Thrown in the setter if EncryptionAlgorithm.Unsupported is specified.

Assembly: Ionic.Zip (Module: Ionic.Zip) Version: 1.9.1.8 (1.9.1.8)