Skip to content

Commit c0c3240

Browse files
authored
Feature/41 major imporvement (#44)
* Fix encrypt and decrypt files with general improvement (reported by and thanks to @TK-SMF) #41 Tom Kersting * unit test refactoring, adding cli examples refactoring, adding file test * Fix cross-system compatibility * Update documentation and add code coverage --------- Co-authored-by: Maytham Fahmi <[email protected]>
1 parent 0b7ec83 commit c0c3240

24 files changed

+660
-518
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ jobs:
3333
run: dotnet test --configuration Release --no-build
3434
- name: Upload coverage reports to Codecov
3535
uses: codecov/codecov-action@v3
36+
env:
37+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

CryptoNet.Cli/CryptoNet.Cli.csproj

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net7.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
@@ -11,4 +11,19 @@
1111
<ProjectReference Include="..\CryptoNet\CryptoNet.csproj" />
1212
</ItemGroup>
1313

14+
<ItemGroup>
15+
<None Update="TestFiles\test.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="TestFiles\test.pdf">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="TestFiles\test.png">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
<None Update="TestFiles\test.xlsx">
25+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26+
</None>
27+
</ItemGroup>
28+
1429
</Project>

CryptoNet.Cli/ExampleAes.cs

Lines changed: 93 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,111 +9,127 @@
99
using System.Security.Cryptography;
1010
using System.Text;
1111
using CryptoNet.Models;
12+
using CryptoNet.Utils;
1213

13-
namespace CryptoNet.Cli;
14-
15-
public class ExampleAes
14+
namespace CryptoNet.Cli
1615
{
17-
private const string ConfidentialDummyData = @"Some Secret Data";
18-
19-
private static readonly string BaseFolder = AppDomain.CurrentDomain.BaseDirectory;
20-
private static readonly string SymmetricKeyFile = Path.Combine(BaseFolder, $"{KeyType.SymmetricKey}.xml");
2116

22-
public static void Test()
17+
public class ExampleAes
2318
{
24-
Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey();
25-
Example_2_SelfGenerated_And_Save_SymmetricKey();
26-
Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey();
27-
Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey();
28-
}
19+
protected ExampleAes() { }
2920

30-
public static void Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey()
31-
{
32-
ICryptoNet cryptoNet = new CryptoNetAes();
33-
var key = cryptoNet.ExportKey();
21+
private const string ConfidentialDummyData = @"Some Secret Data";
3422

35-
ICryptoNet encryptClient = new CryptoNetAes(key);
36-
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
23+
private static readonly string BaseFolder = AppDomain.CurrentDomain.BaseDirectory;
24+
private readonly static string SymmetricKeyFile = Path.Combine(BaseFolder, $"{KeyType.SymmetricKey}.xml");
3725

38-
ICryptoNet decryptClient = new CryptoNetAes(key);
39-
var decrypt = decryptClient.DecryptToString(encrypt);
26+
public static void Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey()
27+
{
28+
ICryptoNet cryptoNet = new CryptoNetAes();
29+
var key = cryptoNet.ExportKey();
4030

41-
Debug.Assert(ConfidentialDummyData == decrypt);
42-
}
31+
ICryptoNet encryptClient = new CryptoNetAes(key);
32+
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
4333

44-
public static void Example_2_SelfGenerated_And_Save_SymmetricKey()
45-
{
46-
ICryptoNet cryptoNet = new CryptoNetAes();
47-
var file = new FileInfo(SymmetricKeyFile);
48-
cryptoNet.ExportKeyAndSave(file);
34+
ICryptoNet decryptClient = new CryptoNetAes(key);
35+
var decrypt = decryptClient.DecryptToString(encrypt);
4936

50-
Debug.Assert(File.Exists(file.FullName));
37+
Debug.Assert(ConfidentialDummyData == decrypt);
38+
}
5139

52-
var encrypt = cryptoNet.EncryptFromString(ConfidentialDummyData);
53-
54-
ICryptoNet cryptoNetKeyImport = new CryptoNetAes(file);
55-
var decrypt = cryptoNetKeyImport.DecryptToString(encrypt);
40+
public static void Example_2_SelfGenerated_And_Save_SymmetricKey()
41+
{
42+
ICryptoNet cryptoNet = new CryptoNetAes();
43+
var file = new FileInfo(SymmetricKeyFile);
44+
cryptoNet.ExportKeyAndSave(file);
5645

57-
Debug.Assert(ConfidentialDummyData == decrypt);
58-
}
46+
Debug.Assert(File.Exists(file.FullName));
5947

60-
public static void Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey()
61-
{
62-
var symmetricKey = "12345678901234567890123456789012";
63-
if (symmetricKey.Length != 32)
64-
{
65-
Console.WriteLine("key should be 32 character long");
66-
Environment.Exit(0);
48+
var encrypt = cryptoNet.EncryptFromString(ConfidentialDummyData);
49+
50+
ICryptoNet cryptoNetKeyImport = new CryptoNetAes(file);
51+
var decrypt = cryptoNetKeyImport.DecryptToString(encrypt);
52+
53+
Debug.Assert(ConfidentialDummyData == decrypt);
6754
}
6855

69-
var secret = "1234567890123456";
70-
if (secret.Length != 16)
56+
public static void Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey()
7157
{
72-
Console.WriteLine("key should be 16 character long");
73-
Environment.Exit(1);
58+
var symmetricKey = "12345678901234567890123456789012";
59+
if (symmetricKey.Length != 32)
60+
{
61+
Console.WriteLine("key should be 32 character long");
62+
Environment.Exit(0);
63+
}
64+
65+
var secret = "1234567890123456";
66+
if (secret.Length != 16)
67+
{
68+
Console.WriteLine("key should be 16 character long");
69+
Environment.Exit(1);
70+
}
71+
72+
var key = Encoding.UTF8.GetBytes(symmetricKey);
73+
var iv = Encoding.UTF8.GetBytes(secret);
74+
75+
ICryptoNet encryptClient = new CryptoNetAes(key, iv);
76+
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
77+
78+
ICryptoNet decryptClient = new CryptoNetAes(key, iv);
79+
var decrypt = decryptClient.DecryptToString(encrypt);
80+
81+
Debug.Assert(ConfidentialDummyData == decrypt);
7482
}
7583

76-
var key = Encoding.UTF8.GetBytes(symmetricKey);
77-
var iv = Encoding.UTF8.GetBytes(secret);
84+
public static void Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey()
85+
{
86+
var symmetricKey = UniqueKeyGenerator("symmetricKey");
87+
var secret = new string(UniqueKeyGenerator("password").Take(16).ToArray());
7888

79-
ICryptoNet encryptClient = new CryptoNetAes(key, iv);
80-
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
89+
var key = Encoding.UTF8.GetBytes(symmetricKey);
90+
var iv = Encoding.UTF8.GetBytes(secret);
8191

82-
ICryptoNet decryptClient = new CryptoNetAes(key, iv);
83-
var decrypt = decryptClient.DecryptToString(encrypt);
92+
ICryptoNet encryptClient = new CryptoNetAes(key, iv);
93+
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
8494

85-
Debug.Assert(ConfidentialDummyData == decrypt);
86-
}
95+
ICryptoNet decryptClient = new CryptoNetAes(key, iv);
96+
var decrypt = decryptClient.DecryptToString(encrypt);
8797

88-
public static void Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey()
89-
{
90-
var symmetricKey = UniqueKeyGenerator("symmetricKey");
91-
var secret = new string(UniqueKeyGenerator("password").Take(16).ToArray());
98+
Debug.Assert(ConfidentialDummyData == decrypt);
99+
}
92100

93-
var key = Encoding.UTF8.GetBytes(symmetricKey);
94-
var iv = Encoding.UTF8.GetBytes(secret);
101+
public static void Example_5_Encrypt_And_Decrypt_PdfFile_With_SymmetricKey_Test(string filename)
102+
{
103+
ICryptoNet cryptoNet = new CryptoNetAes();
104+
var key = cryptoNet.ExportKey();
95105

96-
ICryptoNet encryptClient = new CryptoNetAes(key, iv);
97-
var encrypt = encryptClient.EncryptFromString(ConfidentialDummyData);
106+
FileInfo fi = new FileInfo(filename);
98107

99-
ICryptoNet decryptClient = new CryptoNetAes(key, iv);
100-
var decrypt = decryptClient.DecryptToString(encrypt);
108+
ICryptoNet encryptClient = new CryptoNetAes(key);
109+
string pdfFilePath = Path.Combine(BaseFolder, filename);
110+
byte[] pdfFileBytes = File.ReadAllBytes(pdfFilePath);
111+
var encrypt = encryptClient.EncryptFromBytes(pdfFileBytes);
101112

102-
Debug.Assert(ConfidentialDummyData == decrypt);
103-
}
113+
ICryptoNet decryptClient = new CryptoNetAes(key);
114+
var decrypt = decryptClient.DecryptToBytes(encrypt);
115+
string pdfDecryptedFilePath = $"TestFiles\\{Path.GetFileNameWithoutExtension(fi.Name)}-decrypted.{fi.Extension}";
116+
File.WriteAllBytes(pdfDecryptedFilePath, decrypt);
104117

105-
public static string UniqueKeyGenerator(string input)
106-
{
107-
MD5 md5 = MD5.Create();
108-
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
109-
byte[] hash = md5.ComputeHash(inputBytes);
118+
var isIdenticalFile = CryptoNetUtils.ByteArrayCompare(pdfFileBytes, decrypt);
119+
Debug.Assert(isIdenticalFile);
120+
}
110121

111-
StringBuilder sb = new StringBuilder();
112-
foreach (var t in hash)
122+
public static string UniqueKeyGenerator(string input)
113123
{
114-
sb.Append(t.ToString("X2"));
124+
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
125+
byte[] hash = MD5.HashData(inputBytes);
126+
127+
StringBuilder sb = new StringBuilder();
128+
foreach (var t in hash)
129+
{
130+
sb.Append(t.ToString("X2"));
131+
}
132+
return sb.ToString();
115133
}
116-
return sb.ToString();
117134
}
118-
119-
}
135+
}

0 commit comments

Comments
 (0)