r/AskProgramming • u/XRay2212xray • 23h ago
C# filestream writing to bitlocker drive binary files are corrupt, any ideas?
I wrote a program in c# that stores multiple files in a single file similar to zip or tar. That program just uses filestream writing, nothing fancy. I recently got a new computer, so I copied my compressed file onto the computer and was able to extract files from it.
After using my program, I used bitlocker on the ssd. Bitlocker gives you two options, use the old aes-cbc or new xts-aes cypher and if you want to do the entire drive or just existing files. I took the default of new xts-aes and only existing files. Let it run until the drive was encrypted and rebooted.
First thing I did after reboot was try to extract more files using my program and it found the input file corrupted. That shouldn't happen, but I just reloaded the file from a backup and things seemed to be working again. However, when I extracted to the encrypted ssd, text files were ok but media files like images and videos were corrupted. I used a diff tool and confirmed every single text was ok and every other file corrupted. Extracting from the same file on the encrypted ssd to a flash drive worked perfectly and nothing was corrupted. So only writing to the encrypted ssd from my app was having issues. Copying from the extracted files on the flash drive to the ssd also did not corrupt the files.
My understanding is bitlocker shouldn't care about the file type and it should be completely transparent to the application, yet thats not the behavior I was getting.
I then turned bitlocker off on the ssd, let it decrypt the drive and rebooted. The file on the ssd was not corrupted by the decrypt and extracts from the same file to the ssd now worked for all file types.
I turned bitlocker back on for the ssd and used the older aes-cbc and told it to encrypt everything including free space. After it completed and I rebooted, my program now correctly extracts files to the ssd.
So clearly other applications like the windows file copy didn't have issues with the bitlocker ssd and only my applcation did. I'm not able to come up with a theory as to how this could happen. I could see if everything was corrupted but the fact that text files are all ok is even more confusing. I used crystaldiskinfo to confirm there were no read or write failures on the ssd and the event logs don't show any errors reading or writing files to the ssd or bitlocker issues.
I guess my next step would be to decrypt the drive again and try the original bitlocker options to see if the problem returns. Any ideas what could be going on? Everything I've been able to read says bitlocker shouldn't care about the file type and bitlocker presence should be transparent to filestream writing in c#.
Edit: including relevent code
public void Extract(string pFileName, long pSize)
{
byte[] vIV = ReadIV();
CryptoStream cryptoStream = new(
oFileStream,
oAes.CreateDecryptor(oAes.Key, vIV),
CryptoStreamMode.Read, true);
{
long vSize = pSize;
int n = 0;
using (FileStream vOutputFileStream = new FileStream(pFileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
do
{
n = MAXBUFFER;
if (vSize < n)
n = (int)vSize;
cryptoStream.ReadExactly(oBytes, 0, n);
vOutputFileStream.Write(oBytes, 0, n);
vSize -= n;
}
while (vSize > 0);
vOutputFileStream.Close();
}
}
}
3
u/PLBjt 13h ago
Text fine and binary broken smells like the code assumes Read fills the whole buffer. Stream.Read can return fewer bytes than you asked for, and the encrypted volume changes the I/O pattern enough that short reads start happening. Check that every Read uses the returned count (or ReadExactly on .NET 7+) and that you only write that many bytes back out. Also make sure nothing in the extract path goes through a StreamReader/StreamWriter or text-mode API, since an encoding pass leaves plain text intact and mangles everything else. Quick test: diff a broken file against the original and see if the first bad byte lands on a multiple of your buffer size.
2
u/Good_Construction190 17h ago
Are you flushing and closing streams?
7
u/dodexahedron 21h ago
Bitlocker is completely transparent to applications. File contents are never exposed encrypted and, in fact, unless you have unlocked the volume in tje first place, you wouldn't even be able to enumerate the contents of it and open the file at all.
So no. Bitlocker is not involved.
This is a program bug or an invocation or environment difference (or multiple of the above).
First, attempt to reproduce the problem. I suspect you will not be able to, and that your problem was simply around how it was invoked or some other potentially tiny difference between the failed and successful runs. This could include threading issues, as well, or anything else causing non-deterministic behavior of your deserializer code.
If you do manage to reproduce it, show us the code for your program, the way you invoked it, and an example data file that reproduces the problem.
Otherwise, IO on composed streams with a FileStream at the bottom will work just fine so long as the writer and the reader use exactly the same composition, and the data on disk will be whatever results from applying the transformations of each stream, in sequence, from top to bottom of the stack of streams. And then you can read it back with the same stack, which does the same stuff in reverse for you already.
Oh also...
.net version may matter, depending on what the data itself is and its encoding at each layer of that stream stack. A recompilation, especially after a reboot, may have changed the environment due to an update, for example.
No matter what, the answer to the question is that no, Bitlocker doesnt have anything at all to do with this and you would have a lot more problems, a lot earlier, if it did.
4
u/DamienTheUnbeliever 20h ago
Almost certainly there's one or more race conditions in *your code* that are being exposed because the IO to the bitlocker drive takes *slightly longer* and uses a bit more CPU.
3
u/henke37 22h ago
Post code.
1
u/ih-shah-may-ehl 19h ago
Absolutely 100% guaranteed the problem is your code, not bitlocker