コード例 #1
0
 public static void RestoreSettings()
 {
     try
     {
         using (var selectFileDialog = new VistaOpenFileDialog())
         {
             selectFileDialog.Title = "Settings Restore";
             if (selectFileDialog.ShowDialog() == DialogResult.OK)
             {
                 if (selectFileDialog.FileName.EndsWith(".ini", StringComparison.Ordinal))
                 {
                     File.Copy(selectFileDialog.FileName, _settingsFile, true);
                     // Reload settings
                     ReadSettings();
                     DisplayMessage.InformationMessageBox("Your settings have been restored.", "Settings Restored");
                 }
                 else
                 {
                     DisplayMessage.ErrorMessageBox("The selected file is not an INI file. Please select a 'settings.ini' file.");
                 }
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to restore settings from the selected file.");
     }
 }
コード例 #2
0
 public static char[] GenerateRandomPassphrase(int wordCount, bool uppercase, bool numbers)
 {
     try
     {
         string wordlistFilePath = Path.Combine(Constants.KryptorDirectory, "wordlist.txt");
         if (File.Exists(wordlistFilePath))
         {
             List <char> passphrase     = new List <char>();
             int         wordlistLength = File.ReadLines(wordlistFilePath).Count();
             int[]       lineNumbers    = GenerateLineNumbers(wordlistLength, wordCount);
             string[]    words          = GetRandomWords(wordlistFilePath, lineNumbers, wordCount, uppercase, numbers);
             Array.Clear(lineNumbers, 0, lineNumbers.Length);
             if (words != null)
             {
                 FormatPassphrase(words, ref passphrase, wordCount);
                 Array.Clear(words, 0, words.Length);
             }
             return(passphrase.ToArray());
         }
         else
         {
             File.WriteAllText(wordlistFilePath, KryptorGUI.Properties.Resources.wordlist);
             return(GenerateRandomPassphrase(wordCount, uppercase, numbers));
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to generate a random passphrase.");
         return(Array.Empty <char>());
     }
 }
コード例 #3
0
 public static void ReadSettings()
 {
     try
     {
         if (File.Exists(_settingsFile))
         {
             string[] settingsLines = File.ReadAllLines(_settingsFile);
             var      settings      = new List <string>();
             foreach (string line in settingsLines)
             {
                 string[] splitLine = line.Split(':');
                 // Ignore the name of each setting - only store values
                 settings.Add(splitLine[1]);
             }
             LoadSettings(settings);
         }
         else
         {
             // Create settings file with default settings
             SaveSettings();
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to read the settings file.");
     }
 }
コード例 #4
0
 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.");
     }
 }
コード例 #5
0
 private static void ExportPublicKey(string publicKey)
 {
     try
     {
         if (!string.IsNullOrEmpty(publicKey))
         {
             using (var saveFileDialog = new VistaSaveFileDialog())
             {
                 saveFileDialog.Title      = "Export Public Key";
                 saveFileDialog.DefaultExt = ".txt";
                 saveFileDialog.FileName   = "KRYPTOR PUBLIC KEY";
                 if (saveFileDialog.ShowDialog() == DialogResult.OK)
                 {
                     File.WriteAllText(saveFileDialog.FileName, publicKey);
                     File.SetAttributes(saveFileDialog.FileName, FileAttributes.ReadOnly);
                 }
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to export public key.");
     }
 }
コード例 #6
0
 private static string[] GetRandomWords(string wordListFilePath, int[] lineNumbers, int wordCount, bool upperCase, bool numbers)
 {
     try
     {
         string[] words = new string[wordCount];
         for (int i = 0; i < wordCount; i++)
         {
             words[i] = File.ReadLines(wordListFilePath).Skip(lineNumbers[i]).Take(1).First();
             // Remove any numbers/spaces on the line
             words[i] = Regex.Replace(words[i], @"[\d-]", string.Empty).Trim();
             if (upperCase == true)
             {
                 words[i] = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(words[i].ToLower(CultureInfo.CurrentCulture));
             }
             if (numbers == true)
             {
                 words[i] += words[i].Length;
             }
         }
         return(words);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex) || ex is RegexMatchTimeoutException)
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to retrieve words from the wordlist.");
         return(null);
     }
 }
コード例 #7
0
ファイル: ReadFileHeaders.cs プロジェクト: smalltsky/Kryptor
 private static void RetrieveArgon2Parameters(string filePath, ref string memorySize, ref string iterations)
 {
     try
     {
         // Read the first line of the file
         using (var streamReader = new StreamReader(filePath, true))
         {
             string firstLine       = streamReader.ReadLine();
             int    memorySizeIndex = firstLine.IndexOf(Constants.MemorySizeFlag, StringComparison.Ordinal);
             if (memorySizeIndex != -1)
             {
                 int iterationsIndex = firstLine.IndexOf(Constants.IterationsFlag, StringComparison.Ordinal);
                 int endIndex        = firstLine.IndexOf(Constants.EndFlag, StringComparison.Ordinal);
                 // If the strings are found on the line
                 if (memorySizeIndex != -1 && iterationsIndex != -1 && endIndex != -1)
                 {
                     memorySize = firstLine.Substring(memorySizeIndex, iterationsIndex - memorySizeIndex);
                     iterations = firstLine.Substring(iterationsIndex, endIndex - iterationsIndex);
                 }
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex) || ExceptionFilters.CharacterEncodingExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to read Argon2 parameters from the file.");
     }
 }
コード例 #8
0
 public static void RestoreOriginalFileName(string decryptedFilePath)
 {
     try
     {
         if (Globals.AnonymousRename == true)
         {
             string originalFileName = ReadOriginalFileName(decryptedFilePath);
             if (!string.IsNullOrEmpty(originalFileName))
             {
                 string anonymousFileName = Path.GetFileName(decryptedFilePath);
                 string originalFilePath  = Regex.Replace(decryptedFilePath, anonymousFileName, originalFileName);
                 if (File.Exists(originalFilePath))
                 {
                     // Replace the file
                     File.Delete(originalFilePath);
                 }
                 File.Move(decryptedFilePath, originalFilePath);
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(decryptedFilePath, ex.GetType().Name, "Unable to restore original file name.");
     }
 }
コード例 #9
0
 public static bool AuthenticateFile(string filePath, byte[] macKey, out byte[] storedHash)
 {
     try
     {
         bool tampered = true;
         storedHash = ReadStoredHash(filePath);
         if (storedHash != null)
         {
             byte[] computedHash = new byte[Constants.HashLength];
             using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, Constants.FileBufferSize, FileOptions.SequentialScan))
             {
                 // Remove the stored MAC from the file before computing the MAC
                 fileStream.SetLength(fileStream.Length - computedHash.Length);
                 MemoryEncryption.DecryptByteArray(ref macKey);
                 computedHash = HashingAlgorithms.Blake2(fileStream, macKey);
                 MemoryEncryption.EncryptByteArray(ref macKey);
             }
             // Invert result
             tampered = !Sodium.Utilities.Compare(storedHash, computedHash);
             if (tampered == true)
             {
                 // Restore the stored MAC
                 AppendHash(filePath, storedHash);
             }
         }
         return(tampered);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to authenticate the file.");
         storedHash = null;
         return(true);
     }
 }
コード例 #10
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.");
     }
 }
コード例 #11
0
 public static void MakeFileReadOnly(string filePath)
 {
     try
     {
         File.SetAttributes(filePath, FileAttributes.ReadOnly);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to make the file read-only.");
     }
 }
コード例 #12
0
 public static void OverwriteFile(string fileToDelete, string fileToCopy)
 {
     try
     {
         File.Copy(fileToCopy, fileToDelete, true);
         File.Delete(fileToDelete);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorResultsText(fileToDelete, ex.GetType().Name, "Unable to overwrite and/or delete.");
     }
 }
コード例 #13
0
ファイル: AnonymousRename.cs プロジェクト: smalltsky/Kryptor
 private static string[] GetAllDirectories(string folderPath)
 {
     try
     {
         return(Directory.GetDirectories(folderPath, "*", SearchOption.AllDirectories));
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(folderPath, ex.GetType().Name, "Unable to get subdirectories in selected folder.");
         return(null);
     }
 }
コード例 #14
0
 public static bool?IsDirectory(string filePath)
 {
     try
     {
         var fileAttributes = File.GetAttributes(filePath);
         return(fileAttributes.HasFlag(FileAttributes.Directory));
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to check if file path is a directory.");
         return(null);
     }
 }
コード例 #15
0
ファイル: Logging.cs プロジェクト: smalltsky/Kryptor
 public static void LogException(string exceptionMessage, Severity severity)
 {
     try
     {
         const string logFileName = "error log.txt";
         string       logFilePath = Path.Combine(Constants.KryptorDirectory, logFileName);
         string       logMessage  = $"[Error] Severity = {severity}" + Environment.NewLine + exceptionMessage + Environment.NewLine;
         File.AppendAllText(logFilePath, logMessage);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to log exception.");
     }
 }
コード例 #16
0
 public static void DeleteFile(string filePath)
 {
     try
     {
         if (File.Exists(filePath))
         {
             File.Delete(filePath);
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to delete the file.");
     }
 }
コード例 #17
0
 public static string ReadOriginalFileName(string filePath)
 {
     try
     {
         // Read the last line of the decrypted file
         string originalFileName = File.ReadLines(filePath).Last().Trim('\0');
         RemoveStoredFileName(filePath, originalFileName);
         return(originalFileName);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex) || ex is InvalidOperationException)
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to read original file name.");
         return(string.Empty);
     }
 }
コード例 #18
0
ファイル: Keyfiles.cs プロジェクト: smalltsky/Kryptor
 private static bool GenerateKeyfile(string filePath)
 {
     try
     {
         byte[] keyfileBytes = SodiumCore.GetRandomBytes(Constants.MACKeySize);
         File.WriteAllBytes(filePath, keyfileBytes);
         File.SetAttributes(filePath, FileAttributes.ReadOnly);
         Utilities.ZeroArray(keyfileBytes);
         return(true);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to generate keyfile.");
         return(false);
     }
 }
コード例 #19
0
        private static void RestoreMAC(string filePath, byte[] macBackup)
        {
            bool restored = FileAuthentication.AppendHash(filePath, macBackup);

            if (restored == false)
            {
                try
                {
                    File.WriteAllBytes($"{filePath}.backup", macBackup);
                }
                catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
                {
                    Logging.LogException(ex.ToString(), Logging.Severity.High);
                    DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Failed to backup the MAC. This data is required for decryption.");
                }
            }
            Utilities.ZeroArray(macBackup);
        }
コード例 #20
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, Constants.FileBufferSize, FileOptions.RandomAccess))
         {
             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);
     }
 }
コード例 #21
0
        public static void DeleteFirstRunFile()
        {
            const string firstRunFile = "first run.tmp";

            try
            {
                // Prevent Argon2 benchmark reoccuring automatically
                string firstRunFilePath = Path.Combine(Constants.KryptorDirectory, firstRunFile);
                if (File.Exists(firstRunFilePath))
                {
                    File.Delete(firstRunFilePath);
                }
            }
            catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
            {
                Logging.LogException(ex.ToString(), Logging.Severity.Medium);
                DisplayMessage.ErrorMessageBox(ex.GetType().Name, $"Unable to delete {Constants.KryptorDirectory}\\{firstRunFile}. Please manually delete this file to prevent the Argon2 benchmark from automatically running again.");
            }
        }
コード例 #22
0
 private static void RemoveStoredFileName(string filePath, string originalFileName)
 {
     try
     {
         int fileNameLength = GetFileNameLength(filePath, originalFileName);
         if (fileNameLength != 0)
         {
             using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, Constants.FileBufferSize, FileOptions.RandomAccess))
             {
                 fileStream.SetLength(fileStream.Length - fileNameLength);
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to remove the original file name stored in the decrypted file. The file should be decrypted, but there is a leftover string at the end of the file.");
     }
 }
コード例 #23
0
ファイル: AnonymousRename.cs プロジェクト: smalltsky/Kryptor
 private static string AnonymiseDirectoryName(string folderPath)
 {
     try
     {
         string originalDirectoryName = Path.GetFileName(folderPath);
         string anonymisedPath        = GetAnonymousFileName(folderPath);
         Directory.Move(folderPath, anonymisedPath);
         // Store the original directory name in a text file inside the directory
         string storageFilePath = Path.Combine(anonymisedPath, $"{Path.GetFileName(anonymisedPath)}.txt");
         File.WriteAllText(storageFilePath, originalDirectoryName);
         return(anonymisedPath);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(folderPath, ex.GetType().Name, "Unable to anonymise directory name.");
         return(folderPath);
     }
 }
コード例 #24
0
ファイル: ReadFileHeaders.cs プロジェクト: smalltsky/Kryptor
 private static byte[] ReadHeader(string filePath, int headerLength, int offset)
 {
     try
     {
         byte[] header = new byte[headerLength];
         using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileBufferSize, FileOptions.RandomAccess))
         {
             fileStream.Seek(offset, SeekOrigin.Begin);
             fileStream.Read(header, 0, header.Length);
         }
         return(header);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to read salt from the selected file.");
         return(null);
     }
 }
コード例 #25
0
 private static byte[] ReadStoredHash(string filePath)
 {
     try
     {
         byte[] storedHash = new byte[Constants.HashLength];
         using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileBufferSize, FileOptions.RandomAccess))
         {
             // Read the last 64 bytes of the file
             fileStream.Seek(fileStream.Length - storedHash.Length, SeekOrigin.Begin);
             fileStream.Read(storedHash, 0, storedHash.Length);
         }
         return(storedHash);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to read the MAC stored in the file.");
         return(null);
     }
 }
コード例 #26
0
 public static bool AppendOriginalFileName(string filePath)
 {
     try
     {
         string fileName = Path.GetFileName(filePath);
         EncodeFileName(filePath, fileName, out byte[] newLineBytes, out byte[] fileNameBytes);
         using (var fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read, Constants.FileBufferSize, FileOptions.RandomAccess))
         {
             fileStream.Write(newLineBytes, 0, newLineBytes.Length);
             fileStream.Write(fileNameBytes, 0, fileNameBytes.Length);
         }
         return(true);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex) || ex is EncoderFallbackException)
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Could not store original file name.");
         return(false);
     }
 }
コード例 #27
0
 private static byte[] ComputeFileHash(string encryptedFilePath, byte[] macKey)
 {
     try
     {
         byte[] computedHash = new byte[Constants.HashLength];
         using (var fileStream = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileBufferSize, FileOptions.SequentialScan))
         {
             MemoryEncryption.DecryptByteArray(ref macKey);
             computedHash = HashingAlgorithms.Blake2(fileStream, macKey);
             MemoryEncryption.EncryptByteArray(ref macKey);
         }
         return(computedHash);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(encryptedFilePath, ex.GetType().Name, "Unable to compute MAC.");
         return(null);
     }
 }
コード例 #28
0
 public static void BackupSettings()
 {
     try
     {
         using (var selectFolderDialog = new VistaFolderBrowserDialog())
         {
             selectFolderDialog.Description = "Settings Backup";
             if (selectFolderDialog.ShowDialog() == DialogResult.OK)
             {
                 string backupFile = Path.Combine(selectFolderDialog.SelectedPath, Path.GetFileName(_settingsFile));
                 File.Copy(_settingsFile, backupFile, true);
             }
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to backup settings file.");
     }
 }
コード例 #29
0
ファイル: Keyfiles.cs プロジェクト: smalltsky/Kryptor
 public static byte[] ReadKeyfile(string keyfilePath)
 {
     try
     {
         File.SetAttributes(keyfilePath, FileAttributes.Normal);
         byte[] keyfileBytes = new byte[Constants.MACKeySize];
         // Read the first 64 bytes of a keyfile
         using (var fileStream = new FileStream(keyfilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             fileStream.Read(keyfileBytes, 0, keyfileBytes.Length);
         }
         File.SetAttributes(keyfilePath, FileAttributes.ReadOnly);
         return(keyfileBytes);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Medium);
         DisplayMessage.ErrorResultsText(string.Empty, ex.GetType().Name, "Unable to read keyfile. The selected keyfile has not been used.");
         return(null);
     }
 }
コード例 #30
0
 private static void StoreBenchmarkResults(int[] benchmarkTimes, List <int> memorySize, int recommendedMemorySize, int delayPerFile)
 {
     try
     {
         var benchmarkResults = new List <string>();
         for (int i = 0; i < benchmarkTimes.Length; i++)
         {
             benchmarkResults.Add($"{memorySize[i] / Constants.Mebibyte} MiB = {benchmarkTimes[i]} ms");
         }
         benchmarkResults.Add(string.Empty);
         benchmarkResults.Add($"Recommended Memory Size: {Invariant.ToString(recommendedMemorySize / Constants.Mebibyte)} MiB");
         benchmarkResults.Add($"This memory size was chosen because it was <= {delayPerFile} ms. This is the delay per file that it takes for Argon2 to derive an encryption key and MAC key. You can speed up key derivation by lowering the memory size, but this will decrease your security. For more information about Argon2, please read the documentation (https://kryptor.co.uk/key-derivation.html).");
         string benchmarkFilePath = Path.Combine(Constants.KryptorDirectory, "benchmark.txt");
         File.WriteAllLines(benchmarkFilePath, benchmarkResults);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         DisplayMessage.ErrorMessageBox(ex.GetType().Name, "Unable to store benchmark results.");
     }
 }