public static void UsingPassword(string directoryPath, byte[] passwordBytes) { try { string[] filePaths = GetFiles(directoryPath); string saltFilePath = Path.Combine(directoryPath, Constants.SaltFile); byte[] salt = File.ReadAllBytes(saltFilePath); if (salt.Length != Constants.SaltLength) { throw new ArgumentException("Invalid salt length.", directoryPath); } byte[] keyEncryptionKey = Argon2.DeriveKey(passwordBytes, salt); DecryptEachFileWithPassword(filePaths, keyEncryptionKey); Finalize(directoryPath, saltFilePath); } catch (Exception ex) when(ExceptionFilters.FileAccess(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.Error); DisplayMessage.FilePathException(directoryPath, ex.GetType().Name, "Unable to decrypt the directory."); } }
private static void UsingPassword(string inputFilePath, byte[] passwordBytes) { try { bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath); if (fileIsDirectory) { DirectoryDecryption.UsingPassword(inputFilePath, passwordBytes); return; } using var inputFile = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.RandomAccess); byte[] ephemeralPublicKey = FileHeaders.ReadEphemeralPublicKey(inputFile); byte[] salt = FileHeaders.ReadSalt(inputFile); byte[] keyEncryptionKey = Argon2.DeriveKey(passwordBytes, salt); string outputFilePath = GetOutputFilePath(inputFilePath); DecryptFile.Initialize(inputFile, outputFilePath, ephemeralPublicKey, keyEncryptionKey); CryptographicOperations.ZeroMemory(keyEncryptionKey); DecryptionSuccessful(inputFilePath, outputFilePath); } catch (Exception ex) when(ExceptionFilters.Cryptography(ex)) { FileException(inputFilePath, ex); } }