r/csharp •

C# filestream writing to bitlocker drive binary files are corrupt, any ideas?

[deleted] — view deleted post

0 Upvotes

7 comments sorted by

8

u/ellaun 5d ago

Are you somehow under illusion that your CryptoStream is related to Bitlocker? I'm trying to decipher your confusion and here's what I'm getting: you're thinking that since you encrypted files with Bitlocker you need to "decrypt" them with your program. Here's what is happening instead: encrypted data is read from disk, decrypted with Bitlocker transparently and then passed to your C# program where it's decrypted again, thus turned from perfectly readable plaintext to garbage. Seems like you do not understand what "transparent" means.

If you use Bitlocker you need to do precisely nothing to interact the files. Just input valid password. Encryption and decryption is done on OS level, it never reaches your program and you cannot interact with it with your program. All you can do from here is to create another independent layer of encryption for no good reason.

0

u/[deleted] 5d ago

[deleted] — view deleted comment

4

u/ellaun 5d ago edited 5d ago

If problem is no longer present there is nothing much to investigate right now, it's a ghost story. And it's still confusing how you "found the input file corrupted" and had to restore it from backup and at the same time successful decryption was dependent on output path. There is just too much going on at the same time. The code you've shown tells very little. Maybe text files are processed normally because they literally are treated specially compared to binary data. I cannot see full code to verify it's not the case. Maybe path is somehow involved in key derivation because of bugs and presence of Bitlocker is a red herring here. And you haven't told what was the nature of corruption. You should have compared input file to the one from backup if you suspected it's corrupted and see where and how to get a clue what happened. Same from output files. How were they corrupted? Did it appear like a wrong key was given and output was completely gibberish?

This is why you shouldn't roll your own crypto. Having your documents become inaccessible because of bugs is a nasty way to lose data. Use 7zip archives with encryption or VeraCrypt for encrypted containers.

2

u/FullPoet 5d ago

Just put it in a zip archive thats password protected at the end of every month.

2

u/iam-stevie-bee 5d ago

My first guess would be a Read that assumes it filled the buffer. Stream.Read (and CryptoStream especially) can return fewer bytes than you asked for, and how often depends on the drive and driver stack underneath, which BitLocker changes. Small text files fit in one read and come through fine; big media files hit a short read and everything after it shifts. Check you use the return value, or switch to ReadExactly.

0

u/[deleted] 5d ago

[deleted] — view deleted comment

1

u/FullPoet 5d ago

You are not interacting with bitlocker.

We cannot make any assumptions without you showing us the code.

Is there a reason why you dont want to do that? Its just code.

1

u/iam-stevie-bee 4d ago

If ReadExactly isn't throwing, the read side is probably fine, so I'd look at the write. The usual suspects are a WriteAsync that isn't awaited, or the output stream not flushed and disposed before something opens the file again. A quick test: hash the buffer you pass to Write, hash the file once it's closed, and find the first offset where they differ. Posting the code would help too.

2

u/FullPoet 5d ago

Show us the code