Skip to content

Commit

Permalink
Added AesEncryptionType to AesEncryptor constructor instead of int as…
Browse files Browse the repository at this point in the history
… it can only contains certain values
  • Loading branch information
Arcidev committed Feb 7, 2017
1 parent 0407b66 commit 28ab4d8
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 31 deletions.
1 change: 1 addition & 0 deletions Arci.Networking.NetCore/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"../Arci.Networking/Data/ByteBufferGenericTemplate.cs",
"../Arci.Networking/Data/PacketGuid.cs",
"../Arci.Networking/Data/Packet.cs",
"../Arci.Networking/Security/AesOptions/AesEncryptionType.cs",
"../Arci.Networking/Security/AesEncryptor.cs",
"../Arci.Networking/Security/RsaEncryptor.cs",
"../Arci.Networking/Client.cs",
Expand Down
33 changes: 20 additions & 13 deletions Arci.Networking.Tests/EncryptionTests/AesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Arci.Networking.Security;
using Arci.Networking.Security.AesOptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Security.Cryptography;
Expand All @@ -10,15 +11,21 @@ namespace Arci.Networking.Tests.EncryptionTests
public class AesTests
{
[TestMethod]
public void Test16ByteAesEncryptor()
public void Test128AesEncryptor()
{
TestAesEncryptorCreation(16);
TestAesEncryptorCreation(AesEncryptionType.Aes128Bits);
}

[TestMethod]
public void Test32ByteAesEncryptor()
public void Test192AesEncryptor()
{
TestAesEncryptorCreation(32);
TestAesEncryptorCreation(AesEncryptionType.Aes192Bits);
}

[TestMethod]
public void Test256AesEncryptor()
{
TestAesEncryptorCreation(AesEncryptionType.Aes256Bits);
}

[TestMethod]
Expand Down Expand Up @@ -51,8 +58,8 @@ public void TestNonePadding()
using (var aes = new AesEncryptor() { PaddingMode = PaddingMode.None })
{
var sb = new StringBuilder("Hello from unecrypted world");
if(sb.Length % aes.CurrentKeyByteLength != 0)
sb.Append('\0', aes.CurrentKeyByteLength - sb.Length % aes.CurrentKeyByteLength);
if(sb.Length % aes.Key.Length != 0)
sb.Append('\0', aes.Key.Length - sb.Length % aes.Key.Length);

var value = sb.ToString();
var encryptedVal = aes.Encrypt(value);
Expand All @@ -70,7 +77,7 @@ public void TestNonePadding()

private void TestEncryption(PaddingMode padding)
{
using (var aes = new AesEncryptor() { PaddingMode = padding })
using (var aes = new AesEncryptor(AesEncryptionType.Aes256Bits) { PaddingMode = padding })
{
var value = "Hello from unecrypted world";
var encryptedVal = aes.Encrypt(value);
Expand All @@ -82,21 +89,21 @@ private void TestEncryption(PaddingMode padding)
}
}

private void TestAesEncryptorCreation(int keyLength)
private void TestAesEncryptorCreation(AesEncryptionType type)
{
var aesKey = new byte[keyLength];
var aesKey = new byte[(int)type];
var iVec = new byte[16];
for (byte i = 0; i < keyLength; i++)
for (byte i = 0; i < (int)type; i++)
{
aesKey[i] = i;
if (keyLength < 16)
if (i < 16)
iVec[i] = (byte)(byte.MaxValue - i);
}

using (AesEncryptor aes = new AesEncryptor(keyLength), aes2 = new AesEncryptor(aesKey, iVec))
using (AesEncryptor aes = new AesEncryptor(type), aes2 = new AesEncryptor(aesKey, iVec))
{
Assert.AreNotEqual(null, aes.Encryptors, "Encryptors not created");
Assert.AreEqual(aes.Encryptors.Length, keyLength + iVec.Length, "Invalid length of encryptors");
Assert.AreEqual(aes.Encryptors.Length, (int)type + iVec.Length, "Invalid length of encryptors");

Assert.AreNotEqual(null, aes2.Encryptors, "Encryptors not created");
Assert.IsTrue(aesKey.Concat(iVec).SequenceEqual(aes2.Encryptors), "Encryptors not created correctly");
Expand Down
3 changes: 3 additions & 0 deletions Arci.Networking.UAP/Arci.Networking.UAP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
<Compile Include="..\Arci.Networking\Security\AesEncryptor.cs">
<Link>Security\AesEncryptor.cs</Link>
</Compile>
<Compile Include="..\Arci.Networking\Security\AesOptions\AesEncryptionType.cs">
<Link>Security\AesOptions\AesEncryptionType.cs</Link>
</Compile>
<Compile Include="..\Arci.Networking\Security\RsaEncryptor.cs">
<Link>Security\RsaEncryptor.cs</Link>
</Compile>
Expand Down
1 change: 1 addition & 0 deletions Arci.Networking/Arci.Networking.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="Data\Packet.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security\AesEncryptor.cs" />
<Compile Include="Security\AesOptions\AesEncryptionType.cs" />
<Compile Include="Security\RsaEncryptor.cs" />
<Compile Include="Server.cs" />
</ItemGroup>
Expand Down
27 changes: 13 additions & 14 deletions Arci.Networking/Security/AesEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security.Cryptography;
using System.IO;
using System;
using Arci.Networking.Security.AesOptions;

namespace Arci.Networking.Security
{
Expand All @@ -11,18 +12,21 @@ namespace Arci.Networking.Security
public class AesEncryptor : IDisposable
{
private Aes aes;
private byte[] iVec;
private byte[] key;

/// <summary>
/// Current length of the key in bytes
/// Returns copy of the current aes key
/// </summary>
public int CurrentKeyByteLength { get; private set; }
public byte[] Key { get { return aes.Key.ToArray(); } }

/// <summary>
/// Returns copy of the current aes iVec
/// </summary>
public byte[] IVec { get { return aes.IV.ToArray(); } }

/// <summary>
/// Current encryptors. First bytes represent key, last 16 bytes represent iVec
/// </summary>
public byte[] Encryptors { get { return key.Concat(iVec).ToArray(); } }
public byte[] Encryptors { get { return aes.Key.Concat(aes.IV).ToArray(); } }

/// <summary>
/// Aes padding mode to be used
Expand All @@ -36,13 +40,12 @@ public PaddingMode PaddingMode
/// <summary>
/// Creates AES instance
/// </summary>
/// <param name="keyByteLength">Valid length (16, 24, 32) of AES key in bytes</param>
public AesEncryptor(int keyByteLength = 16)
/// <param name="type">Bit version type of Aes to be used</param>
public AesEncryptor(AesEncryptionType type = AesEncryptionType.Aes128Bits)
{
aes = Aes.Create();
iVec = new byte[16];
key = new byte[keyByteLength];
CurrentKeyByteLength = keyByteLength;
var iVec = new byte[16];
var key = new byte[(int)type];

using (var rng = RandomNumberGenerator.Create())
{
Expand All @@ -62,10 +65,6 @@ public AesEncryptor(int keyByteLength = 16)
/// <param name="iVec">IVec to be set as AES iVec</param>
public AesEncryptor(byte[] key, byte[] iVec)
{
this.key = key;
this.iVec = iVec;
CurrentKeyByteLength = key?.Length ?? 0;

aes = Aes.Create();
aes.IV = iVec;
aes.Key = key;
Expand Down
24 changes: 24 additions & 0 deletions Arci.Networking/Security/AesOptions/AesEncryptionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

namespace Arci.Networking.Security.AesOptions
{
/// <summary>
/// Aes encryption type
/// </summary>
public enum AesEncryptionType
{
/// <summary>
/// 128 bits (16 bytes) version of AES
/// </summary>
Aes128Bits = 16,

/// <summary>
/// 192 bits (24 bytes) version of AES
/// </summary>
Aes192Bits = 24,

/// <summary>
/// 256 bits (32 bytes) version of AES
/// </summary>
Aes256Bits = 32
}
}
6 changes: 4 additions & 2 deletions ClientSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Arci.Networking;
using Arci.Networking.Data;
using Arci.Networking.Security;
using Arci.Networking.Security.AesOptions;
using Shared;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -27,9 +29,9 @@ private static async Task RunClient()

// Sending AES key via RSA encryption
// Aes (key, ivec generation)
AesEncryptor aes = new AesEncryptor();
AesEncryptor aes = new AesEncryptor(AesEncryptionType.Aes256Bits) { PaddingMode = PaddingMode.PKCS7 };
// Rsa (sets server public key)
RsaEncryptor rsa = new RsaEncryptor(RSAKey.Modulus, RSAKey.PublicExponent);
RsaEncryptor rsa = new RsaEncryptor(RSAKey.Modulus, RSAKey.PublicExponent) { UseOAEPPadding = true };
// Sets AES key for our client, key will be used for SendPacket function and ReceiveData function
client.AesEncryptor = aes;

Expand Down
5 changes: 3 additions & 2 deletions ServerSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Shared;
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace ServerSample
Expand All @@ -20,7 +21,7 @@ private static async Task RunServer()
// Client will send us the key and iVec
AesEncryptor aes = null;
// Rsa inicialization
RsaEncryptor rsa = new RsaEncryptor(RSAKey.RsaParams);
RsaEncryptor rsa = new RsaEncryptor(RSAKey.RsaParams) { UseOAEPPadding = true };

// Creates new server instance on port 10751
Server server = new Server(10751);
Expand Down Expand Up @@ -49,7 +50,7 @@ private static async Task RunServer()
{
case ClientPacketTypes.CMSG_INIT_ENCRYPTED_RSA:
var keys = rsa.Decrypt(packet.ReadBytes());
aes = new AesEncryptor(keys.Take(16).ToArray(), keys.Skip(16).ToArray());
aes = new AesEncryptor(keys.Take(32).ToArray(), keys.Skip(32).ToArray()) { PaddingMode = PaddingMode.PKCS7 };
client.AesEncryptor = aes;
response = new Packet(ServerPacketTypes.SMSG_INIT_RESPONSE_ENCRYPTED_RSA);
response.Write("Hello Client!");
Expand Down

0 comments on commit 28ab4d8

Please sign in to comment.