Encryption Methods: How to Best Protect Your Data
Applies To: All. AES is available in the Full Version only.

Introduction

Encrypting Existing Files in an Archive

To encrypt existing files in an archive, use one of the following methods:

Callbacks Called

When encrypting existing files, the following callbacks are called:

Additional Considerations

Standard Zip Encryption

The standard zip encryption method (CZipCryptograph::encStandard) is considered weak and should be only used with low security requirements or for preserving compatibility with applications that do not support the strong encryption.
Sample Code
LPCTSTR zipFileName = _T("C:\\Temp\\test.zip");
LPCTSTR password = _T("secret");
CZipArchive zip;
zip.Open(zipFileName, CZipArchive::zipCreate);
// there is no need to set the encryption method with the
// standard zip encryption, it is used by default
// set the password to encrypt the next file
zip.SetPassword(password);
zip.AddNewFile(_T("C:\\Temp\\file1.dat"));
// clear the password
zip.SetPassword();
zip.AddNewFile(_T("C:\\Temp\\file2.dat"));
zip.Close();
// decompress the files
zip.Open(zipFileName);
// the first file is encrypted
ASSERT(zip[0]->IsEncrypted());
// the second file is not
ASSERT(!zip[1]->IsEncrypted());
zip.SetPassword(password);
// there is no need to set the encryption method
// when decrypting
zip.ExtractFile(0, _T("C:\\Temp"), false, _T("file1.ext"));
// there is no need to clear the password
// when decrypting a not encrypted file
zip.ExtractFile(1, _T("C:\\Temp"), false, _T("file2.ext"));
zip.Close();

Strong AES Encryption Conforming to the WinZip Format

The ZipArchive Library supports AES encryption with key sizes 128-bit, 192-bit and 256-bit. The format of AES encrypted data conforms to the WinZip AES Encryption Specification. It is the preferred way to ensure that your data is secure. The ZipArchive Library encrypts files the way the WinZip does:

Enabling AES Encryption in the ZipArchive Library

To use the AES encryption, you need to make sure that _ZIP_AES is defined in the _features.h file. Rebuild the ZipArchive Library and your application, if you modify this definition. You may choose to keep the encryption disabled, if you don't use it, to keep the size of the compiled library smaller.

Encrypting Archives with AES

You need to choose the encryption method with the CZipArchive::SetEncryptionMethod() using one of the CZipCryptograph::EncryptionMethod values.
Sample Code
LPCTSTR zipFileName = _T("C:\\Temp\\test.zip");
LPCTSTR password = _T("secret");
CZipArchive zip;
zip.Open(zipFileName, CZipArchive::zipCreate);
zip.SetPassword(password);
// set the strongest encryption
zip.SetEncryptionMethod(CZipCryptograph::encWinZipAes256);
zip.AddNewFile(_T("C:\\Temp\\file1.dat"));
// use the standard encryption for the second file
zip.SetEncryptionMethod(CZipCryptograph::encStandard);
zip.AddNewFile(_T("C:\\Temp\\file2.dat"));
zip.Close();
// decompress the files
zip.Open(zipFileName);
ASSERT(zip[0]->IsEncrypted());
ASSERT(zip[1]->IsEncrypted());
zip.SetPassword(password);
// there is no need to set the encryption method
// when decrypting, even if files are encrypted
// using different methods
zip.ExtractFile(0, _T("C:\\Temp"), false, _T("file1.ext"));
zip.ExtractFile(1, _T("C:\\Temp"), false, _T("file2.ext"));
zip.Close();

Special Considerations

Checking Password Validity

The password itself is not stored inside of an archive. A preliminary check is performed when opening a file in an archive for extraction (e.g. with the CZipArchive::OpenFile() method). The ZipArchive Library throws CZipException::badPassword, if this preliminary check fails. However, this is not reliable enough to detect all cases of an invalid password.

The final check is performed when a file is completely extracted. A file's data checksum is then verified (but only if the consistency check CZipArchive::checkCRC was not disabled with the CZipArchive::SetIgnoredConsistencyChecks() method). If it fails, an exception with the code CZipException::badCrc is thrown. At this point it is not possible to distinguish whether the bad checksum was caused by an invalid password or a corrupted archive.

See Also API Links

Article ID: 0610201627
Copyright © 2000 - 2022 Artpol Software - Tadeusz Dracz