private static void UsingPrivateKey(string inputFilePath, byte[] sharedSecret, byte[] recipientPrivateKey) { try { bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath); if (fileIsDirectory) { DirectoryDecryption.UsingPrivateKey(inputFilePath, sharedSecret, recipientPrivateKey); return; } byte[] ephemeralPublicKey = FileHeaders.ReadEphemeralPublicKey(inputFilePath); byte[] ephemeralSharedSecret = KeyExchange.GetSharedSecret(recipientPrivateKey, ephemeralPublicKey); byte[] salt = FileHeaders.ReadSalt(inputFilePath); byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, salt); string outputFilePath = GetOutputFilePath(inputFilePath); DecryptFile.Initialize(inputFilePath, outputFilePath, keyEncryptionKey); Utilities.ZeroArray(keyEncryptionKey); DecryptionSuccessful(inputFilePath, outputFilePath); } catch (Exception ex) when(ExceptionFilters.FileAccess(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.Error); if (ex is ArgumentException || ex is ArgumentOutOfRangeException) { DisplayMessage.FilePathMessage(inputFilePath, ex.Message); return; } DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to decrypt the file."); } }
private static void UsingPassword(string inputFilePath, byte[] passwordBytes) { try { bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath); if (fileIsDirectory) { DirectoryDecryption.UsingPassword(inputFilePath, passwordBytes); return; } byte[] salt = FileHeaders.ReadSalt(inputFilePath); byte[] keyEncryptionKey = Argon2.DeriveKey(passwordBytes, salt); string outputFilePath = GetOutputFilePath(inputFilePath); DecryptFile.Initialize(inputFilePath, outputFilePath, keyEncryptionKey); Utilities.ZeroArray(keyEncryptionKey); DecryptionSuccessful(inputFilePath, outputFilePath); } catch (Exception ex) when(ExceptionFilters.FileAccess(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.Error); if (ex is ArgumentException || ex is ArgumentOutOfRangeException) { DisplayMessage.FilePathMessage(inputFilePath, ex.Message); return; } DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to decrypt the file."); } }
private static void UsingPublicKey(string inputFilePath, byte[] sharedSecret, byte[] recipientPrivateKey) { try { bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath); if (fileIsDirectory) { DirectoryDecryption.UsingPublicKey(inputFilePath, sharedSecret, recipientPrivateKey); return; } using var inputFile = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.RandomAccess); byte[] ephemeralPublicKey = FileHeaders.ReadEphemeralPublicKey(inputFile); byte[] ephemeralSharedSecret = KeyExchange.GetSharedSecret(recipientPrivateKey, ephemeralPublicKey); byte[] salt = FileHeaders.ReadSalt(inputFile); byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, 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); } }
private static void DecryptInputFile(FileStream inputFile, byte[] ephemeralPublicKey, byte[] keyEncryptionKey) { string inputFilePath = inputFile.Name; string outputFilePath = FileDecryption.GetOutputFilePath(inputFilePath); DecryptFile.Initialize(inputFile, outputFilePath, ephemeralPublicKey, keyEncryptionKey); FileDecryption.DecryptionSuccessful(inputFilePath, outputFilePath); }
private static void DecryptInputFile(string inputFilePath, byte[] keyEncryptionKey) { try { string outputFilePath = FileDecryption.GetOutputFilePath(inputFilePath); DecryptFile.Initialize(inputFilePath, outputFilePath, keyEncryptionKey); FileDecryption.DecryptionSuccessful(inputFilePath, outputFilePath); } catch (Exception ex) when(ExceptionFilters.Cryptography(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.Error); if (ex is ArgumentException || ex is ArgumentOutOfRangeException) { DisplayMessage.FilePathMessage(inputFilePath, ex.Message); return; } DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to decrypt the file."); } }