public void Dispose() { if (File.GetLastWriteTimeUtc(DecryptedPath) > _origWriteTime) { //open the encrypted file using (var encryptedFile = new FileStream(_encryptedPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete)) //create the temporary decryption file using (var decryptedFile = new FileStream(DecryptedPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 512)) { _cryptProvider.EncryptFileAsync(encryptedFile, decryptedFile, CancellationToken.None).GetAwaiter().GetResult(); } } File.Delete(DecryptedPath); }
static void EncryptFile(string path) { var pwd = PromptPassword("Password:"******"Confirm Password:"******"passwords do not match"); throw new InvalidPasswordException(); } var crypto = new PasswordEncryptionProvider(pwd); using (var writeHandle = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read)) using (var readHandle = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Write)) { crypto.EncryptFileAsync(writeHandle, readHandle, CancellationToken.None).GetAwaiter().GetResult(); } }