Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

Protect ZipArchive

RadZipLibrary lets you protect a ZIP archive with a password. This help article will teach you how to use RadZipLibrary to password-protect files and how to open files that are protected with a password. To protect a ZIP archive and all ZipArchiveEntry items in it, you should specify encryption settings when creating the ZipArchive object.

RadZipLibrary supports the following encryption algorithms:

  • Traditional PKWARE encryption algorithm - the settings for this encryption type are represented by the PasswordEncryptionSettings class.

  • Strong AES encryption algorithm - introduced in 2024 Q1.

AES encryption (Advanced Encryption Standard) is commonly used to secure sensitive information, such as credit card numbers, passwords, and personal data. It uses a symmetric-key algorithm, meaning the same key is used for both encrypting and decrypting the data. AES encryption uses a fixed-length key of 128, 192, or 256 bits to encrypt and decrypt data.

Create a Password-Protected ZipArchive

In order to create a password-protected ZIP archive, you need to pass a PasswordEncryptionSettings object to the ZipArchive's constructor along with the CompressionSettings and Encoding parameter.

PasswordEncryptionSettings has a Password property of type string, which represents the used password.

Example 1: Create a password-protected ZIP archive

using (Stream stream = File.Open("test.zip", FileMode.Create)) 
{ 
 
    //By default the EncryptionStrenght is 256 bits but it can be explicitly specified (EncryptionStrength.Aes128, EncryptionStrength.Aes192, and EncryptionStrength.Aes256) by passing it to the constructor 
    PasswordEncryptionSettings aesEncryptionSettings = EncryptionSettings.CreateAesPasswordEncryptionSettings(); 
 
    //You can also use the PKWARE encryption algorithm instead of the AES one 
    PasswordEncryptionSettings pkwareEncryptionSettings = EncryptionSettings.CreatePkzipPasswordEncryptionSettings(); 
 
    aesEncryptionSettings.Password = "password";  
    CompressionSettings compressionSettings = null; 
    Encoding encoding = null; 
    using (ZipArchive archive = ZipArchive.Create(stream, encoding, compressionSettings, aesEncryptionSettings)) 
    { 
        using (ZipArchiveEntry entry = archive.CreateEntry("text.txt")) 
        { 
            StreamWriter writer = new StreamWriter(entry.Open()); 
            writer.WriteLine("Hello world!"); 
            writer.Flush(); 
        } 
    } 
} 

You must always dispose of the ZIP archive object when all operations that use it are completed. Telerik Support recommends that you declare and instantiate the ZIP archive object in a using statement. If it is not possible for some reason, then do not forget to call the Dispose() method when you complete all operations.

Read a Password-Protected ZipArchive

In order to open a password-protected ZipArchive, you need to pass a DefaultEncryptionSettings object with the password that was used to create the archive in the first place.

Example 2: Open and read a password-protected ZIP archive

using (FileStream stream = File.Open("test.zip", FileMode.Open)) 
{  
        DecryptionSettings decryptionSettings = EncryptionSettings.CreateDecryptionSettings(); 
        decryptionSettings.PasswordRequired += (s, a) => a.Password = "password"; 
        CompressionSettings compressionSettings = null; 
        Encoding encoding = null; 
        using (ZipArchive zipArchive = ZipArchive.Read(stream, encoding, compressionSettings, decryptionSettings)) 
        { 
            // Display the list of the files in the selected zip file using the ZipArchive.Entries property.  
        } 
} 

You must always dispose of the ZIP archive object when all operations that use it are completed. We recommend that you declare and instantiate the ZIP archive object in a using statement. If it is not possible for some reason, then do not forget to call the Dispose() method when you complete all operations.