Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,4 @@ pip-log.txt

#Mr Developer
.mr.developer.cfg
/.vs
28 changes: 26 additions & 2 deletions SimpleAesEncryption.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30110.0
Expand Down Expand Up @@ -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
Expand All @@ -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
24 changes: 24 additions & 0 deletions sample/SimpleExample.Core/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
20 changes: 20 additions & 0 deletions sample/SimpleExample.Core/SimpleExample.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SimpleAesEncryption.Core\SimpleAesEncryption.Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions sample/SimpleExample.Core/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
//"MessageEncryption": {
// "EncryptionKey": {
// "Key": "GRE5sAmmndnu0t3h1+OzMNfrGHoVn2mdy44qISfVJqs=",
// "KeySize": 256
// }
//}
"CustomMessageEncryption": {
"EncryptionKey": {
"Key": "GRE5sAmmndnu0t3h1+OzMNfrGHoVn2mdy44qISfVJqs=",
"KeySize": 256
}
}
}
14 changes: 14 additions & 0 deletions src/KeyGen.Core/KeyGen.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleAesEncryption.Core\SimpleAesEncryption.Core.csproj" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions src/KeyGen.Core/Program.cs
Original file line number Diff line number Diff line change
@@ -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)");
}
33 changes: 33 additions & 0 deletions src/SimpleAesEncryption.Core/EncryptionKeyConfigurationElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace ArtisanCode.SimpleAesEncryption
{
public class EncryptionKeyConfigurationElement
{
/// <summary>
/// Initializes a new instance of the <see cref="EncryptionKeyConfigurationElement"/> class.
/// </summary>
public EncryptionKeyConfigurationElement()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EncryptionKeyConfigurationElement"/> class.
/// </summary>
/// <param name="keySize">Size of the key.</param>
/// <param name="key">The key.</param>
public EncryptionKeyConfigurationElement(int keySize, string key)
{
this.KeySize = keySize;
this.Key = key;
}

/// <summary>
/// Gets or sets the encryption key.
/// </summary>
public string Key { get; set; }

/// <summary>
/// Gets or sets the size of the key in bits.
/// </summary>
public int KeySize { get; set; } = 256;
}
}
16 changes: 16 additions & 0 deletions src/SimpleAesEncryption.Core/IMessageDecryptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ArtisanCode.SimpleAesEncryption
{
public interface IMessageDecryptor
{
/// <summary>
/// Decrypts the specified cypher text.
/// </summary>
/// <param name="cypherText">The cypherText.</param>
/// <returns>The plaintext decrypted version of the cypher text</returns>
string Decrypt(string cypherText);
}
}
12 changes: 12 additions & 0 deletions src/SimpleAesEncryption.Core/IMessageEncryptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ArtisanCode.SimpleAesEncryption
{
public interface IMessageEncryptor
{
/// <summary>
/// Encrypts the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>The cypher-text generated from the source</returns>
string Encrypt(string source);
}
}
Loading