Skip to content

ZipArchive Update: changing LastWriteTime of a ZipCrypto entry makes its correct password fail #135538

Closed

Description

@pawlos

Description

In ZipArchiveMode.Update, LastWriteTime can be changed without re-encrypting the entry. If the new time has a different high byte the stored check byte no longer matches the time in the header. .NET then reject the correct password. Update mode saves the archive without any error.

Reproduction Steps

using System.IO.Compression;

File.Delete("secret.zip");
using (ZipArchive archive = ZipFile.Open("secret.zip", ZipArchiveMode.Create))
{
    ZipArchiveEntry entry = archive.CreateEntry("a.txt", "pw", ZipEncryptionMethod.ZipCrypto);
    entry.LastWriteTime = new DateTimeOffset(2026, 1, 1, 12, 0, 0, TimeSpan.Zero);
    using var writer = new StreamWriter(entry.Open());
    writer.Write("hello");
}

// Change only the modification time.
using (ZipArchive archive = ZipFile.Open("secret.zip", ZipArchiveMode.Update))
{
    archive.GetEntry("a.txt")!.LastWriteTime = new DateTimeOffset(2026, 1, 1, 17, 0, 0, TimeSpan.Zero);
}

using (ZipArchive archive = ZipFile.OpenRead("secret.zip"))
{
    using var reader = new StreamReader(archive.GetEntry("a.txt")!.Open("pw"));
    Console.WriteLine(reader.ReadToEnd());
}

Expected behavior

Either the entry stays readable with its password and the program prints hello, or Update mode refuses the change instead of saving an entry whose correct password is rejected.

Actual behavior

Unhandled exception. System.IO.InvalidDataException: The password did not match.
   at System.IO.Compression.ZipCryptoStream.ReadAndValidateHeaderCore(Boolean isAsync, Stream baseStream, ZipCryptoKeys keys, Byte expectedCheckByte, CancellationToken cancellationToken)

Regression?

No, ZIP password support is new in .NET 11.

Known Workarounds

Re-encrypt the entry in the Update session.

Configuration

.NET 11.0.0-rc.1.26425.128, linux-x64/windows-x64

Other information

For comparison, libzip refuses such change returning ZIP_ER_OPNOTSUPP.

Activity

  1. added
    untriaged
    on Oct 9, 2026
  2. dotnet-policy-service commented on Oct 9, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
    See info in area-owners.md if you want to be subscribed.

  3. MuhammadBilal64 commented on Oct 10, 2026

    @MuhammadBilal64
    Contributor

    I looked into this and can confirm the cause. ZipCrypto entries written by .NET always set the data-descriptor bit, so the header’s check byte is derived from the high byte of the DOS time field. In Update mode, LastWriteTime only updates _lastModified, while an entry that isn’t re-encrypted retains its original encrypted bytes. This causes CalculateZipCryptoCheckByte to expect a different byte on read.

    Since the setter can’t re-encrypt without the password, I think throwing NotSupportedException for existing ZipCrypto entries that aren’t being rewritten would be a reasonable fix.
    I’m happy to submit a PR with tests. Would this approach make sense, or would it be better to reject only timestamp changes that affect the check byte?

  4. removed
    untriaged
    on Oct 15, 2026
  5. added this to the 12.0.0 milestone on Oct 15, 2026
  6. added a commit that references this issue on Oct 29, 2026
    96d80f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions