Fix missing bytes in large file encryption/decryption by adding explicit stream flushes#317
Merged
mattosaurus merged 13 commits intomasterfrom Oct 20, 2025
Merged
Conversation
…cit stream flushes Co-authored-by: mattosaurus <[email protected]>
Co-authored-by: mattosaurus <[email protected]>
Copilot
AI
changed the title
[WIP] Fix missing bytes after encrypting and decrypting large files
Fix missing bytes in large file encryption/decryption by adding explicit stream flushes
Oct 18, 2025
There was a problem hiding this comment.
Pull Request Overview
Adds explicit Flush/FlushAsync calls to PGP encryption/signing pipelines to prevent buffered data loss with large files when using BouncyCastle stream wrappers.
- Inserted flushes after writing literal data and before disposing layered encrypted/compressed streams
- Applied changes consistently across async/sync encryption and signing paths
- Added flushes to utility methods that write literal data
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| PgpCore/PGP.cs | Added Flush/FlushAsync calls in encrypted, signed, and sign+encrypt output paths. |
| PgpCore/PGP.EncryptAsync.cs | Added FlushAsync calls after writing literal data (compressed and uncompressed cases). |
| PgpCore/PGP.EncryptSync.cs | Added Flush calls after writing literal data (compressed and uncompressed cases). |
| PgpCore/Helpers/Utilities.cs | Added Flush/FlushAsync after CopyTo operations when creating literal data. |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
…y and consistency
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
This PR fixes a critical data loss bug that reappeared in all 6.x versions (6.3.1, 6.4.1, 6.5.0, 6.5.1) after working correctly in version 5.13.1. When encrypting and decrypting large files (e.g., ~9.2GB), approximately 1MB of data would be missing from the decrypted output, causing data corruption.
Closes #[issue_number]
Root Cause
The issue was caused by streams not being explicitly flushed before disposal. When working with the BouncyCastle PGP stream wrappers (
PgpEncryptedDataGenerator,PgpCompressedDataGenerator,PgpLiteralDataGenerator), buffered data was being lost when the streams were disposed through theusingstatement without first being flushed. This buffering behavior became particularly problematic with large files where significant amounts of data could remain in buffers.Solution
Added explicit
FlushAsync()/Flush()calls at strategic points before stream disposal to ensure all buffered data is written:Changes Made
1. Stream-to-literal data conversion (
Utilities.cs)CopyToAsync()andCopyTo()in literal data writing methods2. Encryption methods (
PGP.EncryptAsync.cs,PGP.EncryptSync.cs)3. Sign and EncryptAndSign methods (
PGP.cs)WriteOutputAndSign*methods (after writing literal data)OutputEncrypted*methods (for compressed and encrypted streams)OutputSigned*methods (for compressed streams)Testing
Created comprehensive manual tests that verify:
All tests confirm complete data preservation by comparing both file sizes and MD5 hashes.
Impact
These are minimal, surgical changes (26 lines added across 4 files) that:
The fix follows .NET best practices for stream handling and ensures all buffered data is written before streams are disposed.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com/usr/lib/apt/methods/https(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fixes #304
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.