Ejemplo n.º 1
0
 public static void ShowEncryptMessage(Settings settings)
 {
     Console.ForegroundColor = ConsoleColor.Red;
     Console.Write("Encrypting... ");
     Console.WriteLine("{0} -> {1}", settings.InputFileName, OutputFilenameGenerator.Generate(settings.InputFileName, EncryptAction.Encrypt));
     Console.ResetColor();
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var settings = new Settings();
            var optionSet = OptionSetHelper.DefineOptions(settings);

            try
            {
                optionSet.Parse(args);
            }
            catch (OptionException e)
            {
                ConsoleWriter.WriteException(e);
            }

            if (settings.ShowHelp)
            {
                ConsoleWriter.ShowHelp(optionSet);
                return;
            }

            var validator = new SettingsValidator();

            var errorMessages = validator.Validate(settings);
            if (errorMessages.Count > 0)
            {
                errorMessages.ForEach(Console.WriteLine);
                ConsoleWriter.ShowHelp(optionSet);
                return;
            }

            try
            {
                var saltStore = new SaltStore(settings.SaltFileName);
                var encrypter = new FileEncryptRijndael(settings.Password, saltStore.CreateAndGet());

                switch (settings.EncryptAction)
                {
                    case EncryptAction.Decrypt:
                        ConsoleWriter.ShowDecryptMessage(settings);
                        encrypter.Decrypt(settings.InputFileName);
                        break;

                    case EncryptAction.Encrypt:
                        ConsoleWriter.ShowEncryptMessage(settings);
                        encrypter.Encrypt(settings.InputFileName);
                        break;
                }
            }
            //catch (CryptographicException ex)
            //catch (IOException ex)
            catch (Exception ex)
            {
                ConsoleWriter.ShowException(ex);
            }
        }
Ejemplo n.º 3
0
        public List<string> Validate(Settings settings)
        {
            if (settings.EncryptAction == EncryptAction.None)
            {
                ErrorMessages.Add("-encrypt -decrypt option is missing.");
            }

            if (string.IsNullOrEmpty(settings.InputFileName))
            {
                ErrorMessages.Add("Option -i is missing.");
            }

            if (string.IsNullOrEmpty(settings.Password))
            {
                ErrorMessages.Add("Option -p is missing.");
            }

            if (settings.EncryptAction == EncryptAction.Decrypt && !settings.InputFileName.Contains("_encrypted"))
            {
                ErrorMessages.Add("Encrypted file must end with '_encrypted' prefix (e.g. 'passwords_encrypted.txt')");
            }

            return ErrorMessages;
        }
Ejemplo n.º 4
0
 public static OptionSet DefineOptions(Settings settings)
 {
     return new OptionSet
     {
         {
             "encrypt",
             "Encrypt  the file",
             v => settings.EncryptAction = EncryptAction.Encrypt
         },
         {
             "decrypt",
             "Decrypt  the file",
             v => settings.EncryptAction = EncryptAction.Decrypt
         },
         {
             "i|in=",
             "the input {FILENAME}",
             v => settings.InputFileName = v
         },
         {
             "p|password="******"the {PASSWORD}",
             v => settings.Password = v
         },
         {
             "s|salt=",
             "the {SALT} file (default is 'salt')",
             v => settings.SaltFileName = v
         },
         {
             "h|help",
             "show this message and exit",
             v => settings.ShowHelp = v != null
         }
     };
 }