diff --git a/.gitignore b/.gitignore
index 1d2787d..5d1e228 100644
--- a/.gitignore
+++ b/.gitignore
@@ -214,3 +214,4 @@ pip-log.txt
#Mr Developer
.mr.developer.cfg
+/.vs
diff --git a/SimpleAesEncryption.sln b/SimpleAesEncryption.sln
index a15eff9..05f4d94 100644
--- a/SimpleAesEncryption.sln
+++ b/SimpleAesEncryption.sln
@@ -1,4 +1,4 @@
-
+
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30110.0
@@ -33,6 +33,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleExample", "sample\Sim
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CastleWithCustomConfigSection", "sample\CastleWithCustomConfigSection\CastleWithCustomConfigSection.csproj", "{20E15878-B343-4C4B-AD5B-684DE4E30BF3}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleAesEncryption.Core", "src\SimpleAesEncryption.Core\SimpleAesEncryption.Core.csproj", "{F7733072-04CF-4536-B63C-954484CA13AB}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyGen.Core", "src\KeyGen.Core\KeyGen.Core.csproj", "{AB93C1BD-46FE-473F-A394-03A78581AFD6}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleExample.Core", "sample\SimpleExample.Core\SimpleExample.Core.csproj", "{30231807-429A-453F-A1A8-E5A80B751274}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -59,15 +65,33 @@ Global
{20E15878-B343-4C4B-AD5B-684DE4E30BF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20E15878-B343-4C4B-AD5B-684DE4E30BF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20E15878-B343-4C4B-AD5B-684DE4E30BF3}.Release|Any CPU.Build.0 = Release|Any CPU
+ {F7733072-04CF-4536-B63C-954484CA13AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {F7733072-04CF-4536-B63C-954484CA13AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {F7733072-04CF-4536-B63C-954484CA13AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {F7733072-04CF-4536-B63C-954484CA13AB}.Release|Any CPU.Build.0 = Release|Any CPU
+ {AB93C1BD-46FE-473F-A394-03A78581AFD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {AB93C1BD-46FE-473F-A394-03A78581AFD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {AB93C1BD-46FE-473F-A394-03A78581AFD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {AB93C1BD-46FE-473F-A394-03A78581AFD6}.Release|Any CPU.Build.0 = Release|Any CPU
+ {30231807-429A-453F-A1A8-E5A80B751274}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {30231807-429A-453F-A1A8-E5A80B751274}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {30231807-429A-453F-A1A8-E5A80B751274}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {30231807-429A-453F-A1A8-E5A80B751274}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E94E1D6C-7182-491D-A5C4-79B1BE3DC48A} = {C6DD140B-413D-4023-8EA6-50F9871CA803}
- {9E3D5C62-B5BA-4213-A4DB-CB523528713C} = {C6DD140B-413D-4023-8EA6-50F9871CA803}
{2829C552-8959-4E0C-8994-9D6800922CC9} = {E0D81C72-5893-4720-BE6B-74834EF19FD5}
+ {9E3D5C62-B5BA-4213-A4DB-CB523528713C} = {C6DD140B-413D-4023-8EA6-50F9871CA803}
{47B4F4B7-BD6B-4792-8CA6-153EA4FE772B} = {65354631-B9A7-4B93-9889-6623B06B9230}
{20E15878-B343-4C4B-AD5B-684DE4E30BF3} = {65354631-B9A7-4B93-9889-6623B06B9230}
+ {F7733072-04CF-4536-B63C-954484CA13AB} = {C6DD140B-413D-4023-8EA6-50F9871CA803}
+ {AB93C1BD-46FE-473F-A394-03A78581AFD6} = {C6DD140B-413D-4023-8EA6-50F9871CA803}
+ {30231807-429A-453F-A1A8-E5A80B751274} = {65354631-B9A7-4B93-9889-6623B06B9230}
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {817C46EA-8F4C-44FA-99BF-25FC9EDF7790}
EndGlobalSection
EndGlobal
diff --git a/sample/SimpleExample.Core/Program.cs b/sample/SimpleExample.Core/Program.cs
new file mode 100644
index 0000000..78a2ecd
--- /dev/null
+++ b/sample/SimpleExample.Core/Program.cs
@@ -0,0 +1,24 @@
+using ArtisanCode.SimpleAesEncryption;
+
+var input = "Hello World!";
+
+if (args.Length > 0)
+{
+ input = args[0];
+}
+
+var encryptor = new RijndaelMessageEncryptor("CustomMessageEncryption");
+//var encryptor = new RijndaelMessageEncryptor();
+var cyphertext = encryptor.Encrypt(input);
+
+var decryptor = new RijndaelMessageDecryptor("CustomMessageEncryption");
+//var decryptor = new RijndaelMessageDecryptor();
+var plaintext = decryptor.Decrypt(cyphertext);
+
+Console.WriteLine("Input:" + input);
+Console.WriteLine("Cyphertext:" + cyphertext);
+Console.WriteLine("Plaintext:" + plaintext);
+
+Console.WriteLine();
+Console.WriteLine("Please press any key to exit.");
+Console.ReadKey();
\ No newline at end of file
diff --git a/sample/SimpleExample.Core/SimpleExample.Core.csproj b/sample/SimpleExample.Core/SimpleExample.Core.csproj
new file mode 100644
index 0000000..762f9a1
--- /dev/null
+++ b/sample/SimpleExample.Core/SimpleExample.Core.csproj
@@ -0,0 +1,20 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
+
+ Always
+
+
+
+
diff --git a/sample/SimpleExample.Core/appsettings.json b/sample/SimpleExample.Core/appsettings.json
new file mode 100644
index 0000000..4ca4e9e
--- /dev/null
+++ b/sample/SimpleExample.Core/appsettings.json
@@ -0,0 +1,14 @@
+{
+ //"MessageEncryption": {
+ // "EncryptionKey": {
+ // "Key": "GRE5sAmmndnu0t3h1+OzMNfrGHoVn2mdy44qISfVJqs=",
+ // "KeySize": 256
+ // }
+ //}
+ "CustomMessageEncryption": {
+ "EncryptionKey": {
+ "Key": "GRE5sAmmndnu0t3h1+OzMNfrGHoVn2mdy44qISfVJqs=",
+ "KeySize": 256
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/KeyGen.Core/KeyGen.Core.csproj b/src/KeyGen.Core/KeyGen.Core.csproj
new file mode 100644
index 0000000..fc9c4b9
--- /dev/null
+++ b/src/KeyGen.Core/KeyGen.Core.csproj
@@ -0,0 +1,14 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
diff --git a/src/KeyGen.Core/Program.cs b/src/KeyGen.Core/Program.cs
new file mode 100644
index 0000000..48fe44b
--- /dev/null
+++ b/src/KeyGen.Core/Program.cs
@@ -0,0 +1,78 @@
+using System.Security.Cryptography;
+using ArtisanCode.SimpleAesEncryption;
+
+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();
+
+static void ExitWithError(int errorCode)
+{
+ WriteUsageStatement();
+ Environment.Exit(errorCode);
+}
+
+static void WriteUsageStatement()
+{
+ Console.WriteLine("Usage:");
+ Console.WriteLine("No Arguments : Use the default key length of 256 bits (recommended)");
+ Console.WriteLine("-l or -L : Specifies the key length in 128, 182, or 256)");
+}
\ No newline at end of file
diff --git a/src/SimpleAesEncryption.Core/EncryptionKeyConfigurationElement.cs b/src/SimpleAesEncryption.Core/EncryptionKeyConfigurationElement.cs
new file mode 100644
index 0000000..3fa5418
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/EncryptionKeyConfigurationElement.cs
@@ -0,0 +1,33 @@
+namespace ArtisanCode.SimpleAesEncryption
+{
+ public class EncryptionKeyConfigurationElement
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public EncryptionKeyConfigurationElement()
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Size of the key.
+ /// The key.
+ public EncryptionKeyConfigurationElement(int keySize, string key)
+ {
+ this.KeySize = keySize;
+ this.Key = key;
+ }
+
+ ///
+ /// Gets or sets the encryption key.
+ ///
+ public string Key { get; set; }
+
+ ///
+ /// Gets or sets the size of the key in bits.
+ ///
+ public int KeySize { get; set; } = 256;
+ }
+}
diff --git a/src/SimpleAesEncryption.Core/IMessageDecryptor.cs b/src/SimpleAesEncryption.Core/IMessageDecryptor.cs
new file mode 100644
index 0000000..d50c4ff
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/IMessageDecryptor.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace ArtisanCode.SimpleAesEncryption
+{
+ public interface IMessageDecryptor
+ {
+ ///
+ /// Decrypts the specified cypher text.
+ ///
+ /// The cypherText.
+ /// The plaintext decrypted version of the cypher text
+ string Decrypt(string cypherText);
+ }
+}
diff --git a/src/SimpleAesEncryption.Core/IMessageEncryptor.cs b/src/SimpleAesEncryption.Core/IMessageEncryptor.cs
new file mode 100644
index 0000000..cd5c2d6
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/IMessageEncryptor.cs
@@ -0,0 +1,12 @@
+namespace ArtisanCode.SimpleAesEncryption
+{
+ public interface IMessageEncryptor
+ {
+ ///
+ /// Encrypts the specified source.
+ ///
+ /// The source.
+ /// The cypher-text generated from the source
+ string Encrypt(string source);
+ }
+}
diff --git a/src/SimpleAesEncryption.Core/RijndaelMessageDecryptor.cs b/src/SimpleAesEncryption.Core/RijndaelMessageDecryptor.cs
new file mode 100644
index 0000000..cdbbb4b
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/RijndaelMessageDecryptor.cs
@@ -0,0 +1,133 @@
+using System;
+using System.IO;
+using System.Security.Cryptography;
+using Microsoft.Extensions.Configuration;
+
+namespace ArtisanCode.SimpleAesEncryption
+{
+ public class RijndaelMessageDecryptor : RijndaelMessageHandler, IMessageDecryptor
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageDecryptor(): base()
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageDecryptor(IConfiguration configuration) : base(configuration)
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageDecryptor(string configurationSectionPath) : base(configurationSectionPath)
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageDecryptor(SimpleAesEncryptionConfiguration config)
+ : base(config)
+ {
+
+ }
+
+ ///
+ /// Decrypts the specified cypherText.
+ ///
+ /// The cypherText to decrypt.
+ /// The plaintext decrypted version of the cypher text
+ /// Invalid source string. Unable to determine the correct IV used for the encryption. Please ensure the source string is in the format 'Cypher Text' + CYPHER_TEXT_IV_SEPERATOR + 'IV';source
+ public virtual string Decrypt(string cypherText)
+ {
+ // Short-circuit decryption for empty strings
+ if (string.IsNullOrEmpty(cypherText))
+ {
+ return string.Empty;
+ }
+
+ var primatives = cypherText.Split(new[] { CYPHER_TEXT_IV_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
+
+ if (primatives.Length != 2)
+ {
+ throw new ArgumentException("Invalid cypherText. Unable to determine the correct IV used for the encryption. Please ensure the source string is in the format 'Cypher Text'" + CYPHER_TEXT_IV_SEPARATOR + "'IV'", "source");
+ }
+
+ var cypherTextPrimitave = Convert.FromBase64String(primatives[0]);
+ var iv = Convert.FromBase64String(primatives[1]);
+
+ return DecryptStringFromBytes(cypherTextPrimitave, iv);
+ }
+
+ ///
+ /// Decrypts the string from bytes.
+ ///
+ /// The cipher text.
+ /// The key.
+ /// The iv.
+ ///
+ ///
+ /// Original version: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
+ /// 20/05/2014 @ 20:05
+ ///
+ ///
+ /// cipherText
+ /// or
+ /// IV
+ ///
+ public virtual string DecryptStringFromBytes(byte[] cipherText, byte[] IV)
+ {
+ // Check arguments.
+ if (cipherText == null || cipherText.Length <= 0)
+ {
+ throw new ArgumentNullException("cipherText");
+ }
+
+ if (IV == null || IV.Length <= 0)
+ {
+ throw new ArgumentNullException("IV");
+ }
+
+ // Declare the string used to hold
+ // the decrypted text.
+ string plaintext = null;
+
+ // Create an RijndaelManaged object
+ // with the specified key and IV.
+ using (RijndaelManaged cryptoContainer = new RijndaelManaged())
+ {
+ ConfigureCryptoContainer(cryptoContainer);
+
+ // Remember to set the IV to the correct value for decryption
+ cryptoContainer.IV = IV;
+
+ // Create a decrytor to perform the stream transform.
+ ICryptoTransform decryptor = cryptoContainer.CreateDecryptor(cryptoContainer.Key, cryptoContainer.IV);
+
+ // Create the streams used for decryption.
+ using (MemoryStream msDecrypt = new MemoryStream(cipherText))
+ {
+ using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
+ {
+ using (StreamReader srDecrypt = new StreamReader(csDecrypt))
+ {
+ // Read the decrypted bytes from the decrypting stream
+ // and place them in a string.
+ plaintext = srDecrypt.ReadToEnd();
+ }
+ }
+ }
+ }
+
+ return plaintext;
+ }
+ }
+}
diff --git a/src/SimpleAesEncryption.Core/RijndaelMessageEncryptor.cs b/src/SimpleAesEncryption.Core/RijndaelMessageEncryptor.cs
new file mode 100644
index 0000000..a48bdcc
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/RijndaelMessageEncryptor.cs
@@ -0,0 +1,107 @@
+using Microsoft.Extensions.Configuration;
+using System;
+using System.IO;
+using System.Security.Cryptography;
+
+namespace ArtisanCode.SimpleAesEncryption
+{
+ public class RijndaelMessageEncryptor : RijndaelMessageHandler, IMessageEncryptor
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageEncryptor() : base()
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageEncryptor(IConfiguration configuration) : base(configuration)
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageEncryptor(string configurationSectionPath) : base(configurationSectionPath)
+ {
+
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageEncryptor(SimpleAesEncryptionConfiguration config)
+ : base(config)
+ {
+
+ }
+
+ ///
+ /// Encrypts the specified source.
+ ///
+ /// The source.
+ ///
+ public virtual string Encrypt(string source)
+ {
+ // Short-circuit encryption for empty strings
+ if (string.IsNullOrEmpty(source))
+ {
+ return string.Empty;
+ }
+
+ // Encrypt the string to an array of bytes.
+ var output = EncryptStringToBytes(source);
+
+ // Return the Base64 encoded cypher-text along with the (plaintext) unique IV used for this encryption
+ return string.Format("{0}{1}{2}", Convert.ToBase64String(output.Item1), CYPHER_TEXT_IV_SEPARATOR, Convert.ToBase64String(output.Item2));
+ }
+
+ ///
+ /// Encrypts the string to bytes.
+ ///
+ /// The plain text.
+ ///
+ /// Original version: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
+ /// 20/05/2014 @ 20:05
+ ///
+ ///
+ /// Item 1: The cyphertext that is generated from the plaintext input
+ /// Item 2: The IV used for the encryption algorithm
+ ///
+ public virtual Tuple EncryptStringToBytes(string plainText)
+ {
+ Tuple output;
+
+ // Create an RijndaelManaged object with the specified key and IV.
+ using (RijndaelManaged cryptoContainer = new RijndaelManaged())
+ {
+ ConfigureCryptoContainer(cryptoContainer);
+
+ // Create an encryptor to perform the stream transform.
+ ICryptoTransform encryptor = cryptoContainer.CreateEncryptor(cryptoContainer.Key, cryptoContainer.IV);
+
+ // Create the streams used for encryption.
+ using (MemoryStream msEncrypt = new MemoryStream())
+ {
+ using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
+ {
+ using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
+ {
+ //Write all data to the stream.
+ swEncrypt.Write(plainText);
+ }
+
+ output = new Tuple(msEncrypt.ToArray(), cryptoContainer.IV);
+ }
+ }
+ }
+
+ // Return the encrypted bytes from the memory stream.
+ return output;
+ }
+ }
+}
diff --git a/src/SimpleAesEncryption.Core/RijndaelMessageHandler.cs b/src/SimpleAesEncryption.Core/RijndaelMessageHandler.cs
new file mode 100644
index 0000000..f1c43f7
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/RijndaelMessageHandler.cs
@@ -0,0 +1,109 @@
+using Microsoft.Extensions.Configuration;
+using System;
+using System.IO;
+using System.Linq;
+using System.Security.Cryptography;
+
+namespace ArtisanCode.SimpleAesEncryption
+{
+
+ public abstract class RijndaelMessageHandler
+ {
+ public const string CYPHER_TEXT_IV_SEPARATOR = "??";
+
+ protected string _configurationSectionPath = "MessageEncryption";
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageHandler()
+ {
+ var builder = new ConfigurationBuilder()
+ .SetBasePath(Directory.GetCurrentDirectory())
+ .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
+
+ var configuration = builder.Build();
+ var configSection = configuration.GetSection(_configurationSectionPath);
+ Configuration = configSection.Get();
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageHandler(string configurationSectionPath)
+ {
+ var builder = new ConfigurationBuilder()
+ .SetBasePath(Directory.GetCurrentDirectory())
+ .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
+
+ var configuration = builder.Build();
+ var configSection = configuration.GetSection(configurationSectionPath);
+ Configuration = configSection.Get();
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageHandler(IConfiguration configuration)
+ {
+ var _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
+ Configuration = _configuration.Get();
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RijndaelMessageHandler(SimpleAesEncryptionConfiguration config)
+ {
+ Configuration = config ?? throw new ArgumentNullException(nameof(config));
+ }
+
+ ///
+ /// Gets or sets the configuration.
+ ///
+ public SimpleAesEncryptionConfiguration Configuration { get; }
+
+ ///
+ /// Configures the crypto container.
+ ///
+ public virtual void ConfigureCryptoContainer(RijndaelManaged cryptoContainer)
+ {
+ if (Configuration == null)
+ {
+ throw new ArgumentNullException(nameof(Configuration), "The whole encryption configuration is null.");
+ }
+
+ if (Configuration.EncryptionKey == null)
+ {
+ throw new ArgumentException(nameof(Configuration), "The encryption key configuration is null.");
+ }
+
+ if (string.IsNullOrWhiteSpace(Configuration.EncryptionKey.Key))
+ {
+ throw new CryptographicException("Encryption key is missing.");
+ }
+
+ if (!cryptoContainer.LegalKeySizes.Any(x => (x.MinSize <= Configuration.EncryptionKey.KeySize) && (Configuration.EncryptionKey.KeySize <= x.MaxSize)))
+ {
+ throw new CryptographicException("Invalid Key Size specified. The recommended value is: 256");
+ }
+
+ byte[] key = Convert.FromBase64String(Configuration.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 != (Configuration.EncryptionKey.KeySize / 8))
+ {
+ throw new CryptographicException("Encryption key is the wrong length. Please ensure that it is *EXACTLY* " + Configuration.EncryptionKey.KeySize + " bits long");
+ }
+
+ cryptoContainer.Mode = Configuration.CipherMode;
+ cryptoContainer.Padding = Configuration.Padding;
+ cryptoContainer.KeySize = Configuration.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();
+ }
+ }
+}
diff --git a/src/SimpleAesEncryption.Core/SimpleAesEncryption.Core.csproj b/src/SimpleAesEncryption.Core/SimpleAesEncryption.Core.csproj
new file mode 100644
index 0000000..f3cb7f9
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/SimpleAesEncryption.Core.csproj
@@ -0,0 +1,12 @@
+
+
+
+ netstandard2.0
+
+
+
+
+
+
+
+
diff --git a/src/SimpleAesEncryption.Core/SimpleAesEncryption.nuspec b/src/SimpleAesEncryption.Core/SimpleAesEncryption.nuspec
new file mode 100644
index 0000000..47b2fd3
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/SimpleAesEncryption.nuspec
@@ -0,0 +1,25 @@
+
+
+ Simple.AES.Core
+ $version$
+ Simple AES encryption
+ Tom Kuhn
+ Artisan Code
+ https://avatars3.githubusercontent.com/u/7647107
+ https://github.com/ArtisanCode/SimpleAesEncryption/blob/master/LICENSE
+ https://github.com/ArtisanCode/SimpleAesEncryption
+ false
+ An AES encryption library.
+ A library to allow you to use AES encryption with minimum effort.
+ Initial version
+ .NET AES Encryption
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/SimpleAesEncryption.Core/SimpleAesEncryptionConfiguration.cs b/src/SimpleAesEncryption.Core/SimpleAesEncryptionConfiguration.cs
new file mode 100644
index 0000000..24a6b22
--- /dev/null
+++ b/src/SimpleAesEncryption.Core/SimpleAesEncryptionConfiguration.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Security.Cryptography;
+using System.Text;
+using Microsoft.Extensions.Configuration;
+
+namespace ArtisanCode.SimpleAesEncryption
+{
+ public class SimpleAesEncryptionConfiguration
+ {
+ public EncryptionKeyConfigurationElement EncryptionKey { get; set; }
+ public CipherMode CipherMode { get; set; } = CipherMode.CBC;
+ public PaddingMode Padding { get; set; } = PaddingMode.ISO10126;
+ }
+}