示例#1
0
 public static void RestoreDirectoryName(string folderPath)
 {
     try
     {
         NullChecks.Strings(folderPath);
         string anonymisedDirectoryName = Path.GetFileName(folderPath);
         // Get the path where the original directory name is stored
         string storageFileName = $"{anonymisedDirectoryName}.txt";
         string storageFilePath = Path.Combine(folderPath, storageFileName);
         if (File.Exists(storageFilePath))
         {
             string originalDirectoryName = File.ReadAllText(storageFilePath);
             string originalDirectoryPath = folderPath.Replace(anonymisedDirectoryName, originalDirectoryName);
             Directory.Move(folderPath, originalDirectoryPath);
             storageFilePath = Path.Combine(originalDirectoryPath, storageFileName);
             if (File.Exists(storageFilePath))
             {
                 File.Delete(storageFilePath);
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(folderPath, ex.GetType().Name, "Unable to restore original directory name.");
     }
 }
示例#2
0
 public static void SetAutoClearClipboard(ComboBox cmbAutoClearClipboard)
 {
     NullChecks.ComboBoxes(cmbAutoClearClipboard);
     if (Globals.ClearClipboardInterval == 15000)
     {
         cmbAutoClearClipboard.SelectedIndex = 1;
     }
     else if (Globals.ClearClipboardInterval == 30000)
     {
         cmbAutoClearClipboard.SelectedIndex = 2;
     }
     else if (Globals.ClearClipboardInterval == 60000)
     {
         cmbAutoClearClipboard.SelectedIndex = 3;
     }
     else if (Globals.ClearClipboardInterval == 90000)
     {
         cmbAutoClearClipboard.SelectedIndex = 4;
     }
     else if (Globals.ClearClipboardInterval == 120000)
     {
         cmbAutoClearClipboard.SelectedIndex = 5;
     }
     else
     {
         Globals.ClearClipboardInterval      = 1;
         cmbAutoClearClipboard.SelectedIndex = 0;
     }
 }
示例#3
0
 public static char[] ConvertUserInput(bool encryption, char[] base64Key, char[] password)
 {
     try
     {
         NullChecks.CharArray(base64Key);
         NullChecks.CharArray(password);
         char[] message;
         byte[] key = Convert.FromBase64CharArray(base64Key, 0, base64Key.Length);
         if (encryption == true)
         {
             byte[] plaintext = Encoding.UTF8.GetBytes(password);
             message = EncryptPassword(plaintext, key);
             Utilities.ZeroArray(plaintext);
         }
         else
         {
             byte[] ciphertext = Convert.FromBase64CharArray(password, 0, password.Length);
             message = DecryptPassword(ciphertext, key);
             Utilities.ZeroArray(ciphertext);
         }
         Utilities.ZeroArray(key);
         return(message);
     }
     catch (Exception ex) when(ex is FormatException || ex is EncoderFallbackException)
     {
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Invalid key or password format.");
         return(Array.Empty <char>());
     }
 }
示例#4
0
 public static void WriteHeaders(FileStream ciphertext, byte[] salt, byte[] nonce)
 {
     NullChecks.FileHeaders(ciphertext, salt, nonce);
     ConvertArgon2Parameters(out byte[] memorySize, out byte[] iterations, out byte[] endFlag);
     ciphertext.Write(memorySize, 0, memorySize.Length);
     ciphertext.Write(iterations, 0, iterations.Length);
     ciphertext.Write(endFlag, 0, endFlag.Length);
     ciphertext.Write(salt, 0, salt.Length);
     ciphertext.Write(nonce, 0, nonce.Length);
 }
示例#5
0
 public static void SetAutoClearPassword(ComboBox cmbShredFilesMethod)
 {
     NullChecks.ComboBoxes(cmbShredFilesMethod);
     if (Globals.AutoClearPassword == true)
     {
         cmbShredFilesMethod.SelectedIndex = 0;
     }
     else
     {
         cmbShredFilesMethod.SelectedIndex = 1;
     }
 }
示例#6
0
 public static void SetOverwriteFiles(ComboBox cmbOverwriteFiles)
 {
     NullChecks.ComboBoxes(cmbOverwriteFiles);
     if (Globals.OverwriteFiles == true)
     {
         cmbOverwriteFiles.SelectedIndex = 0;
     }
     else
     {
         cmbOverwriteFiles.SelectedIndex = 1;
     }
 }
示例#7
0
 public static void SetAnonymousRename(ComboBox cmbAnonymousRename)
 {
     NullChecks.ComboBoxes(cmbAnonymousRename);
     if (Globals.AnonymousRename == true)
     {
         cmbAnonymousRename.SelectedIndex = 0;
     }
     else
     {
         cmbAnonymousRename.SelectedIndex = 1;
     }
 }
示例#8
0
 public static void SetMemoryEncryption(ComboBox cmbMemoryEncryption)
 {
     NullChecks.ComboBoxes(cmbMemoryEncryption);
     if (Globals.MemoryEncryption == true)
     {
         cmbMemoryEncryption.SelectedIndex = 0;
     }
     else
     {
         cmbMemoryEncryption.SelectedIndex = 1;
     }
 }
示例#9
0
 public static void SetTheme(ComboBox cmbTheme)
 {
     NullChecks.ComboBoxes(cmbTheme);
     if (Globals.DarkTheme == true)
     {
         cmbTheme.SelectedIndex = 0;
     }
     else
     {
         cmbTheme.SelectedIndex = 1;
     }
 }
示例#10
0
 public static void SetCheckForUpdates(ComboBox cmbCheckForUpdates)
 {
     NullChecks.ComboBoxes(cmbCheckForUpdates);
     if (Globals.CheckForUpdates == true)
     {
         cmbCheckForUpdates.SelectedIndex = 0;
     }
     else
     {
         cmbCheckForUpdates.SelectedIndex = 1;
     }
 }
示例#11
0
 public static void SetExitClearClipboard(ComboBox cmbExitClearClipboard)
 {
     NullChecks.ComboBoxes(cmbExitClearClipboard);
     if (Globals.ExitClearClipboard == true)
     {
         cmbExitClearClipboard.SelectedIndex = 0;
     }
     else
     {
         cmbExitClearClipboard.SelectedIndex = 1;
     }
 }
示例#12
0
 public static void SetShowPassword(ComboBox cmbShowPassword)
 {
     NullChecks.ComboBoxes(cmbShowPassword);
     if (Globals.ShowPasswordByDefault == true)
     {
         cmbShowPassword.SelectedIndex = 0;
     }
     else
     {
         cmbShowPassword.SelectedIndex = 1;
     }
 }
示例#13
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);
            }
        }
示例#14
0
 public static void SetIterations(NumericUpDown nudArgon2Iterations)
 {
     try
     {
         NullChecks.NumericUpDowns(nudArgon2Iterations);
         nudArgon2Iterations.Value = Globals.Iterations;
     }
     catch (ArgumentOutOfRangeException ex)
     {
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Invalid 'Iterations' setting. The default setting will be used instead.");
         Globals.Iterations = Constants.DefaultIterations;
         Settings.SaveSettings();
         SetIterations(nudArgon2Iterations);
     }
 }
示例#15
0
 public static void SetEncryptionAlgorithm(ComboBox cmbEncryptionAlgorithm)
 {
     try
     {
         NullChecks.ComboBoxes(cmbEncryptionAlgorithm);
         cmbEncryptionAlgorithm.SelectedIndex = Globals.EncryptionAlgorithm;
     }
     catch (ArgumentOutOfRangeException ex)
     {
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Invalid 'Encryption Algorithm' setting. The default setting will be used instead.");
         Globals.EncryptionAlgorithm = (int)Cipher.XChaCha20;
         Settings.SaveSettings();
         SetEncryptionAlgorithm(cmbEncryptionAlgorithm);
     }
 }
示例#16
0
 public static void SetShredFilesMethod(ComboBox cmbShredFilesMethod)
 {
     try
     {
         NullChecks.ComboBoxes(cmbShredFilesMethod);
         cmbShredFilesMethod.SelectedIndex = Globals.ShredFilesMethod;
     }
     catch (ArgumentOutOfRangeException ex)
     {
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Invalid 'Shred Files Method' setting. The default setting will be used instead.");
         Globals.ShredFilesMethod = 2;
         Settings.SaveSettings();
         SetShredFilesMethod(cmbShredFilesMethod);
     }
 }
示例#17
0
 public static void SetMemorySize(NumericUpDown nudArgon2MemorySize)
 {
     try
     {
         NullChecks.NumericUpDowns(nudArgon2MemorySize);
         nudArgon2MemorySize.Value = Globals.MemorySize / Constants.Mebibyte;
     }
     catch (ArgumentOutOfRangeException ex)
     {
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Invalid 'Memory Size' setting. The default setting will be used instead.");
         Globals.MemorySize = Constants.DefaultMemorySize;
         Settings.SaveSettings();
         SetMemorySize(nudArgon2MemorySize);
     }
 }
示例#18
0
        public static byte[] ReadNonce(string filePath, byte[] salt, int parametersLength)
        {
            NullChecks.ByteArray(salt);
            int headerLength = 0;
            int offset       = salt.Length + parametersLength;

            if (Globals.EncryptionAlgorithm == (int)Cipher.XChaCha20 || Globals.EncryptionAlgorithm == (int)Cipher.XSalsa20)
            {
                headerLength = Constants.XChaChaNonceLength;
            }
            else if (Globals.EncryptionAlgorithm == (int)Cipher.AesCBC)
            {
                headerLength = Constants.AesNonceLength;
            }
            return(ReadHeader(filePath, headerLength, offset));
        }
示例#19
0
 public static string GetAnonymousFileName(string filePath)
 {
     try
     {
         NullChecks.Strings(filePath);
         string originalFileName  = Path.GetFileName(filePath);
         string randomFileName    = GenerateRandomFileName();
         string anonymousFilePath = filePath.Replace(originalFileName, randomFileName);
         return(anonymousFilePath);
     }
     catch (ArgumentException ex)
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Bug);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to get anonymous file name. This is a bug - please report it.");
         return(filePath);
     }
 }
示例#20
0
        public static byte[] GetBufferSize(FileStream fileStream)
        {
            NullChecks.FileStreams(fileStream);
            int bufferSize = 4096;

            // Use a larger buffer for bigger files
            if (fileStream.Length >= Constants.Mebibyte)
            {
                // 128 KiB
                bufferSize = 131072;
            }
            else if (bufferSize > fileStream.Length)
            {
                // Use file size as buffer for small files
                return(new byte[fileStream.Length]);
            }
            return(new byte[bufferSize]);
        }
示例#21
0
 public static bool AppendHash(string filePath, byte[] fileHash)
 {
     try
     {
         NullChecks.ByteArray(fileHash);
         using (var fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read))
         {
             fileStream.Write(fileHash, 0, fileHash.Length);
         }
         return(true);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to append the MAC to the file. This data is required for decryption of the file.");
         return(false);
     }
 }
示例#22
0
 public static void BackgroundWorkerReportProgress(long progress, long total, BackgroundWorker backgroundWorker)
 {
     try
     {
         NullChecks.BackgroundWorkers(backgroundWorker);
         int percentage = (int)Math.Round((double)((double)progress / total) * 100);
         // Prevent unnecessary calls
         if (percentage != 0 && percentage != _previousPercentage)
         {
             backgroundWorker.ReportProgress(percentage);
         }
         _previousPercentage = percentage;
     }
     catch (Exception ex) when(ExceptionFilters.ReportProgressExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Bug);
         DisplayMessage.ErrorResultsText(string.Empty, ex.GetType().Name, "Background worker report progress exception. This is a bug - please report it.");
     }
 }
示例#23
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);
             }
         }
     }
 }