Exemplo n.º 1
0
 private static void UsingPublicKey(string inputFilePath, byte[] sharedSecret, byte[] recipientPublicKey)
 {
     try
     {
         bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath);
         if (fileIsDirectory)
         {
             DirectoryEncryption.UsingPublicKey(inputFilePath, sharedSecret, recipientPublicKey);
             return;
         }
         // Derive a unique KEK per file
         (byte[] ephemeralSharedSecret, byte[] ephemeralPublicKey) = KeyExchange.GetEphemeralSharedSecret(recipientPublicKey);
         byte[] salt             = Generate.Salt();
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, salt);
         string outputFilePath   = GetOutputFilePath(inputFilePath);
         EncryptFile.Initialize(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         Utilities.ZeroArray(keyEncryptionKey);
         EncryptionSuccessful(inputFilePath, outputFilePath);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to encrypt the file.");
     }
 }
Exemplo n.º 2
0
 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.");
     }
 }
Exemplo n.º 3
0
 private static void DecryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         try
         {
             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(privateKey, ephemeralPublicKey);
             byte[] salt             = FileHeaders.ReadSalt(inputFile);
             byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
             DecryptInputFile(inputFile, ephemeralPublicKey, keyEncryptionKey);
             CryptographicOperations.ZeroMemory(keyEncryptionKey);
         }
         catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
         {
             FileException(inputFilePath, ex);
         }
     }
 }
Exemplo n.º 4
0
 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);
     }
 }
Exemplo n.º 5
0
 private static void DecryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         byte[] ephemeralPublicKey    = FileHeaders.ReadEphemeralPublicKey(inputFilePath);
         byte[] ephemeralSharedSecret = KeyExchange.GetSharedSecret(privateKey, ephemeralPublicKey);
         byte[] salt             = FileHeaders.ReadSalt(inputFilePath);
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
         DecryptInputFile(inputFilePath, keyEncryptionKey);
         Utilities.ZeroArray(keyEncryptionKey);
     }
 }
Exemplo n.º 6
0
 private static void EncryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         // Derive a unique KEK per file
         byte[] ephemeralSharedSecret = KeyExchange.GetPrivateKeySharedSecret(privateKey, out byte[] ephemeralPublicKey);
         byte[] salt             = Generate.Salt();
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
         string outputFilePath   = FileEncryption.GetOutputFilePath(inputFilePath);
         EncryptInputFile(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         CryptographicOperations.ZeroMemory(keyEncryptionKey);
     }
 }
Exemplo n.º 7
0
 private static void EncryptEachFileWithPublicKey(string[] filePaths, byte[] sharedSecret, byte[] recipientPublicKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         // Derive a unique KEK per file
         (byte[] ephemeralSharedSecret, byte[] ephemeralPublicKey) = KeyExchange.GetEphemeralSharedSecret(recipientPublicKey);
         byte[] salt             = Generate.Salt();
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, salt);
         string outputFilePath   = FileEncryption.GetOutputFilePath(inputFilePath);
         EncryptInputFile(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         Utilities.ZeroArray(keyEncryptionKey);
     }
 }
Exemplo n.º 8
0
 public static void EncryptEachFileWithPublicKey(string[] filePaths, byte[] senderPrivateKey, byte[] recipientPublicKey)
 {
     Globals.TotalCount = filePaths.Length;
     senderPrivateKey   = PrivateKey.Decrypt(senderPrivateKey);
     if (senderPrivateKey == null)
     {
         return;
     }
     byte[] sharedSecret = KeyExchange.GetSharedSecret(senderPrivateKey, recipientPublicKey);
     Utilities.ZeroArray(senderPrivateKey);
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPublicKey(inputFilePath, sharedSecret, recipientPublicKey);
     }
     Utilities.ZeroArray(sharedSecret);
     DisplayMessage.SuccessfullyEncrypted();
 }
Exemplo n.º 9
0
 public static void DecryptEachFileWithPublicKey(string[] filePaths, byte[] recipientPrivateKey, byte[] senderPublicKey)
 {
     Globals.TotalCount  = filePaths.Length;
     recipientPrivateKey = PrivateKey.Decrypt(recipientPrivateKey);
     if (recipientPrivateKey == null)
     {
         return;
     }
     byte[] sharedSecret = KeyExchange.GetSharedSecret(recipientPrivateKey, senderPublicKey);
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPublicKey(inputFilePath, sharedSecret, recipientPrivateKey);
     }
     CryptographicOperations.ZeroMemory(recipientPrivateKey);
     CryptographicOperations.ZeroMemory(sharedSecret);
     DisplayMessage.SuccessfullyDecrypted();
 }
Exemplo n.º 10
0
 private static void UsingPrivateKey(string inputFilePath, byte[] privateKey)
 {
     try
     {
         bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath);
         if (fileIsDirectory)
         {
             DirectoryEncryption.UsingPrivateKey(inputFilePath, privateKey);
             return;
         }
         // Derive a unique KEK per file
         byte[] ephemeralSharedSecret = KeyExchange.GetPrivateKeySharedSecret(privateKey, out byte[] ephemeralPublicKey);
         byte[] salt             = Generate.Salt();
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
         string outputFilePath   = GetOutputFilePath(inputFilePath);
         EncryptFile.Initialize(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         CryptographicOperations.ZeroMemory(keyEncryptionKey);
         EncryptionSuccessful(inputFilePath, outputFilePath);
     }
     catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
     {
         DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to encrypt the file.");
     }
 }