Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 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);
             }
         }
     }
 }
Ejemplo n.º 3
0
 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.");
     }
 }
Ejemplo n.º 4
0
 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.");
     }
 }