Ejemplo n.º 1
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.");
     }
 }
Ejemplo n.º 2
0
 public static void SignEachFile(string[] filePaths, string signatureFilePath, string comment, bool preHash, byte[] privateKey)
 {
     privateKey = PrivateKey.Decrypt(privateKey);
     if (privateKey == null)
     {
         return;
     }
     if (string.IsNullOrEmpty(comment))
     {
         comment = _defaultComment;
     }
     foreach (string filePath in filePaths)
     {
         try
         {
             DigitalSignatures.SignFile(filePath, signatureFilePath, comment, preHash, privateKey);
             DisplayMessage.FilePathMessage(filePath, "File signed successfully.");
         }
         catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
         {
             DisplayMessage.FilePathException(filePath, ex.GetType().Name, "Unable to create signature.");
         }
     }
     CryptographicOperations.ZeroMemory(privateKey);
 }
Ejemplo n.º 3
0
 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.");
     }
 }
Ejemplo n.º 4
0
 public static void VerifyFile(byte[] publicKey, string signatureFilePath, string filePath)
 {
     try
     {
         bool validSignature = DigitalSignatures.VerifySignature(signatureFilePath, filePath, publicKey);
         if (!validSignature)
         {
             DisplayMessage.FilePathMessage(filePath, "Bad signature.");
             return;
         }
         DisplayMessage.FilePathMessage(filePath, "Good signature.");
         string comment = DigitalSignatures.GetComment();
         DisplayMessage.Message($"Authenticated comment: {comment}");
     }
     catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         if (ex is ArgumentOutOfRangeException)
         {
             DisplayMessage.Exception(ex.GetType().Name, ex.Message);
             return;
         }
         DisplayMessage.Exception(ex.GetType().Name, "Unable to verify signature.");
     }
 }
Ejemplo n.º 5
0
 public static void SignEachFile(byte[] privateKey, string comment, bool preHash, string[] filePaths)
 {
     privateKey = PrivateKey.Decrypt(privateKey);
     if (privateKey == null)
     {
         return;
     }
     if (string.IsNullOrEmpty(comment))
     {
         comment = _defaultComment;
     }
     foreach (string filePath in filePaths)
     {
         try
         {
             DigitalSignatures.SignFile(filePath, comment, preHash, privateKey);
             DisplayMessage.FilePathMessage(filePath, "File signed successfully.");
         }
         catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
         {
             Logging.LogException(ex.ToString(), Logging.Severity.Error);
             DisplayMessage.FilePathException(filePath, ex.GetType().Name, "Unable to create signature.");
         }
     }
     Utilities.ZeroArray(privateKey);
 }
Ejemplo n.º 6
0
 private static void FileException(string inputFilePath, Exception ex)
 {
     if (ex is ArgumentException)
     {
         DisplayMessage.FilePathMessage(inputFilePath, ex.Message);
         return;
     }
     DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to decrypt the file.");
 }
Ejemplo n.º 7
0
 private static void FileException(string inputFilePath, Exception ex)
 {
     Logging.LogException(ex.ToString(), Logging.Severity.Error);
     if (ex is ArgumentException)
     {
         DisplayMessage.FilePathMessage(inputFilePath, ex.Message);
         return;
     }
     DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to decrypt the file.");
 }
Ejemplo n.º 8
0
        private static string BackupDirectory(string directoryPath)
        {
            if (Globals.Overwrite)
            {
                return(null);
            }
            DisplayMessage.FilePathMessage(directoryPath, "Copying directory because you didn't specify -o|--overwrite...");
            string destinationDirectoryPath = FileHandling.GetUniqueDirectoryPath($"{directoryPath} - Copy");

            FileHandling.CopyDirectory(directoryPath, destinationDirectoryPath, copySubdirectories: true);
            if (!Globals.ObfuscateFileNames)
            {
                DisplayMessage.FileEncryptionResult(directoryPath, destinationDirectoryPath);
            }
            return(destinationDirectoryPath);
        }
Ejemplo n.º 9
0
 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.");
     }
 }
Ejemplo n.º 10
0
 public static void VerifyFile(string signatureFilePath, string filePath, byte[] publicKey)
 {
     try
     {
         bool validSignature = DigitalSignatures.VerifySignature(signatureFilePath, filePath, publicKey, out string comment);
         if (!validSignature)
         {
             DisplayMessage.FilePathMessage(filePath, "Bad signature.");
             return;
         }
         DisplayMessage.FilePathMessage(filePath, "Good signature.");
         DisplayMessage.Message($"Authenticated comment: {comment}");
     }
     catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
     {
         if (ex is ArgumentException)
         {
             DisplayMessage.FilePathMessage(signatureFilePath, ex.Message);
             return;
         }
         DisplayMessage.Exception(ex.GetType().Name, "Unable to verify signature.");
     }
 }