Example #1
0
 private static void Finalize(string directoryPath)
 {
     try
     {
         RestoreDirectoryNames.AllDirectories(directoryPath);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         DisplayMessage.FilePathException(directoryPath, ex.GetType().Name, "Unable to restore the directory names.");
     }
 }
Example #2
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.Error(filePath, ex.GetType().Name, "Unable to make the file read-only.");
     }
 }
Example #3
0
 public static bool?IsKryptorFile(string filePath)
 {
     try
     {
         byte[] magicBytes = FileHeaders.ReadMagicBytes(filePath);
         return(Utilities.Compare(magicBytes, Constants.KryptorMagicBytes));
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         return(null);
     }
 }
 private static void EncryptInputFile(string inputFilePath, string outputFilePath, byte[] ephemeralPublicKey, byte[] salt, byte[] keyEncryptionKey)
 {
     try
     {
         EncryptFile.Initialize(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         FileEncryption.EncryptionSuccessful(inputFilePath, outputFilePath);
     }
     catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
     {
         DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to encrypt the file.");
     }
 }
Example #5
0
 public static bool?IsSignatureFile(string filePath)
 {
     try
     {
         byte[] magicBytes = ReadFileHeader(filePath, offset: 0, Constants.SignatureMagicBytes.Length);
         return(Utilities.Compare(magicBytes, Constants.SignatureMagicBytes));
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         return(null);
     }
 }
Example #6
0
 public static void UsingPrivateKey(string directoryPath, byte[] privateKey)
 {
     try
     {
         string[] filePaths = GetFiles(ref directoryPath);
         EncryptEachFileWithPrivateKey(filePaths, privateKey);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         DisplayMessage.FilePathException(directoryPath, ex.GetType().Name, "Unable to encrypt the directory.");
     }
 }
Example #7
0
 public static void UsingPublicKey(string directoryPath, byte[] sharedSecret, byte[] recipientPrivateKey)
 {
     try
     {
         string[] filePaths = GetFiles(directoryPath);
         DecryptEachFileWithPublicKey(filePaths, sharedSecret, recipientPrivateKey);
         Finalize(directoryPath);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.FilePathException(directoryPath, ex.GetType().Name, "Unable to decrypt the directory.");
     }
 }
Example #8
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.Error(fileToDelete, ex.GetType().Name, "Unable to overwrite and/or delete.");
     }
 }
Example #9
0
 public static void OverwriteFile(string fileToDelete, string fileToCopy)
 {
     try
     {
         File.SetAttributes(fileToDelete, FileAttributes.Normal);
         File.Copy(fileToCopy, fileToDelete, overwrite: true);
         File.Delete(fileToDelete);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.FilePathException(fileToDelete, ex.GetType().Name, "Unable to overwrite the file.");
     }
 }
Example #10
0
 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.Error(folderPath, ex.GetType().Name, "Unable to get subdirectories in selected folder.");
         return(null);
     }
 }
Example #11
0
 private static byte[] UseKeyfile(byte[] passwordBytes, string keyfilePath)
 {
     try
     {
         byte[] keyfileBytes = Keyfiles.ReadKeyfile(keyfilePath);
         return(passwordBytes == null ? keyfileBytes : CombineKeyfileAndPassword(passwordBytes, keyfileBytes));
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to read keyfile. The keyfile has not been used.");
         return(passwordBytes);
     }
 }
Example #12
0
 public static void OverwriteFile(string fileToDelete, string fileToCopy)
 {
     try
     {
         File.Copy(fileToCopy, fileToDelete, overwrite: true);
         DeleteFile(fileToDelete);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Warning);
         DisplayMessage.FilePathException(fileToDelete, ex.GetType().Name, "Unable to overwrite the file.");
     }
 }
Example #13
0
 private static bool?LoadBooleanSetting(string setting)
 {
     try
     {
         return(Invariant.ToBoolean(setting));
     }
     catch (Exception ex) when(ExceptionFilters.SettingsExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         DisplayMessage.Error(ex.GetType().Name, $"Unable to convert {setting} setting to boolean.");
         return(null);
     }
 }
Example #14
0
 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.Error(ex.GetType().Name, "Unable to log exception.");
     }
 }
Example #15
0
 private static char[] EncryptPassword(byte[] plaintext, byte[] publicKey)
 {
     try
     {
         byte[] ciphertext = SealedPublicKeyBox.Create(plaintext, publicKey);
         return(Convert.ToBase64String(ciphertext).ToCharArray());
     }
     catch (Exception ex) when(ExceptionFilters.PasswordSharingExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         GetErrorMessage(ex);
         return(Array.Empty <char>());
     }
 }
Example #16
0
 private static int GetFileNameLength(string filePath, string originalFileName)
 {
     try
     {
         EncodeFileName(filePath, originalFileName, out byte[] newLineBytes, out byte[] fileNameBytes);
         return(newLineBytes.Length + fileNameBytes.Length);
     }
     catch (Exception ex) when(ExceptionFilters.CharacterEncodingExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.Error(filePath, ex.GetType().Name, "Unable to remove the original file name stored in the file. The length of the stored file name could not be calculated.");
         return(0);
     }
 }
Example #17
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.Error(filePath, ex.GetType().Name, "Unable to check if file path is a directory.");
         return(null);
     }
 }
Example #18
0
 public static void RemoveAppendedFileName(string inputFilePath)
 {
     try
     {
         File.SetAttributes(inputFilePath, FileAttributes.Normal);
         string fileName      = Path.GetFileName(inputFilePath);
         byte[] fileNameBytes = Encoding.UTF8.GetBytes(fileName);
         using var inputFile = new FileStream(inputFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.RandomAccess);
         inputFile.SetLength(inputFile.Length - fileNameBytes.Length);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to remove appended file name.");
     }
 }
Example #19
0
 private static char[] DecryptPassword(byte[] ciphertext, byte[] privateKey)
 {
     try
     {
         using var keyPair = PublicKeyBox.GenerateKeyPair(privateKey);
         byte[] plaintext = SealedPublicKeyBox.Open(ciphertext, keyPair);
         return(Encoding.UTF8.GetChars(plaintext));
     }
     catch (Exception ex) when(ExceptionFilters.PasswordSharingExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         GetErrorMessage(ex);
         return(Array.Empty <char>());
     }
 }
Example #20
0
 public static void DeleteFile(string filePath)
 {
     try
     {
         if (File.Exists(filePath))
         {
             File.SetAttributes(filePath, FileAttributes.Normal);
             File.Delete(filePath);
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.FilePathException(filePath, ex.GetType().Name, "Unable to delete the file.");
     }
 }
Example #21
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.Error(filePath, ex.GetType().Name, "Unable to delete the file.");
     }
 }
Example #22
0
 public static void DecryptByteArray(ref byte[] byteArray)
 {
     try
     {
         if (Globals.MemoryEncryption == true && byteArray != null)
         {
             byteArray = SealedPublicKeyBox.Open(byteArray, _keyPair);
         }
     }
     catch (Exception ex) when(ExceptionFilters.MemoryEncryptionExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Bug);
         DisplayMessage.Error(ex.GetType().Name, "Memory decryption failed. This is a bug - please report it.");
     }
 }
Example #23
0
 public static void LogException(string exceptionMessage, Severity severity)
 {
     try
     {
         string logMessage = $"Exception Severity = {severity}" + Environment.NewLine + exceptionMessage + Environment.NewLine;
         if (!Directory.Exists(_logdirectoryPath))
         {
             Directory.CreateDirectory(_logdirectoryPath);
         }
         File.AppendAllText(_logFilePath, logMessage);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to log exception.");
     }
 }
 private static string ObfuscateDirectoryName(string directoryPath)
 {
     try
     {
         string directoryName  = Path.GetFileName(directoryPath);
         string obfuscatedPath = ObfuscateFileName.ReplaceFilePath(directoryPath);
         Directory.Move(directoryPath, obfuscatedPath);
         StoreDirectoryName(directoryName, obfuscatedPath);
         return(obfuscatedPath);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         DisplayMessage.FilePathException(directoryPath, ex.GetType().Name, "Unable to obfuscate directory name.");
         return(directoryPath);
     }
 }
Example #25
0
 public static void GenerateKeyfile(string filePath)
 {
     try
     {
         byte[] keyfileBytes = SodiumCore.GetRandomBytes(Constants.MACKeySize);
         File.WriteAllBytes(filePath, keyfileBytes);
         File.SetAttributes(filePath, FileAttributes.ReadOnly);
         Utilities.ZeroArray(keyfileBytes);
         Console.WriteLine("Keyfile successfully generated.");
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Low);
         DisplayMessage.Error(ex.GetType().Name, "Keyfile generation failed.");
     }
 }
Example #26
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.Error(filePath, ex.GetType().Name, "Unable to read original file name.");
         return(string.Empty);
     }
 }
Example #27
0
 public static void CheckForUpdates()
 {
     try
     {
         bool updateAvailable = Updates.CheckForUpdates();
         if (updateAvailable)
         {
             Console.WriteLine("An update is available for Kryptor. Visit <https://github.com/samuel-lucas6/Kryptor/releases> to update.");
             return;
         }
         Console.WriteLine("Kryptor is up-to-date.");
     }
     catch (Exception ex) when(ExceptionFilters.CheckForUpdates(ex))
     {
         DisplayMessage.Exception(ex.GetType().Name, "Unable to check for updates.");
     }
 }
Example #28
0
 public static string GetOutputFilePath(string inputFilePath)
 {
     try
     {
         if (Globals.ObfuscateFileNames)
         {
             ObfuscateFileName.AppendFileName(inputFilePath);
             inputFilePath = ObfuscateFileName.ReplaceFilePath(inputFilePath);
         }
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex) || ex is EncoderFallbackException)
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to store file name.");
     }
     return(inputFilePath + Constants.EncryptedExtension);
 }
Example #29
0
 private static (byte[], byte[], byte[]) GetParametersBytes(string memorySize, string iterations)
 {
     try
     {
         Encoding encoding        = Encoding.UTF8;
         byte[]   memorySizeBytes = encoding.GetBytes(memorySize);
         byte[]   iterationsBytes = encoding.GetBytes(iterations);
         byte[]   endFlagBytes    = encoding.GetBytes(Constants.EndFlag);
         return(memorySizeBytes, iterationsBytes, endFlagBytes);
     }
     catch (Exception ex) when(ExceptionFilters.CharacterEncodingExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.Error(ex.GetType().Name, "Unable to convert Argon2 parameters to bytes.");
         return(null, null, null);
     }
 }
Example #30
0
 public static void EncryptByteArray(ref byte[] byteArray)
 {
     try
     {
         if (Globals.MemoryEncryption == true && byteArray != null)
         {
             byteArray = SealedPublicKeyBox.Create(byteArray, _keyPair.PublicKey);
         }
     }
     catch (Exception ex) when(ExceptionFilters.MemoryEncryptionExceptions(ex))
     {
         Globals.MemoryEncryption = false;
         KryptorSettings.SaveSettings();
         Logging.LogException(ex.ToString(), Logging.Severity.Bug);
         DisplayMessage.Error(ex.GetType().Name, "Memory encryption has been disabled due to an exception. This is a bug - please report it.");
     }
 }