Skip to content

Commit 2af4eb1

Browse files
authored
Merge pull request #41 from selfmadecode/dev
update master with dev
2 parents 71a61f5 + fbd98fe commit 2af4eb1

17 files changed

Lines changed: 160 additions & 71 deletions

SafeCrypt.sln

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.8.34322.80
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "SafeCrypt.csproj", "{204CA507-752E-43A6-A094-794E40ABAE1F}"
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0B7C0C60-9850-4554-AF85-86C0378B6B16}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "safecrypt-testapp", "..\safecrypt-testapp\safecrypt-testapp.csproj", "{76D17C56-5643-4148-A504-8D6E24D24CAD}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SafeCrypt.Lib", "SafeCrypt.Lib", "{8507D130-9F07-426C-8EE6-0AC714CF72E5}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SafeCrypt.App", "SafeCrypt.App", "{1D91E775-F63F-4537-B81E-B8F9A6480D6D}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "src\SafeCrypt.Lib\SafeCrypt.csproj", "{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt.App", "src\SafeCrypt.Test\SafeCrypt.App.csproj", "{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}"
915
EndProject
1016
Global
1117
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1218
Debug|Any CPU = Debug|Any CPU
1319
Release|Any CPU = Release|Any CPU
1420
EndGlobalSection
1521
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16-
{204CA507-752E-43A6-A094-794E40ABAE1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17-
{204CA507-752E-43A6-A094-794E40ABAE1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
18-
{204CA507-752E-43A6-A094-794E40ABAE1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
19-
{204CA507-752E-43A6-A094-794E40ABAE1F}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
2733
EndGlobalSection
34+
GlobalSection(NestedProjects) = preSolution
35+
{8507D130-9F07-426C-8EE6-0AC714CF72E5} = {0B7C0C60-9850-4554-AF85-86C0378B6B16}
36+
{1D91E775-F63F-4537-B81E-B8F9A6480D6D} = {0B7C0C60-9850-4554-AF85-86C0378B6B16}
37+
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8} = {8507D130-9F07-426C-8EE6-0AC714CF72E5}
38+
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6} = {1D91E775-F63F-4537-B81E-B8F9A6480D6D}
39+
EndGlobalSection
2840
GlobalSection(ExtensibilityGlobals) = postSolution
2941
SolutionGuid = {639A4359-2BA4-4F71-9EBF-D6EAB68C84CB}
3042
EndGlobalSection

src/Enums/ReturnType.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Encryption/AesEncryption/BaseAesEncryption.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using SafeCrypt.Models;
22
using System;
33
using System.IO;
4-
using System.Reflection;
54
using System.Security.Cryptography;
6-
using System.Text;
75

86
namespace SafeCrypt.AesEncryption
97
{
@@ -25,7 +23,7 @@ public class BaseAesEncryption
2523
/// <exception cref="Exception">
2624
/// Thrown for general encryption-related exceptions.
2725
/// </exception>
28-
internal static byte[] EncryptAES(ByteEncryptionParameters param)
26+
internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC)
2927
{
3028
try
3129
{
@@ -35,6 +33,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
3533
// Set the key and initialization vector
3634
aes.Key = param.SecretKey;
3735
aes.IV = param.IV;
36+
aes.Mode = mode;
3837
// Create an encryptor using the key and initialization vector
3938
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
4039

@@ -73,7 +72,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
7372
/// <exception cref="ArgumentNullException">
7473
/// Thrown if the input encrypted data, key, or initialization vector is null.
7574
/// </exception>
76-
internal static byte[] DecryptAES(ByteDecryptionParameters param)
75+
internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC)
7776
{
7877
try
7978
{
@@ -83,6 +82,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param)
8382
// Set the key and initialization vector
8483
aes.Key = param.SecretKey;
8584
aes.IV = param.IV;
85+
aes.Mode= mode;
8686

8787
// Create a decryptor using the key and initialization vector
8888
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
@@ -106,7 +106,6 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param)
106106
}
107107
catch (Exception ex)
108108
{
109-
110109
throw;
111110
}
112111
}

src/Encryption/AesEncryption/Decrypting.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using SafeCrypt.Helpers;
33
using SafeCrypt.Models;
44
using System;
5+
using System.Security.Cryptography;
56

67
namespace SafeCrypt.AESDecryption
78
{
89
public class AesDecryption : BaseAesEncryption
910
{
10-
public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
11+
public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
1112
{
1213
var responseData = new DecryptionData();
1314

@@ -43,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
4344
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
4445
};
4546

46-
var response = DecryptAES(byteEncryptionParameters);
47+
var response = DecryptAES(byteEncryptionParameters, mode);
4748

4849
return new DecryptionData
4950
{
@@ -53,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
5354
};
5455
}
5556

56-
public DecryptionData DecryptFromBase64String(DecryptionParameters param)
57+
public DecryptionData DecryptFromBase64String(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
5758
{
5859
var responseData = new DecryptionData();
5960

@@ -81,7 +82,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param)
8182
Data = Convert.FromBase64String(param.DataToDecrypt)
8283
};
8384

84-
var response = DecryptAES(byteDecryptionParameters);
85+
var response = DecryptAES(byteDecryptionParameters, mode);
8586

8687
return new DecryptionData
8788
{

src/Encryption/AesEncryption/Encrypting.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Security.Cryptography;
23
using SafeCrypt.AesEncryption;
34
using SafeCrypt.Helpers;
45
using SafeCrypt.Models;
@@ -21,24 +22,17 @@ public class AesEncryption : BaseAesEncryption
2122
/// <param name="secretKey">The secret key used for encryption.</param>
2223
/// <param name="iv">The initialization vector used for encryption.</param>
2324
/// <returns>The encrypted data as a byte array.</returns>
24-
public EncryptionData EncryptToHexString(EncryptionParameters param)
25+
public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC)
2526
{
2627
var responseData = new EncryptionData();
2728

28-
Validators.ValidateNotNull(param);
29+
var parameterValidation = ValidateEncryptionParameters(param);
2930

30-
// validate is base64
31-
if (!Validators.IsBase64String(param.SecretKey))
31+
if (parameterValidation.HasError)
3232
{
33-
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
34-
return responseData;
33+
return parameterValidation;
3534
}
3635

37-
if (!Validators.IsBase64String(param.IV))
38-
{
39-
AddError(responseData, $"IV: {param.IV} is not a base64 string");
40-
return responseData;
41-
}
4236
// Convert input string to bytes
4337
byte[] dataBytes = param.IV.ConvertKeysToBytes();
4438

@@ -57,7 +51,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
5751
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
5852
};
5953

60-
var response = EncryptAES(byteEncryptionParameters);
54+
var response = EncryptAES(byteEncryptionParameters, mode);
6155

6256
return new EncryptionData
6357
{
@@ -66,8 +60,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
6660
SecretKey = param.SecretKey
6761
};
6862
}
69-
70-
63+
7164
/// <summary>
7265
/// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
7366
/// </summary>
@@ -90,7 +83,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
9083
/// <exception cref="FormatException">
9184
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
9285
/// </exception>
93-
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
86+
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC)
9487
{
9588
// validate is base64
9689
if (!Validators.IsBase64String(base64secretKey))
@@ -110,7 +103,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
110103
Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray()
111104
};
112105

113-
var response = EncryptAES(byteEncryptionParameters);
106+
var response = EncryptAES(byteEncryptionParameters, mode);
114107

115108
return new EncryptionData
116109
{
@@ -120,6 +113,26 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
120113
};
121114
}
122115

116+
private EncryptionData ValidateEncryptionParameters(EncryptionParameters param)
117+
{
118+
var responseData = new EncryptionData();
119+
120+
Validators.ValidateNotNull(param);
121+
122+
// validate is base64
123+
if (!Validators.IsBase64String(param.SecretKey))
124+
{
125+
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
126+
}
127+
128+
if (!Validators.IsBase64String(param.IV))
129+
{
130+
AddError(responseData, $"IV: {param.IV} is not a base64 string");
131+
}
132+
133+
return responseData;
134+
}
135+
123136
private void NullChecks(string data, string secretKey)
124137
{
125138
if (data == null || data.Length <= 0)

src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
4-
using System.Text;
1+
using System.ComponentModel.DataAnnotations;
52

63
namespace SafeCrypt.Models
74
{

src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace SafeCrypt.Models
64
{

src/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs

File renamed without changes.

src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace SafeCrypt.Models
64
{
@@ -25,6 +23,7 @@ public class EncryptionData
2523
public string SecretKey { get; set; }
2624

2725
public bool HasError { get; set; }
26+
2827
public List<string> Errors { get; set; } = new List<string>();
2928
}
3029
}

src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs renamed to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations;
4-
using System.Text;
1+
using System.ComponentModel.DataAnnotations;
2+
53

64
namespace SafeCrypt.Models
75
{

0 commit comments

Comments
 (0)