-
Notifications
You must be signed in to change notification settings - Fork 104
Fix missing bytes in large file encryption/decryption by adding explicit stream flushes #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…cit stream flushes Co-authored-by: mattosaurus <22458485+mattosaurus@users.noreply.github.com>
Co-authored-by: mattosaurus <22458485+mattosaurus@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|



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.