예제 #1
0
        public void TestAesCrypto()
        {
            var crypto = new AesCryptography();

            const string originalString = "AES cryptography";
            var cryptoString = crypto.Encrypt(originalString);
            var decryptString = crypto.Decrypt(cryptoString);

            Assert.AreEqual(originalString, decryptString);
        }
예제 #2
0
        private ComponentDeclaration UnfoldKey(ComponentAccessKey key)
        {
            if (key == null)
            throw new ArgumentNullException("key");

              AesCryptography aes = new AesCryptography();
              byte[] bytes = aes.Decrypt(SecurityIds.Password, Encoding.UTF8.GetBytes(SecurityIds.Salt), key.Key);

              string keyString = Encoding.UTF8.GetString(bytes);

              ComponentDeclaration accessKey;
              bool result = Enum.TryParse(keyString, true, out accessKey);
              if (!result)
            throw new ArgumentException("Invalid key! Unable to parse key");
              return accessKey;
        }
예제 #3
0
        static void Main(string[] args)
        {
            #region Testing encryption/decryption

              bool shouldTest = false;
              if (shouldTest)
            {
                AesCryptography aes = new AesCryptography();

                var password = "******";
                var salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
                var ct1 = aes.Encrypt(password, salt, Encoding.UTF8.GetBytes("Alice; Bob; Eve;: PerformAct1"));
                Console.WriteLine(Convert.ToBase64String(ct1));

                var ct2 = aes.Encrypt(password, salt, Encoding.UTF8.GetBytes("Alice; Bob; Eve;: PerformAct2"));
                Console.WriteLine(Convert.ToBase64String(ct2));

                var pt1 = aes.Decrypt(password, salt, ct1);
                Console.WriteLine(Encoding.UTF8.GetString(pt1));

                var pt2 = aes.Decrypt(password, salt, ct2);
                Console.WriteLine(Encoding.UTF8.GetString(pt2));

                // Now check tampering
                try
                {
                    ct1[30]++;
                    aes.Decrypt(password, salt, ct1);
                    Console.WriteLine("Error: tamper detection failed.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Success: tampering detected.");
                    Console.WriteLine(ex.ToString());
                }
                Console.ReadLine();
            }
            #endregion

              string COMPARE_SILENT = "SILENT";
              string COMPARE_HELP   = "HELP";

              string[] cmdArgs = Environment.GetCommandLineArgs();
              foreach (string arg in cmdArgs)
              {
            if (arg.ToUpper() == COMPARE_SILENT)
            {
              ConsoleEnabled = false;
            }

            if (arg.ToUpper() == COMPARE_HELP)
            {
              ConsoleEnabled = true;

              #region Help output to console
              //      12345678901234567890123456789012345678901234567890123456789012345678901234567890
              Output("╔══════════════════════════════════════════════════════════════════════════════╗");
              Output("║                                  Code Analyser                               ║");
              Output("╠══════════════════════════════════════════════════════════════════════════════╣");
              Output("║                                                                              ║");
              Output("║ Purpose   The application is designed to search files for suspect code       ║");
              Output("║           constructions, i.e. try-catch statements suppressing exceptions    ║");
              Output("║           from being handled.                                                ║");
              Output("║                                                                              ║");
              Output("║           The type of code constructions that are matched during the search  ║");
              Output("║           is specified through regular expressions in the applications       ║");
              Output("║           configuration file. Multiple regular expressions can be added to   ║");
              Output("║           the configuration file as well as what directories the search      ║");
              Output("║           should include, what directories should be excluded, the type of   ║");
              Output("║           files to include in the search.                                    ║");
              Output("║                                                                              ║");
              Output("║                                                                              ║");
              Output("║  Result:  A resulting xml file containing the result of the search will be   ║");
              Output("║           created in the execution directory, 'Analyser.xml'. Just open it   ║");
              Output("║           in a browser - it will be transformed into html by the associated  ║");
              Output("║           xslt file.                                                         ║");
              Output("║                                                                              ║");
              Output("║                                                                              ║");
              Output("║  How to:  The application can be run with no arguments. The following        ║");
              Output("║           arguments are allowed:                                             ║");
              Output("║                                                                              ║");
              Output("║           <help>   Will show this dialog.                                    ║");
              Output("║                                                                              ║");
              Output("║           <silent> Indicates whether output from the client should be        ║");
              Output("║                    enabled. Adding the argument 'silent' will disable        ║");
              Output("║                    output to the command line.                               ║");
              Output("║                                                                              ║");
              Output("║           NOTE:    Using the 'silent' argument will not disable output from  ║");
              Output("║                    the log system to the 'Console' target! If all messages   ║");
              Output("║                    to the command line should be completely disabled then    ║");
              Output("║                    disable the 'Console' target in the log system            ║");
              Output("║                    configuration file as well.                               ║");
              Output("║                                                                              ║");
              Output("║                                                                              ║");
              Output("║   Setup:  Two configuration files (.config) are needed in order to execute   ║");
              Output("║           the application. Both files is expected to be located in the       ║");
              Output("║           applications execution directory. If not placed here the           ║");
              Output("║           application will fail.                                             ║");
              Output("║                                                                              ║");
              Output("║           <hunter> Configuration file for setting up the include             ║");
              Output("║                    directories, regular expressions etc.                     ║");
              Output("║                                                                              ║");
              Output("║           <log>    Configuration file for setting up the log system that the ║");
              Output("║                    application uses.                                         ║");
              Output("║                                                                              ║");
              Output("╚══════════════════════════════════════════════════════════════════════════════╝");
              #endregion

              ConsoleEnabled = false;
              return;
            }
              }

            try
            {
            DirHandler.Instance.CurrentDirectory = Environment.CurrentDirectory;
            }
            catch (Exception e)
            {
            Console.WriteLine(BaseException.Format(null, -1, @"Failed to initialize 'Directory Handler' with current DIR? Unable to continue.", e));
            Console.ReadLine();
            return;
            }

            ApplicationManager am = null;
            try
            {
            am = new ApplicationManager();
            }
            catch (Exception e)
            {
                Console.WriteLine(BaseException.Format(null, -1, @"Failed to construct the 'Application Manager'? Unable to continue.", e));
            Console.ReadLine();
            return;
            }

              try
              {
            am.Start();
              }
              catch (CoordinationException ce)
              {
            Console.WriteLine(ce.ExceptionSummary());
              Console.ReadLine();
            return;
              }

              Output(ProxyHome.Instance.StatisticsProxy.ExtractTimerMeasurings());

              // Shutdown the log system - should also empty all the queues before stopping.
              Out.Stop();
              Console.ReadLine();
        }
예제 #4
0
        internal ComponentAccessKey GenerateComponentAccessKey(string password, string salt, IKeyConsumer keyConsumer)
        {
            #region Validate input arguments...
              if (string.IsNullOrWhiteSpace(password))
            throw new ArgumentNullException("password");

              if (string.IsNullOrWhiteSpace(salt))
            throw new ArgumentNullException("salt");

              if (keyConsumer == null)
            throw new ArgumentNullException("keyConsumer");
              #endregion

              // All flags are default cleared!
              ComponentDeclaration accessSetup = ComponentDeclaration.NotDefined;

              #region Setup the component access flags for the 'Configuration' component
              if (keyConsumer is IConfigurationProxy)
              {
            accessSetup = accessSetup | ComponentDeclaration.Configuration;
            accessSetup = accessSetup | ComponentDeclaration.Statistics;
            accessSetup = accessSetup | ComponentDeclaration.DataAccess;
              }
              #endregion

              #region Setup the component access flags for the 'Configuration' component
              if (keyConsumer is IConfigurationFactory)
              {
            accessSetup = accessSetup | ComponentDeclaration.Statistics;
              }
              #endregion

              #region Setup the component access flags for the 'Engine' component
              if (keyConsumer is IEngineProxy)
              {
            // Setup the component access flags for the configuration component.
            accessSetup = accessSetup | ComponentDeclaration.Configuration;
            accessSetup = accessSetup | ComponentDeclaration.Engine;
            accessSetup = accessSetup | ComponentDeclaration.Matches;
            accessSetup = accessSetup | ComponentDeclaration.Statistics;
              }
              #endregion

              #region Setup the component access flags for the 'Output' component
              if (keyConsumer is IOutputProxy)
              {
            // Setup the component access flags for the configuration component.
            accessSetup = accessSetup | ComponentDeclaration.Configuration;
            // accessSetup = accessSetup | ComponentDeclaration.Engine;
            accessSetup = accessSetup | ComponentDeclaration.Statistics;
            accessSetup = accessSetup | ComponentDeclaration.Matches;
              }
              #endregion

              #region Setup the component access flags for the 'Matches' component
            if (keyConsumer is IMatchProxy)
              {
            // Setup the component access flags for the configuration component.
            accessSetup = accessSetup | ComponentDeclaration.Statistics;
            accessSetup = accessSetup | ComponentDeclaration.DataAccess;
              }
              #endregion

              #region Setup the component access flags for the 'Statistics' component
              if (keyConsumer is IStatisticsProxy)
              {
            // Setup the component access flags for the configuration component.
            accessSetup = accessSetup | ComponentDeclaration.Statistics;
              }
              #endregion

              #region Setup the component access flags for the 'DataAccess' component
              if (keyConsumer is IDataAccessProxy)
              {
            // Setup the component access flags for the configuration component.
            accessSetup = accessSetup | ComponentDeclaration.DataAccess;
            accessSetup = accessSetup | ComponentDeclaration.Configuration;
              }
              #endregion

              // Create the description for the key - the description must declare
              // what components the flags give access to.
              string description = GenerateKeyDescription(accessSetup);

              // Let's do the actual encryption...
              AesCryptography aes = new AesCryptography();
              byte[]        bytes = aes.Encrypt(password, Encoding.UTF8.GetBytes(salt), Encoding.UTF8.GetBytes(accessSetup + ""));

              // Constructing the actual key for accessing the components...
              return new ComponentAccessKey(bytes, description, keyConsumer);
        }