c# - 使用 SharpZipLib 如何设置加密算法?

标签 c# sharpziplib

我正在编写一个 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 加密算法。

最佳答案

据我通过阅读代码和论坛帖子了解到,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;

(评论来自此页面https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples/6dc300804f36f981e516fa477219b0e40c192861)

关于c# - 使用 SharpZipLib 如何设置加密算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53164862/

相关文章:

C# 网络流压缩 - Sharpziplib、DotNetZip、gzipstream 在我的流上均出现错误

.net - 在多个线程上并行调用 ICsharpCode.SharpZipLib 是否安全

c# - 在 C# 中提取 zip 文件时,lastwritetime 正在改变?

c# - 将.bz2 流解压缩到 C# 中的文件

c# - 私有(private)方法和属性的 ReSharper C# 命名风格

c# - 从反射或硬编码字符串中将方法名称提取为字符串?

c# - 解压缩 PVRTC

c# - NHibernate 删除集合中的项目不起作用

c# - for循环中的条件是否在每次迭代中都被评估?

c# - SharpZipLib 提取的文件大小