static void Main(string[] args)
        {
            Console.WriteLine("Generating a new key for Log4Net.MessageEncryptor: ");

            // Read the configuration file for the key size information
            Log4NetMessageEncryptorConfiguration config = (Log4NetMessageEncryptorConfiguration)ConfigurationManager.GetSection("Log4NetMessageEncryption");

            using (RijndaelManaged cryptoContainer = new RijndaelManaged())
            {
                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);
                Clipboard.SetText(key);

                Console.ResetColor();
                Console.WriteLine();
                Console.WriteLine("This Key has been copied to your clipboard.");
            }

            Console.WriteLine();
            Console.WriteLine("Please press any key to exit.");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // Ensure log4net is configured
            XmlConfigurator.Configure();

            Log4NetMessageEncryptorConfiguration config = (Log4NetMessageEncryptorConfiguration)ConfigurationManager.GetSection("Log4NetMessageEncryption");

            Console.WriteLine("Configuration found:");
            Console.WriteLine("Configuration.Padding - {0}", config.Padding);
            Console.WriteLine("Configuration.CipherMode - {0}", config.CipherMode);
            Console.WriteLine("Configuration.EncryptionKey.KeySize - {0}", config.EncryptionKey.KeySize);
            Console.WriteLine("Configuration.EncryptionKey.Key - {0}", config.EncryptionKey.Key);

            log.Debug("Debug message 1");
            log.DebugFormat("Debug message {0}a", 1);
            log.Debug("Debug Exception message", new ApplicationException("Debug message log"));

            log.Info("Info message 1");
            log.InfoFormat("Info message {0}a", 1);
            log.Info("Info Exception message", new ApplicationException("Info message log"));

            log.Warn("Warning message 1");
            log.WarnFormat("Warning message {0}a", 1);
            log.Warn("Warning Exception message", new ApplicationException("Debug message log"));

            log.Error("Error message 1");
            log.ErrorFormat("Error message {0}a", 1);
            log.Error("Error Exception message", new MemberAccessException("Error message log"));

            log.Fatal("Fatal message 1");
            log.FatalFormat("Fatal message {0}a", 1);
            log.Fatal("Fatal Exception message", new StackOverflowException("Fatal message log", new OutOfMemoryException("Out of memory inner exception")));

            Console.WriteLine("Please press any key to close...");
            Console.ReadKey();
        }
Example #3
0
        /// <summary>
        /// Configures the crypto container.
        /// </summary>
        /// <param name="cryptoContainer">The crypto container to configure.</param>
        /// <param name="config">The configuration to use during encryption.</param>
        public virtual void ConfigureCryptoContainer(RijndaelManaged cryptoContainer, Log4NetMessageEncryptorConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config", "The whole encryption configuration is null. Have you forgotten to add it to the config section: " + CONFIGURATION_SECTION_NAME);
            }

            if (config.EncryptionKey == null)
            {
                throw new ArgumentException("config", "The encryption key configuration is null. Have you forgotten to add it to the config section: " + CONFIGURATION_SECTION_NAME);
            }

            if (string.IsNullOrWhiteSpace(config.EncryptionKey.Key))
            {
                throw new CryptographicException("Encryption key is missing. Have you forgotten to add it to the config section: " + CONFIGURATION_SECTION_NAME);
            }

            if (!cryptoContainer.LegalKeySizes.Any(x => (x.MinSize <= config.EncryptionKey.KeySize) && (config.EncryptionKey.KeySize <= x.MaxSize)))
            {
                throw new CryptographicException("Invalid Key Size specified. The recommended value is: 256");
            }

            byte[] key = Convert.FromBase64String(config.EncryptionKey.Key);

            // Check that the key length is equal to config.KeySize / 8
            // e.g. 256/8 == 32 bytes expected for the key
            if (key.Length != (config.EncryptionKey.KeySize / 8))
            {
                throw new CryptographicException("Encryption key is the wrong length. Please ensure that it is *EXACTLY* " + config.EncryptionKey.KeySize + " bits long");
            }

            cryptoContainer.Mode    = config.CipherMode;
            cryptoContainer.Padding = config.Padding;
            cryptoContainer.KeySize = config.EncryptionKey.KeySize;
            cryptoContainer.Key     = key;

            // Generate a new Unique IV for this container and transaction (can be overridden later to decrypt messages where the IV is known)
            cryptoContainer.GenerateIV();
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RijndaelMessageEncryptor"/> class.
 /// </summary>
 /// <param name="config">The configuration.</param>
 public RijndaelMessageHandler(Log4NetMessageEncryptorConfiguration config)
 {
     Configuration = config;
 }
Example #5
0
 public RijndaelMessageDecryptor(Log4NetMessageEncryptorConfiguration config)
     : base(config)
 {
 }