我正在编写一个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还允许您选择Zip2.0PKWare加密算法。
发布于 2018-11-06 02:35:23
我从阅读代码和论坛帖子中了解到,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页面)
https://stackoverflow.com/questions/53164862
复制相似问题