Protect ZipArchive
RadZipLibrary lets you protect a ZIP archive with a password. This article describes 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, 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
PasswordEncryptionSettingsclass. -
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 encrypts and decrypts 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
- Read a Password-protected ZipArchive
- Handle Invalid Passwords
Create a Password-Protected ZipArchive
To create a password-protected ZIP archive, pass a PasswordEncryptionSettings object to the ZipArchive 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();
}
}
}
Always dispose of the ZIP archive object when all operations are completed. Declare and instantiate the ZIP archive object in a
usingstatement. If that is not possible, call theDispose()method when you complete all operations.
Read a Password-Protected ZipArchive
To open a password-protected ZipArchive, pass a DecryptionSettings object (created via EncryptionSettings.CreateDecryptionSettings()) and handle the PasswordRequired event to supply the password.
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.
}
}
Always dispose of the ZIP archive object when all operations are completed. Declare and instantiate the ZIP archive object in a
usingstatement. If that is not possible, call theDispose()method when you complete all operations.
Handle Invalid Passwords
Starting with 2026 Q3, when you open a password-protected archive and supply an incorrect password, RadZipLibrary throws an InvalidPasswordException.
InvalidPasswordException is thrown for both PKWARE and AES encryption in the following scenarios:
- AES: When the password verification byte or the AES authentication code does not match.
- PKZIP: When the 12-byte decryption header verifier byte does not match the supplied password.
Example 3: Handle an incorrect password
DecryptionSettings decryptionSettings = EncryptionSettings.CreateDecryptionSettings();
decryptionSettings.PasswordRequired += (s, a) => a.Password = "wrongPassword";
try
{
using (Stream stream = File.Open("test.zip", FileMode.Open))
using (ZipArchive zipArchive = ZipArchive.Read(stream, null, null, decryptionSettings))
{
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
using (Stream entryStream = entry.Open())
{
entryStream.CopyTo(destinationStream);
}
}
}
}
catch (InvalidPasswordException ex)
{
Console.WriteLine("Invalid password: " + ex.Message);
}