Exemplo n.º 1
0
        private static void StartEncryption(CryptExecutionContext ctx)
        {
            var tasks = new List <Task>();

            tasks.Add(EncryptionHelper.CreateEncryptionTask(ctx));

            // ... and start them
            foreach (var t in tasks)
            {
                t.Start();
            }

            TaskHelper.WaitAll(tasks);
        }
Exemplo n.º 2
0
        private static int Main(string[] args)
        {
            var sync = new object();

            try
            {
                var result = _EXIT_CODE_INVALID_ARGS;
                var ctx    = new CryptExecutionContext(sync)
                {
                    Arguments = new SynchronizedCollection <string>(syncRoot: new object(),
                                                                    list: args),
                    InitialAction = ShowHelpScreenForInvalidArguments,
                    ExitCode      = result,
                };

                if (args.Length > 0)
                {
                    switch (args[0].ToLower().Trim())
                    {
                    case "/?":
                    case "/help":
                        result = _EXIT_CODE_OK;
                        break;

                    case "/e":
                    case "/encrypt":
                        // crypt
                    {
                        IEnumerable <string> nextArgs;
                        DirectoryInfo        srcDir;
                        DirectoryInfo        destDir;
                        if (args.Length < 3)
                        {
                            srcDir  = new DirectoryInfo(Environment.CurrentDirectory);
                            destDir = new DirectoryInfo(args[1]);

                            nextArgs = args.Skip(2);
                        }
                        else
                        {
                            srcDir  = new DirectoryInfo(args[1]);
                            destDir = new DirectoryInfo(args[2]);

                            nextArgs = args.Skip(3);
                        }

                        byte[] pwd;
                        byte[] salt;
                        ExtractPasswordAndSalt(nextArgs,
                                               pwd: out pwd, salt: out salt);

                        if (pwd != null)
                        {
                            ctx = new CryptExecutionContext(sync)
                            {
                                Arguments = new SynchronizedCollection <string>(syncRoot: sync,
                                                                                list: nextArgs),
                                Destination   = destDir,
                                InitialAction = StartEncryption,
                                Mode          = RunningMode.Encrypt,
                                Password      = pwd,
                                Salt          = salt,
                                Source        = srcDir,
                            };
                        }
                    }
                    break;

                    case "/d":
                    case "/decrypt":
                    {
                        IEnumerable <string> nextArgs;
                        DirectoryInfo        srcDir;
                        DirectoryInfo        destDir;
                        if (args.Length < 3)
                        {
                            srcDir  = new DirectoryInfo(Environment.CurrentDirectory);
                            destDir = new DirectoryInfo(args[1]);

                            nextArgs = args.Skip(2);
                        }
                        else
                        {
                            srcDir  = new DirectoryInfo(args[1]);
                            destDir = new DirectoryInfo(args[2]);

                            nextArgs = args.Skip(3);
                        }

                        byte[] pwd;
                        byte[] salt;
                        ExtractPasswordAndSalt(nextArgs,
                                               pwd: out pwd, salt: out salt);

                        if (pwd != null)
                        {
                            ctx = new CryptExecutionContext(sync)
                            {
                                Arguments = new SynchronizedCollection <string>(syncRoot: sync,
                                                                                list: nextArgs),
                                Destination   = destDir,
                                InitialAction = StartDecryption,
                                Mode          = RunningMode.Decrypt,
                                Password      = pwd,
                                Salt          = salt,
                                Source        = srcDir,
                            };
                        }
                    }
                    break;
                    }
                }

                if (ctx != null)
                {
                    ctx.InvokeInitalAction();

                    if (ctx.ExitCode.HasValue)
                    {
                        result = ctx.ExitCode.Value;
                    }
                }

                return(result);
            }
            catch
            {
                return(_EXIT_CODE_EXCEPTION);
            }
            finally
            {
#if DEBUG
                global::MarcelJoachimKloubert.CLRToolbox.IO.GlobalConsole.Current.WriteLine();
                global::MarcelJoachimKloubert.CLRToolbox.IO.GlobalConsole.Current.WriteLine();
                global::MarcelJoachimKloubert.CLRToolbox.IO.GlobalConsole.Current.WriteLine("===== ENTER ====");
                global::MarcelJoachimKloubert.CLRToolbox.IO.GlobalConsole.Current.ReadLine();
#endif
            }
        }
Exemplo n.º 3
0
 private static void ShowHelpScreenForInvalidArguments(CryptExecutionContext ctx)
 {
     ShowHelpScreen();
     ctx.ExitCode = _EXIT_CODE_INVALID_ARGS;
 }