public static void EncryptionErasure(string filePath, BackgroundWorker bgwShredFiles) { try { string encryptedFilePath = AnonymousRename.GetAnonymousFileName(filePath); using (var ciphertext = new FileStream(encryptedFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) using (var plaintext = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { byte[] fileBytes = FileHandling.GetBufferSize(plaintext); byte[] key = SodiumCore.GetRandomBytes(Constants.EncryptionKeySize); byte[] nonce = SodiumCore.GetRandomBytes(Constants.XChaChaNonceLength); StreamCiphers.Encrypt(plaintext, ciphertext, 0, fileBytes, nonce, key, bgwShredFiles); Utilities.ZeroArray(key); Utilities.ZeroArray(nonce); } // Overwrite the original file File.Copy(encryptedFilePath, filePath, true); ShredFiles.EraseFileMetadata(encryptedFilePath); File.Delete(encryptedFilePath); } catch (Exception ex) when(ex is CryptographicException || ExceptionFilters.FileAccessExceptions(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.High); DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "'Encryption' erasure failed."); } }
private void BgwShredFiles_DoWork(object sender, DoWorkEventArgs e) { ShredFiles.ShredSelectedFiles(bgwShredFiles); }