コード例 #1
0
        public void ConfigureCryptoContainer_NullConfiguration_ArgumentNullExceptionThrown()
        {
            var testContainer = new RijndaelManaged();
            SimpleAesEncryptionConfiguration invalidTestConfig = null;

            _target.ConfigureCryptoContainer(testContainer, invalidTestConfig);
        }
コード例 #2
0
        public void __init()
        {
            testConfig = new SimpleAesEncryptionConfiguration();
            testConfig.EncryptionKey = new EncryptionKeyConfigurationElement(256, "3q2+796tvu/erb7v3q2+796tvu/erb7v3q2+796tvu8=");

            _target = new RijndaelMessageDecryptor(testConfig);
        }
コード例 #3
0
        /// <summary>
        /// Generates the encryption configuration.
        /// </summary>
        /// <param name="options">The commandline options to use for the encryption.</param>
        /// <returns>The configuration required for the Simple AES library</returns>
        public SimpleAesEncryptionConfiguration GenerateEncryptionConfiguration(LogDecryptOptions options)
        {
            SimpleAesEncryptionConfiguration result;

            if (string.IsNullOrWhiteSpace(options.Key))
            {
                // No key is specified, read the whole config from file
                result = ConfigurationManager.GetSection(DEFAULT_CONFIG_SECTION_NAME) as SimpleAesEncryptionConfiguration;
            }
            else
            {
                // Use the command line options
                result = new SimpleAesEncryptionConfiguration()
                {
                    CipherMode    = options.Mode,
                    Padding       = options.Padding,
                    EncryptionKey = new EncryptionKeyConfigurationElement()
                    {
                        KeySize = options.KeySize,
                        Key     = options.Key
                    }
                };
            }

            return(result);
        }
コード例 #4
0
        public void ConfigureCryptoContainer_NullEncryptionKey_CryptographicExceptionThrown()
        {
            var testContainer = new RijndaelManaged();
            SimpleAesEncryptionConfiguration invalidTestConfig = new SimpleAesEncryptionConfiguration
            {
                EncryptionKey = null,
            };

            _target.ConfigureCryptoContainer(testContainer, invalidTestConfig);
        }
コード例 #5
0
        public void ConfigureCryptoContainer_InvalidKeyLength_CryptographicExceptionThrown()
        {
            var testContainer = new RijndaelManaged();
            SimpleAesEncryptionConfiguration invalidTestConfig = new SimpleAesEncryptionConfiguration
            {
                EncryptionKey = new EncryptionKeyConfigurationElement(255, Convert.ToBase64String(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF })),
            };

            _target.ConfigureCryptoContainer(testContainer, invalidTestConfig);
        }
コード例 #6
0
        public void ConfigureCryptoContainer_IllegalKeySizeTooSmall_CryptographicExceptionThrown()
        {
            var testContainer = new RijndaelManaged();
            SimpleAesEncryptionConfiguration invalidTestConfig = new SimpleAesEncryptionConfiguration
            {
                EncryptionKey = new EncryptionKeyConfigurationElement(127, "testKey"),
            };

            _target.ConfigureCryptoContainer(testContainer, invalidTestConfig);
        }
コード例 #7
0
        public void ConfigureCryptoContainer_EmptyEncryptionKey_CryptographicExceptionThrown()
        {
            var testContainer = new RijndaelManaged();
            SimpleAesEncryptionConfiguration invalidTestConfig = new SimpleAesEncryptionConfiguration
            {
                EncryptionKey = new EncryptionKeyConfigurationElement(256, string.Empty),
            };

            _target.ConfigureCryptoContainer(testContainer, invalidTestConfig);
        }
コード例 #8
0
        public void ConstructorWithConfig_ConfigStoredInCorrectProperty_ConfigCanBeAccessed()
        {
            var localTestConfig = new SimpleAesEncryptionConfiguration();

            localTestConfig.EncryptionKey = new EncryptionKeyConfigurationElement(256, "3q2+796tvu/erb7v3q2+796tvu/erb7v3q2+796tvu8=");

            var target = new RijndaelMessageDecryptor(localTestConfig);

            Assert.AreSame(localTestConfig, target.Configuration);
        }
コード例 #9
0
        public void ConfigureCryptoContainer_ValidConfiguration_ContainerIsConfigured()
        {
            var testContainer = new RijndaelManaged();
            var testKey       = new byte[32] {
                0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
                0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
                0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
                0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
            };
            var validTestConfig = new SimpleAesEncryptionConfiguration()
            {
                EncryptionKey = new EncryptionKeyConfigurationElement(256, Convert.ToBase64String(testKey)),
                CipherMode    = CipherMode.CBC,
                Padding       = PaddingMode.ISO10126
            };

            _target.ConfigureCryptoContainer(testContainer, validTestConfig);

            Assert.IsTrue(testKey.SequenceEqual(testContainer.Key));
            Assert.AreEqual(validTestConfig.CipherMode, testContainer.Mode);
            Assert.AreEqual(validTestConfig.Padding, testContainer.Padding);
            Assert.AreEqual(validTestConfig.EncryptionKey.KeySize, testContainer.KeySize);
            Assert.IsTrue(testContainer.IV.Length == 16);
        }
コード例 #10
0
        public void __init()
        {
            testConfig = new SimpleAesEncryptionConfiguration();

            _target = new RijndaelMessageHandlerTestHarness(testConfig);
        }
コード例 #11
0
 public RijndaelMessageHandlerTestHarness(SimpleAesEncryptionConfiguration config)
     : base(config)
 {
 }
コード例 #12
0
        static void Main(string[] args)
        {
            int keySize = 256;

            // If the user specified a non default key length
            if (args.Any())
            {
                // If there are missing or additional command-line parameters
                if (args.Length != 2)
                {
                    ExitWithError(1);
                }

                // If the length switch was not selected
                if (!args[0].Equals("-L", StringComparison.OrdinalIgnoreCase))
                {
                    ExitWithError(1);
                }

                // Parse the keySize from the command-line parameters
                // If there was an error, exit
                if (string.IsNullOrWhiteSpace(args[1]) || !Int32.TryParse(args[1], out keySize))
                {
                    ExitWithError(1);
                }
            }

            Console.WriteLine("Generating a new key for Log4Net.MessageEncryptor: ");

            // Read the configuration file for the key size information
            SimpleAesEncryptionConfiguration config = new SimpleAesEncryptionConfiguration()
            {
                EncryptionKey = new EncryptionKeyConfigurationElement(keySize, "")
            };

            using (RijndaelManaged cryptoContainer = new RijndaelManaged())
            {
                var legalKeys = new [] { 128, 192, 256 };
                // Validate KeySize
                if (!legalKeys.Contains(keySize))
                {
                    Console.WriteLine("Invalid Key size (" + keySize + ")");
                    Console.WriteLine("Valid Key sizes are: " + string.Join(", ", legalKeys));

                    return;
                }

                cryptoContainer.KeySize = config.EncryptionKey.KeySize;

                // Generates a new key using the standard .NET method of generating a new symmetric key
                cryptoContainer.GenerateKey();

                var key = Convert.ToBase64String(cryptoContainer.Key);

                // Output the new key to the screen and the clipboard
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine();
                Console.WriteLine(key);
                Console.ResetColor();
            }

            Console.WriteLine();
            Console.WriteLine("Please press any key to exit.");
            Console.ReadKey();
        }