private static void CallEncryption(bool encryption, string filePath, byte[] passwordBytes, ref int progress, BackgroundWorker backgroundWorker) { try { bool kryptorExtension = filePath.EndsWith(Constants.EncryptedExtension, StringComparison.Ordinal); // Prevent Read-Only file attribute causing errors File.SetAttributes(filePath, FileAttributes.Normal); if (encryption == true && kryptorExtension == false) { Encryption.InitializeEncryption(filePath, passwordBytes, backgroundWorker); OverwriteDisabled(filePath); } else if (encryption == false && kryptorExtension == true) { Decryption.InitializeDecryption(filePath, passwordBytes, backgroundWorker); } else { DisplayFileError(filePath, encryption, kryptorExtension); } ReportProgress.IncrementProgress(ref progress, backgroundWorker); } catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.High); DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to set file attributes to normal."); } }
public static void Decrypt(FileStream plaintext, FileStream ciphertext, byte[] fileBytes, byte[] nonce, byte[] key, BackgroundWorker bgwDecryption) { NullChecks.FileEncryption(plaintext, ciphertext, fileBytes, nonce, key); int bytesRead; while ((bytesRead = ciphertext.Read(fileBytes, 0, fileBytes.Length)) > 0) { byte[] decryptedBytes = DecryptFileBytes(fileBytes, nonce, key); plaintext.Write(decryptedBytes, 0, bytesRead); // Report progress if decrypting a single file ReportProgress.ReportEncryptionProgress(plaintext.Position, ciphertext.Length, bgwDecryption); } }
public static void DecryptAesCBC(FileStream plaintext, FileStream ciphertext, byte[] fileBytes, byte[] nonce, byte[] key, BackgroundWorker bgwDecryption) { NullChecks.FileEncryption(plaintext, ciphertext, fileBytes, nonce, key); using (var aes = new AesCryptoServiceProvider() { Mode = _cbcMode, Padding = _pkcs7Padding }) { using (var cryptoStream = new CryptoStream(ciphertext, aes.CreateDecryptor(key, nonce), CryptoStreamMode.Read)) { int bytesRead; while ((bytesRead = cryptoStream.Read(fileBytes, 0, fileBytes.Length)) > 0) { plaintext.Write(fileBytes, 0, bytesRead); // Report progress if encrypting a single file ReportProgress.ReportEncryptionProgress(plaintext.Position, ciphertext.Length, bgwDecryption); } } } }
private static void CallShredFilesMethod(string filePath, ref int progress, BackgroundWorker bgwShredFiles) { try { File.SetAttributes(filePath, FileAttributes.Normal); switch (Globals.ShredFilesMethod) { case 0: ShredFilesMethods.FirstLast16KiB(filePath); break; case 1: // false = use zeros, not ones (used in Infosec Standard 5 Enhanced) ShredFilesMethods.ZeroFill(filePath, false, bgwShredFiles); break; case 2: ShredFilesMethods.PseudorandomData(filePath, bgwShredFiles); break; case 3: ShredFilesMethods.EncryptionErasure(filePath, bgwShredFiles); break; case 4: ShredFilesMethods.InfosecStandard5Enhanced(filePath, bgwShredFiles); break; case 5: ShredFilesMethods.PseudorandomData5Passes(filePath, bgwShredFiles); break; } DeleteFile(filePath); ReportProgress.IncrementProgress(ref progress, bgwShredFiles); } catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.High); DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to set file attributes to normal. This file has not been shredded."); } }
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 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."); } }