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."); } }
public static void ShredSelectedFiles(BackgroundWorker bgwShredFiles) { int progress = 0; Globals.SuccessfulCount = 0; Globals.TotalCount = Globals.GetSelectedFiles().Count; // Use XChaCha20 - store current setting int selectedCipher = Globals.EncryptionAlgorithm; Globals.EncryptionAlgorithm = (int)Cipher.XChaCha20; foreach (string filePath in Globals.GetSelectedFiles()) { bool?directory = FileHandling.IsDirectory(filePath); if (directory != null) { if (directory == false) { CallShredFilesMethod(filePath, ref progress, bgwShredFiles); } else { ShredDirectory(filePath, ref progress, bgwShredFiles); } } } // Restore encryption algorithm setting Globals.EncryptionAlgorithm = selectedCipher; }
public static void PseudorandomData(string filePath, BackgroundWorker bgwShredFiles) { try { using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { byte[] randomBytes = FileHandling.GetBufferSize(fileStream); while (fileStream.Position < fileStream.Length) { randomBytes = SodiumCore.GetRandomBytes(randomBytes.Length); fileStream.Write(randomBytes, 0, randomBytes.Length); ReportProgress.ReportEncryptionProgress(fileStream.Position, fileStream.Length, bgwShredFiles); } } } catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.High); DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "'1 Pass' erasure failed."); } }
public static void GetFilePaths(bool encryption, byte[] passwordBytes, BackgroundWorker backgroundWorker) { int progress = 0; Globals.SuccessfulCount = 0; Globals.TotalCount = Globals.GetSelectedFiles().Count; foreach (string filePath in Globals.GetSelectedFiles()) { bool?fileIsDirectory = FileHandling.IsDirectory(filePath); if (fileIsDirectory != null) { if (fileIsDirectory == false) { CallEncryption(encryption, filePath, passwordBytes, ref progress, backgroundWorker); } else { DirectoryEncryption(encryption, filePath, passwordBytes, ref progress, backgroundWorker); } } } }
public static void ZeroFill(string filePath, bool useOnes, BackgroundWorker bgwShredFiles) { try { using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { byte[] zeroes = FileHandling.GetBufferSize(fileStream); if (useOnes == true) { zeroes = FillArrayWithOnes(zeroes); } while (fileStream.Position < fileStream.Length) { fileStream.Write(zeroes, 0, zeroes.Length); ReportProgress.ReportEncryptionProgress(fileStream.Position, fileStream.Length, bgwShredFiles); } } } catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.High); DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "'Zero fill' erasure failed."); } }