Skip to content

Commit d7c8f80

Browse files
committed
feat: merge master into branch
2 parents 6b7d2d2 + 2af4eb1 commit d7c8f80

17 files changed

Lines changed: 160 additions & 74 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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +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;
7-
using System.Threading.Tasks;
85

96
namespace SafeCrypt.AesEncryption
107
{
@@ -26,7 +23,7 @@ public class BaseAesEncryption
2623
/// <exception cref="Exception">
2724
/// Thrown for general encryption-related exceptions.
2825
/// </exception>
29-
internal static async Task<byte[]> EncryptAES(ByteEncryptionParameters param)
26+
internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC)
3027
{
3128
try
3229
{
@@ -36,6 +33,7 @@ internal static async Task<byte[]> EncryptAES(ByteEncryptionParameters param)
3633
// Set the key and initialization vector
3734
aes.Key = param.SecretKey;
3835
aes.IV = param.IV;
36+
aes.Mode = mode;
3937
// Create an encryptor using the key and initialization vector
4038
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
4139

@@ -74,7 +72,7 @@ internal static async Task<byte[]> EncryptAES(ByteEncryptionParameters param)
7472
/// <exception cref="ArgumentNullException">
7573
/// Thrown if the input encrypted data, key, or initialization vector is null.
7674
/// </exception>
77-
internal static async Task<byte[]> DecryptAES(ByteDecryptionParameters param)
75+
internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC)
7876
{
7977
try
8078
{
@@ -84,6 +82,7 @@ internal static async Task<byte[]> DecryptAES(ByteDecryptionParameters param)
8482
// Set the key and initialization vector
8583
aes.Key = param.SecretKey;
8684
aes.IV = param.IV;
85+
aes.Mode= mode;
8786

8887
// Create a decryptor using the key and initialization vector
8988
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
@@ -107,7 +106,6 @@ internal static async Task<byte[]> DecryptAES(ByteDecryptionParameters param)
107106
}
108107
catch (Exception ex)
109108
{
110-
111109
throw;
112110
}
113111
}

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

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

77
namespace SafeCrypt.AESDecryption
88
{
99
public class AesDecryption : BaseAesEncryption
1010
{
11-
public async Task<DecryptionData> DeEncryptFromHexString(DecryptionParameters param)
11+
public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
1212
{
1313
var responseData = new DecryptionData();
1414

@@ -44,7 +44,7 @@ public async Task<DecryptionData> DeEncryptFromHexString(DecryptionParameters pa
4444
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
4545
};
4646

47-
var response = await DecryptAES(byteEncryptionParameters);
47+
var response = DecryptAES(byteEncryptionParameters, mode);
4848

4949
return new DecryptionData
5050
{
@@ -54,7 +54,7 @@ public async Task<DecryptionData> DeEncryptFromHexString(DecryptionParameters pa
5454
};
5555
}
5656

57-
public async Task<DecryptionData> DecryptFromBase64String(DecryptionParameters param)
57+
public DecryptionData DecryptFromBase64String(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
5858
{
5959
var responseData = new DecryptionData();
6060

@@ -82,7 +82,7 @@ public async Task<DecryptionData> DecryptFromBase64String(DecryptionParameters p
8282
Data = Convert.FromBase64String(param.DataToDecrypt)
8383
};
8484

85-
var response = await DecryptAES(byteDecryptionParameters);
85+
var response = DecryptAES(byteDecryptionParameters, mode);
8686

8787
return new DecryptionData
8888
{

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Threading.Tasks;
2+
using System.Security.Cryptography;
33
using SafeCrypt.AesEncryption;
44
using SafeCrypt.Helpers;
55
using SafeCrypt.Models;
@@ -22,24 +22,17 @@ public class AesEncryption : BaseAesEncryption
2222
/// <param name="secretKey">The secret key used for encryption.</param>
2323
/// <param name="iv">The initialization vector used for encryption.</param>
2424
/// <returns>The encrypted data as a byte array.</returns>
25-
public async Task<EncryptionData> EncryptToHexString(EncryptionParameters param)
25+
public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC)
2626
{
2727
var responseData = new EncryptionData();
2828

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

31-
// validate is base64
32-
if (!Validators.IsBase64String(param.SecretKey))
31+
if (parameterValidation.HasError)
3332
{
34-
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
35-
return responseData;
33+
return parameterValidation;
3634
}
3735

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

@@ -58,7 +51,7 @@ public async Task<EncryptionData> EncryptToHexString(EncryptionParameters param)
5851
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
5952
};
6053

61-
var response = await EncryptAES(byteEncryptionParameters);
54+
var response = EncryptAES(byteEncryptionParameters, mode);
6255

6356
return new EncryptionData
6457
{
@@ -67,8 +60,7 @@ public async Task<EncryptionData> EncryptToHexString(EncryptionParameters param)
6760
SecretKey = param.SecretKey
6861
};
6962
}
70-
71-
63+
7264
/// <summary>
7365
/// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
7466
/// </summary>
@@ -91,7 +83,7 @@ public async Task<EncryptionData> EncryptToHexString(EncryptionParameters param)
9183
/// <exception cref="FormatException">
9284
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
9385
/// </exception>
94-
public async Task<EncryptionData> EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
86+
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC)
9587
{
9688
// validate is base64
9789
if (!Validators.IsBase64String(base64secretKey))
@@ -111,7 +103,7 @@ public async Task<EncryptionData> EncryptToBase64String(string dataToBeEncrypted
111103
Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray()
112104
};
113105

114-
var response = await EncryptAES(byteEncryptionParameters);
106+
var response = EncryptAES(byteEncryptionParameters, mode);
115107

116108
return new EncryptionData
117109
{
@@ -121,6 +113,26 @@ public async Task<EncryptionData> EncryptToBase64String(string dataToBeEncrypted
121113
};
122114
}
123115

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+
124136
private void NullChecks(string data, string secretKey)
125137
{
126138
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)