Skip to content

Commit

Permalink
45 refinement and refactoring (#46)
Browse files Browse the repository at this point in the history
* #45 Refactoring AES, Enhancement of CryptoNet crypto class's
* #45 Refinement and clean up
* #45 fix upgrade net analyzers NuGet warning issue
---------
Co-authored-by: Maytham Fahmi <maythamfahmi@nextbix.dk>
  • Loading branch information
maythamfahmi committed Sep 10, 2023
1 parent b1720fa commit 65e48de
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 95 deletions.
46 changes: 23 additions & 23 deletions CryptoNet.Cli/CryptoNet.Cli.csproj
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<ProjectReference Include="..\CryptoNet\CryptoNet.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CryptoNet\CryptoNet.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestFiles\test.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\test.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\test.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\test.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="TestFiles\test.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\test.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\test.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestFiles\test.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
13 changes: 6 additions & 7 deletions CryptoNet.UnitTests/CryptoNet.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -12,8 +11,8 @@
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>
Expand All @@ -33,16 +32,16 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\test.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\TestFiles\WordDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
7 changes: 3 additions & 4 deletions CryptoNet.UnitTests/CryptoNetRsaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using System;
using System.IO;
using System.Text;
using CryptoNet.Models;
using CryptoNet.Utils;
using NUnit.Framework;
Expand Down Expand Up @@ -173,10 +172,10 @@ public void Validate_Decrypted_File_Against_The_Original_File_By_Comparing_Bytes
// act
var filePath = Path.Combine(Common.TestFilesFolder, filename);
byte[] originalFileBytes = File.ReadAllBytes(filePath);
byte[] encrypted = cryptoNet.EncryptFromBytes(originalFileBytes);
byte[] decrypted = cryptoNet.DecryptToBytes(encrypted);
byte[] encryptedBytes = cryptoNet.EncryptFromBytes(originalFileBytes);
byte[] decryptedBytes = cryptoNet.DecryptToBytes(encryptedBytes);

var isIdenticalFile = CryptoNetUtils.ByteArrayCompare(originalFileBytes, decrypted);
var isIdenticalFile = CryptoNetUtils.ByteArrayCompare(originalFileBytes, decryptedBytes);

// assert
isIdenticalFile.ShouldBeTrue();
Expand Down
5 changes: 3 additions & 2 deletions CryptoNet/CryptoNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
</PropertyGroup>

<Target Name="PrepareReleaseNotes" BeforeTargets="GenerateNuspec">
Expand All @@ -40,7 +41,7 @@

<ItemGroup>
<Content Include="CryptoNetLogo-icon.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3" PrivateAssets="All" />
Expand All @@ -50,7 +51,7 @@
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\img\CryptoNetLogo-icon.png" Pack="true" PackagePath="\">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
93 changes: 64 additions & 29 deletions CryptoNet/CryptoNetAes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public CryptoNetAes()
Aes.KeySize = 256;
Aes.GenerateKey();
Aes.GenerateIV();
Info = CreateDetails(Aes.Key, Aes.IV);
Info = CreateInfo(Aes.Key, Aes.IV);
Aes.Key = Info.AesDetail!.AesKeyValue.Key;
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}
Expand All @@ -34,7 +34,7 @@ public CryptoNetAes(string key)
Aes = Aes.Create();
Aes.KeySize = 256;
var keyInfo = CryptoNetUtils.ImportAesKey(key);
Info = CreateDetails(keyInfo.Key, keyInfo.Iv);
Info = CreateInfo(keyInfo.Key, keyInfo.Iv);
Aes.Key = Info.AesDetail!.AesKeyValue.Key;
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}
Expand All @@ -44,7 +44,7 @@ public CryptoNetAes(FileInfo fileInfo)
Aes = Aes.Create();
Aes.KeySize = 256;
var keyInfo = CryptoNetUtils.ImportAesKey(CryptoNetUtils.LoadFileToString(fileInfo.FullName));
Info = CreateDetails(keyInfo.Key, keyInfo.Iv);
Info = CreateInfo(keyInfo.Key, keyInfo.Iv);
Aes.Key = Info.AesDetail!.AesKeyValue.Key;
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}
Expand All @@ -53,12 +53,12 @@ public CryptoNetAes(byte[] key, byte[] iv)
{
Aes = Aes.Create();
Aes.KeySize = 256;
Info = CreateDetails(key, iv);
Info = CreateInfo(key, iv);
Aes.Key = Info.AesDetail!.AesKeyValue.Key;
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}

private CryptoNetInfo CreateDetails(byte[] key, byte[] iv)
private CryptoNetInfo CreateInfo(byte[] key, byte[] iv)
{
return new CryptoNetInfo()
{
Expand All @@ -85,74 +85,109 @@ public void ExportKeyAndSave(FileInfo fileInfo, bool? privateKey = false)
#region encryption logic
public byte[] EncryptFromString(string content)
{
return EncryptContent(content);
return EncryptContent(CryptoNetUtils.StringToBytes(content));

}

public byte[] EncryptFromBytes(byte[] bytes)
{
return EncryptContent(CryptoNetUtils.Base64BytesToString(bytes));
return EncryptContent(bytes);
}

public string DecryptToString(byte[] bytes)
{
return DecryptContent(bytes);
return CryptoNetUtils.BytesToString(DecryptContent(bytes));
}

public byte[] DecryptToBytes(byte[] bytes)
{
return CryptoNetUtils.Base64StringToBytes(DecryptContent(bytes));
return DecryptContent(bytes);
}

private byte[] EncryptContent(string content)
private byte[] EncryptContent(byte[] bytes)
{
if (content == null || content.Length <= 0)
if (bytes == null || bytes.Length <= 0)
{
throw new ArgumentNullException(nameof(content));
throw new ArgumentNullException(nameof(bytes));
}

byte[] encrypted;
byte[] result;

ICryptoTransform encryptor = Aes.CreateEncryptor(Aes.Key, Aes.IV);
var encryptor = Aes.CreateEncryptor(Aes.Key, Aes.IV);

using (MemoryStream msEncrypt = new MemoryStream())
using (var msOut = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var csEncryptedOut = new CryptoStream(msOut, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
var blockSizeBytes = Aes.BlockSize / 8;
var data = new byte[blockSizeBytes];

using (var msIn = new MemoryStream(bytes))
{
swEncrypt.Write(content);
int count;
do
{
count = msIn.Read(data, 0, blockSizeBytes);
csEncryptedOut.Write(data, 0, count);
} while (count > 0);

msIn.Close();
}
encrypted = msEncrypt.ToArray();

csEncryptedOut.FlushFinalBlock();
csEncryptedOut.Close();
}

result = msOut.ToArray();

msOut.Close();
}

return encrypted;
return result;
}

private string DecryptContent(byte[] bytes)
private byte[] DecryptContent(byte[] bytes)
{
if (bytes == null || bytes.Length <= 0)
{
throw new ArgumentNullException(nameof(bytes));
}

string? plaintext;

ICryptoTransform decryptor = Aes.CreateDecryptor(Aes.Key, Aes.IV);
byte[] result;

using (MemoryStream ms = new MemoryStream(bytes))
using (var inMs = new MemoryStream(bytes))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
var decryptor = Aes.CreateDecryptor(Aes.Key, Aes.IV);

using (var outMs = new MemoryStream())
{
using (StreamReader sr = new StreamReader(cs))
var blockSizeBytes = Aes.BlockSize / 8;
var data = new byte[blockSizeBytes];

using (var csDecryptedOut = new CryptoStream(outMs, decryptor, CryptoStreamMode.Write))
{
plaintext = sr.ReadToEnd();
int count;
do
{
count = inMs.Read(data, 0, blockSizeBytes);
csDecryptedOut.Write(data, 0, count);
} while (count > 0);

csDecryptedOut.FlushFinalBlock();
csDecryptedOut.Close();
}

result = outMs.ToArray();

outMs.Close();
}

inMs.Close();
}

return plaintext;
return result;
}

#endregion
}
}

0 comments on commit 65e48de

Please sign in to comment.