Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions SafeCrypt.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,40 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "SafeCrypt.csproj", "{204CA507-752E-43A6-A094-794E40ABAE1F}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0B7C0C60-9850-4554-AF85-86C0378B6B16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "safecrypt-testapp", "..\safecrypt-testapp\safecrypt-testapp.csproj", "{76D17C56-5643-4148-A504-8D6E24D24CAD}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SafeCrypt.Lib", "SafeCrypt.Lib", "{8507D130-9F07-426C-8EE6-0AC714CF72E5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SafeCrypt.App", "SafeCrypt.App", "{1D91E775-F63F-4537-B81E-B8F9A6480D6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt", "src\SafeCrypt.Lib\SafeCrypt.csproj", "{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SafeCrypt.App", "src\SafeCrypt.Test\SafeCrypt.App.csproj", "{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{204CA507-752E-43A6-A094-794E40ABAE1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{204CA507-752E-43A6-A094-794E40ABAE1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{204CA507-752E-43A6-A094-794E40ABAE1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{204CA507-752E-43A6-A094-794E40ABAE1F}.Release|Any CPU.Build.0 = Release|Any CPU
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76D17C56-5643-4148-A504-8D6E24D24CAD}.Release|Any CPU.Build.0 = Release|Any CPU
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8}.Release|Any CPU.Build.0 = Release|Any CPU
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8507D130-9F07-426C-8EE6-0AC714CF72E5} = {0B7C0C60-9850-4554-AF85-86C0378B6B16}
{1D91E775-F63F-4537-B81E-B8F9A6480D6D} = {0B7C0C60-9850-4554-AF85-86C0378B6B16}
{AE9FAE54-9854-4F98-A60F-19125CEAA3A8} = {8507D130-9F07-426C-8EE6-0AC714CF72E5}
{DAD7FFA3-AABC-47FF-BA79-0C9531BFBBE6} = {1D91E775-F63F-4537-B81E-B8F9A6480D6D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {639A4359-2BA4-4F71-9EBF-D6EAB68C84CB}
EndGlobalSection
Expand Down
12 changes: 0 additions & 12 deletions src/Enums/ReturnType.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using SafeCrypt.Models;
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace SafeCrypt.AesEncryption
{
Expand All @@ -25,7 +23,7 @@ public class BaseAesEncryption
/// <exception cref="Exception">
/// Thrown for general encryption-related exceptions.
/// </exception>
internal static byte[] EncryptAES(ByteEncryptionParameters param)
internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC)
{
try
{
Expand All @@ -35,6 +33,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
// Set the key and initialization vector
aes.Key = param.SecretKey;
aes.IV = param.IV;
aes.Mode = mode;
// Create an encryptor using the key and initialization vector
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

Expand Down Expand Up @@ -73,7 +72,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param)
/// <exception cref="ArgumentNullException">
/// Thrown if the input encrypted data, key, or initialization vector is null.
/// </exception>
internal static byte[] DecryptAES(ByteDecryptionParameters param)
internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC)
{
try
{
Expand All @@ -83,6 +82,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param)
// Set the key and initialization vector
aes.Key = param.SecretKey;
aes.IV = param.IV;
aes.Mode= mode;

// Create a decryptor using the key and initialization vector
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
Expand All @@ -106,7 +106,6 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param)
}
catch (Exception ex)
{

throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using SafeCrypt.Helpers;
using SafeCrypt.Models;
using System;
using System.Security.Cryptography;

namespace SafeCrypt.AESDecryption
{
public class AesDecryption : BaseAesEncryption
{
public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
{
var responseData = new DecryptionData();

Expand Down Expand Up @@ -43,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
};

var response = DecryptAES(byteEncryptionParameters);
var response = DecryptAES(byteEncryptionParameters, mode);

return new DecryptionData
{
Expand All @@ -53,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
};
}

public DecryptionData DecryptFromBase64String(DecryptionParameters param)
public DecryptionData DecryptFromBase64String(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
{
var responseData = new DecryptionData();

Expand Down Expand Up @@ -81,7 +82,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param)
Data = Convert.FromBase64String(param.DataToDecrypt)
};

var response = DecryptAES(byteDecryptionParameters);
var response = DecryptAES(byteDecryptionParameters, mode);

return new DecryptionData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Security.Cryptography;
using SafeCrypt.AesEncryption;
using SafeCrypt.Helpers;
using SafeCrypt.Models;
Expand All @@ -21,24 +22,17 @@ public class AesEncryption : BaseAesEncryption
/// <param name="secretKey">The secret key used for encryption.</param>
/// <param name="iv">The initialization vector used for encryption.</param>
/// <returns>The encrypted data as a byte array.</returns>
public EncryptionData EncryptToHexString(EncryptionParameters param)
public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC)
{
var responseData = new EncryptionData();

Validators.ValidateNotNull(param);
var parameterValidation = ValidateEncryptionParameters(param);

// validate is base64
if (!Validators.IsBase64String(param.SecretKey))
if (parameterValidation.HasError)
{
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
return responseData;
return parameterValidation;
}

if (!Validators.IsBase64String(param.IV))
{
AddError(responseData, $"IV: {param.IV} is not a base64 string");
return responseData;
}
// Convert input string to bytes
byte[] dataBytes = param.IV.ConvertKeysToBytes();

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

var response = EncryptAES(byteEncryptionParameters);
var response = EncryptAES(byteEncryptionParameters, mode);

return new EncryptionData
{
Expand All @@ -66,8 +60,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
SecretKey = param.SecretKey
};
}



/// <summary>
/// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
/// </summary>
Expand All @@ -90,7 +83,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
/// <exception cref="FormatException">
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
/// </exception>
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC)
{
// validate is base64
if (!Validators.IsBase64String(base64secretKey))
Expand All @@ -110,7 +103,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray()
};

var response = EncryptAES(byteEncryptionParameters);
var response = EncryptAES(byteEncryptionParameters, mode);

return new EncryptionData
{
Expand All @@ -120,6 +113,26 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
};
}

private EncryptionData ValidateEncryptionParameters(EncryptionParameters param)
{
var responseData = new EncryptionData();

Validators.ValidateNotNull(param);

// validate is base64
if (!Validators.IsBase64String(param.SecretKey))
{
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
}

if (!Validators.IsBase64String(param.IV))
{
AddError(responseData, $"IV: {param.IV} is not a base64 string");
}

return responseData;
}

private void NullChecks(string data, string secretKey)
{
if (data == null || data.Length <= 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace SafeCrypt.Models
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace SafeCrypt.Models
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace SafeCrypt.Models
{
Expand All @@ -25,6 +23,7 @@ public class EncryptionData
public string SecretKey { get; set; }

public bool HasError { get; set; }

public List<string> Errors { get; set; } = new List<string>();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.ComponentModel.DataAnnotations;


namespace SafeCrypt.Models
{
Expand Down
8 changes: 8 additions & 0 deletions src/SafeCrypt.Lib/Enums/ReturnType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SafeCrypt.Enums
{
public enum ReturnType
{
Hex = 1,
Bytes
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static byte[] HexadecimalStringToByteArray(this string input)
}
return output;
}

/// <summary>
/// Converts a string to its hexadecimal representation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ public static byte[] GenerateRandomIVKeyAsBytes(int length)
return randomBytes;
}

/// <summary>
/// Generates a random initialization vector (IV) key as a hexadecimal string.
/// </summary>
/// <returns>
/// A hexadecimal string representation of the randomly generated IV key.
/// </returns>
/// <remarks>
/// This method internally uses the <see cref="GenerateRandomIVKeyAsBytes"/> method
/// to obtain a random byte array and then converts it to a hexadecimal string using
/// <see cref="BitConverter.ToString"/>. Any hyphens in the resulting string are removed
/// using <see cref="string.Replace"/>.
/// </remarks>
public static string GenerateRandomIVKeyAsString()
{
byte[] randomBytes = GenerateRandomIVKeyAsBytes(16);
return BitConverter.ToString(randomBytes).Replace("-", "");
}

/// <summary>
/// Generates a valid AES secret key with the specified key size.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using SafeCrypt.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace SafeCrypt.Helpers
{
public static class Validators
internal static class Validators
{
/// <summary>
/// Validates that the specified ByteEncryptionParameters instance is not null
Expand All @@ -15,7 +13,7 @@ public static class Validators
/// <exception cref="ArgumentNullException">
/// Thrown if the specified parameters instance is null or if any of its required properties are null.
/// </exception>
public static void ValidateNotNull(EncryptionParameters parameters)
internal static void ValidateNotNull(EncryptionParameters parameters)
{
if (parameters == null)
{
Expand All @@ -37,7 +35,7 @@ public static void ValidateNotNull(EncryptionParameters parameters)
throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null.");
}
}
public static void ValidateNotNull(DecryptionParameters parameters)
internal static void ValidateNotNull(DecryptionParameters parameters)
{
if (parameters == null)
{
Expand All @@ -60,7 +58,7 @@ public static void ValidateNotNull(DecryptionParameters parameters)
}
}

public static void ValidateNotNull(StringEncryptionParameters parameters)
internal static void ValidateNotNull(StringEncryptionParameters parameters)
{
if (parameters == null)
{
Expand Down Expand Up @@ -88,7 +86,7 @@ public static void ValidateNotNull(StringEncryptionParameters parameters)
/// </summary>
/// <param name="keyAsString">The string to validate.</param>
/// <returns>True if the string is a valid Base64-encoded key; otherwise, false.</returns>
public static bool IsBase64String(string keyAsString)
internal static bool IsBase64String(string keyAsString)
{
if (string.IsNullOrEmpty(keyAsString))
{
Expand All @@ -111,7 +109,7 @@ public static bool IsBase64String(string keyAsString)
/// </summary>
/// <param name="dataLength">The length of the data to be encrypted.</param>
/// <returns>True if the block size is valid; otherwise, false.</returns>
public static bool IsValidBlockSize(int dataLength)
internal static bool IsValidBlockSize(int dataLength)
{
// AES block size is 128 bits (16 bytes)
return dataLength % 16 == 0;
Expand Down
Loading