【问题标题】:Using SharpZipLib How do I set the encryption algorithm?使用 SharpZipLib 如何设置加密算法?
【发布时间】:2018-11-06 02:10:31
【问题描述】:

我正在编写一个 zip 文件生成器,第三方使用特定的加密算法将使用它

我在这里找到了算法的枚举: ICSharpCode.SharpZipLib.Zip.EncryptionAlgorithm

但我不知道如何将算法应用于给定的 zip 存档。 这是我的代码。

        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();
            }
        }

编辑
DotNetZip 还允许您选择 Zip 2.0 PKWARE 加密算法。

【问题讨论】:

    标签: c# sharpziplib


    【解决方案1】:

    据我阅读代码和论坛帖子了解到,EncryptionAlgorithm 的存在是为了记录 Zip 标准中可用的值,而不是作为最终用户的选项。

    您实际可用的加密算法是 AES128 和 AES256。您可以通过分配 AESKeySize 属性将算法应用于每个条目。

    所以在你的情况下:

    // 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;
    

    (cmets来自这个页面https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples/6dc300804f36f981e516fa477219b0e40c192861

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多