You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ZipArchive Update: changing LastWriteTime of a ZipCrypto entry makes its correct password fail #135538
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
usingSystem.IO.Compression;File.Delete("secret.zip");using(ZipArchivearchive=ZipFile.Open("secret.zip",ZipArchiveMode.Create)){ZipArchiveEntryentry=archive.CreateEntry("a.txt","pw",ZipEncryptionMethod.ZipCrypto);entry.LastWriteTime=newDateTimeOffset(2026,1,1,12,0,0,TimeSpan.Zero);usingvarwriter=newStreamWriter(entry.Open());writer.Write("hello");}// Change only the modification time.using(ZipArchivearchive=ZipFile.Open("secret.zip",ZipArchiveMode.Update)){archive.GetEntry("a.txt")!.LastWriteTime=newDateTimeOffset(2026,1,1,17,0,0,TimeSpan.Zero);}using(ZipArchivearchive=ZipFile.OpenRead("secret.zip")){usingvarreader=newStreamReader(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.
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?
Activity
dotnet-policy-service commentedon Oct 9, 2026
Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.
MuhammadBilal64 commentedon Oct 10, 2026
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,
LastWriteTimeonly updates_lastModified, while an entry that isn’t re-encrypted retains its original encrypted bytes. This causesCalculateZipCryptoCheckByteto expect a different byte on read.Since the setter can’t re-encrypt without the password, I think throwing
NotSupportedExceptionfor 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?
Fix ZipCrypto LastWriteTime corruption in ZipArchiveMode.Update (#135620