With the current required focus on technology security FileVault was a fun and interesting use case for the C# Programming Language, the project was built with .NET 10 (net10.0-windows) as a Windows Worker Service that watches and monitors an inbox folder, compresses the files with Brotli, encrypts them using AES-256-GCM, stores a master key using Windows DPAPI and supports one shot simple CLI decryption(as detailed below).
-
Directory Monitoring (
DirectoryWatcherService): WatchesWatchPatha specific directory (C:\FileVault\Inboxby default) viaFileSystemWatcher, skips.tmpand.encfiles, It will seed any pre existing files on startup and pushes candidate file paths into aChannel<string>queue. -
Lock Backoff & Processing (
FileProcessorWorker): Will consumes file paths from the channel and it probes for exclusive read access with exponential backoff (BaseRetryDelayMsup toMaxRetryDelayMs, capped atMaxLockRetries), writes to the<filename>.enc.tmpinVaultPath, and also atomically renames the file to<filename>.enc. FileVault has the option of deleting the source file whenDeleteSourceAfterEncryptistrue. -
Compression, Encryption & Key Storage (
CryptoCompressionService):- Compresses the file contents with
BrotliStream(CompressionLevel.Optimal). - Encrypts that compressed date with
AesGcmusing a fresh 12-byte nonce and 16-byte authentication tag. - FileVault also writes a
BVAULTbinary container:[6B "BVAULT"][1B Version][12B Nonce][16B Tag][Ciphertext]. - To Protect the date it loads a 256-bit master key with Windows DPAPI (
ProtectedData).
- Compresses the file contents with
{
"Vault": {
"WatchPath": "C:\\FileVault\\Inbox",
"VaultPath": "C:\\FileVault\\Vault",
"MaxLockRetries": 20,
"BaseRetryDelayMs": 100,
"MaxRetryDelayMs": 2000,
"DeleteSourceAfterEncrypt": false,
"KeyStorePath": "C:\\FileVault\\master.key.dpapi"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}From the project directory:
dotnet build
dotnet runDrop a test file into C:\FileVault\Inbox to verify it is written as <filename>.enc in C:\FileVault\Vault.
dotnet run -- --decrypt "C:\FileVault\Vault\test-note.txt.enc" "C:\FileVault\Restored\test-note.txt"Program.cs— Host builder, options binding, dependency injection, Windows Service / Event Log setup, and--decryptCLI handling.Options/VaultOptions.cs— Strongly typed configuration model bound to the"Vault"section.Services/FileQueueChannel.cs—System.Threading.Channelswrapper decoupling file discovery from processing.Services/DirectoryWatcherService.cs—BackgroundServicewrappingFileSystemWatcher.Services/FileProcessorWorker.cs—BackgroundServicehandling file lock retries, atomic output writes, and optional source cleanup.Services/CryptoCompressionService.cs— Brotli compression/decompression, AES-256-GCM authenticated encryption/decryption, and DPAPI key management.
- C# Program Structure & Top-Level Statements: How
Program.cs, namespaces, and project structure fit together — General structure of a C# program and Top-level statements. - Authenticated Encryption (
AesGcm): Symmetric encryption with a per-file nonce and authentication tag to detect tampering — AesGcm Class. - Stream Compression (
BrotliStream): Compressing file bytes before encryption to reduce payload size — BrotliStream Class. - Windows Data Protection API (
ProtectedData): OS-managed key protection tied to Windows user or machine credentials — ProtectedData Class. - Directory Monitoring (
FileSystemWatcher): Listening for file creation and rename events in a watched folder — FileSystemWatcher Class.