icsharpcode / SharpZipLib

#ziplib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform.
http://icsharpcode.github.io/SharpZipLib/
MIT License
3.73k stars 976 forks source link

Password zip is inconsisent #873

Open HashPong opened 4 months ago

HashPong commented 4 months ago

Describe the bug

Only passwords with length up to 98 seem to work, if i use 99 and i even copy/paste the password directly unzipping the archive using 7zip does not work. how come?

Seems a bit random and not sure if it would be safe for db backups in this case.

private static async Task CreateBackup(string zipFile, string pgDumpArgs)
{
    ProcessStartInfo processInfo = new()
    {
        FileName = @"C:\Program Files\PostgreSQL\16\bin\pg_dump.exe",
        Arguments = pgDumpArgs,
        RedirectStandardOutput = true,
        RedirectStandardError = true, // Capture error output
        UseShellExecute = false,
        CreateNoWindow = true
    };

    using Process process = Process.Start(processInfo);
    if (process == null)
    {
        Console.WriteLine("Failed to start pg_dump process.");
        return;
    }

    using (FileStream fsOut = File.Create(zipFile))
    using (ZipOutputStream zipStream = new(fsOut))
    {
        zipStream.SetLevel(3); // 0-9, 9 being the highest level of compression
        zipStream.Password = "z4KpjH+}H9H*g3K3Jb-9}M^jnMN^-!Zs+8UZ3sfxWg5T~@ZHJ:Eoz!AFNMwdf!g_2QpX#)}4WKwHLL%C?s1BB*wx%:cia0j=m7.W";
        zipStream.UseZip64 = UseZip64.On; // Set to AsNeeded if you need Zip64

        ZipEntry newEntry = new ZipEntry($"backup_{DateTime.Now:yyyyMMdd_HHmmss}.backup")
        {
            DateTime = DateTime.Now,
            AESKeySize = 256 // AES-256 encryption
        };

        zipStream.PutNextEntry(newEntry);

        var outputTask = process.StandardOutput.BaseStream.CopyToAsync(zipStream);
        var errorTask = process.StandardError.ReadToEndAsync();

        await Task.WhenAll(outputTask, errorTask);
        zipStream.CloseEntry();

        string errorOutput = await errorTask;

        if (!string.IsNullOrEmpty(errorOutput))
        {
            Console.WriteLine($"pg_dump reported the following errors:\n{errorOutput}");
        }
    }

    await process.WaitForExitAsync();

    if (process.ExitCode != 0)
    {
        Console.WriteLine($"pg_dump failed with exit code {process.ExitCode}");
    }
    else
    {
        Console.WriteLine($"{zipFile} Database backup successfully completed.");
    }
}

Reproduction Code

No response

Steps to reproduce

zip zip

Expected behavior

work

Operating System

Windows

Framework Version

Other

Tags

No response

Additional context

No response