2

I am writing a zip file generator which will be consumed by a third party using a specific encryption algorithm

I have found the enumeration of algorithms here: ICSharpCode.SharpZipLib.Zip.EncryptionAlgorithm

But I don't know how to apply the algorithm to a given zip archive. Here's my code.

        using (FileStream fsOut = File.Create(fullPath + ".zip"))
        using (var zipStream = new ZipOutputStream(fsOut))
        {
            zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
            zipStream.Password = "password";

            using (MemoryStream memoryStream = new MemoryStream())
            using (TextWriter writer = new StreamWriter(memoryStream))
            {
                // redacted: write data to memorytream...

                var dataEntry = new ZipEntry(fullPath.Split('\\').Last()+".txt");
                dataEntry.DateTime = DateTime.Now;
                zipStream.PutNextEntry(dataEntry);
                memoryStream.WriteTo(zipStream);
                zipStream.CloseEntry();
            }
        }

Edit
DotNetZip allows you to also choose Zip 2.0 PKWARE encryption algorithm.

Myster
  • 17,704
  • 13
  • 64
  • 93

1 Answers1

3

As I understand from reading the code and forum posts, EncryptionAlgorithm exists to document the values available in the Zip standard and not as an option for the end user.

The encryption algorithms that are actually available to you are AES128 and AES256. You apply the algorithm on each entry by assigning the AESKeySize property.

So in your case:

// Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
// A password on the ZipOutputStream is required if using AES.
dataEntry.AESKeySize = 256;

(the comments come from this page https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples/6dc300804f36f981e516fa477219b0e40c192861)

dee-see
  • 23,668
  • 5
  • 58
  • 91