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.Error(ex.GetType().Name, "Invalid key or password format."); return(Array.Empty <char>()); } }
public static void CreateKeyfile(string filePath) { NullChecks.Strings(filePath); const string keyfileExtension = ".key"; // Generate a random name if the path is a directory if (Directory.Exists(filePath)) { string randomFileName = AnonymousRename.GenerateRandomFileName() + keyfileExtension; filePath = Path.Combine(filePath, randomFileName); } if (!File.Exists(filePath)) { // Append .key extension if missing if (!filePath.EndsWith(keyfileExtension, StringComparison.InvariantCulture)) { filePath += keyfileExtension; } Keyfiles.GenerateKeyfile(filePath); } else { Console.WriteLine("Error: A file with this name already exists."); } }
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.Error(folderPath, ex.GetType().Name, "Unable to restore original directory name."); } }
public static void WriteHeaders(FileStream ciphertext, byte[] salt) { NullChecks.FileHeaders(ciphertext, salt); byte[] memorySizeFlag = Encoding.UTF8.GetBytes(Constants.MemorySizeFlag + Invariant.ToString(Globals.MemorySize)); byte[] iterationsFlag = Encoding.UTF8.GetBytes(Constants.IterationsFlag + Invariant.ToString(Globals.Iterations)); byte[] endFlag = Encoding.UTF8.GetBytes(Constants.EndFlag); ciphertext.Write(memorySizeFlag, 0, memorySizeFlag.Length); ciphertext.Write(iterationsFlag, 0, iterationsFlag.Length); ciphertext.Write(endFlag, 0, endFlag.Length); ciphertext.Write(salt, 0, salt.Length); }
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.Error(filePath, ex.GetType().Name, "Unable to get anonymous file name. This is a bug - please report it."); return(filePath); } }
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.Error(filePath, ex.GetType().Name, "Unable to append the MAC to the file. This data is required for decryption of the file."); return(false); } }
public static void GetFilePaths(bool encryption, string[] filePaths, byte[] passwordBytes) { NullChecks.StringArray(filePaths); Globals.SuccessfulCount = 0; Globals.TotalCount = filePaths.Length; foreach (string filePath in filePaths) { bool?fileIsDirectory = FileHandling.IsDirectory(filePath); if (fileIsDirectory != null) { if (fileIsDirectory == false) { CallEncryption(encryption, filePath, passwordBytes); } else { DirectoryEncryption(encryption, filePath, passwordBytes); } } } }