From 806b25b0156a11735451a9939e934e0ab8851e5e Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 17 Dec 2023 00:58:59 +0100 Subject: [PATCH 01/14] feat: add ValidateEncryptionParameters method --- src/Encryption/AesEncryption/Encrypting.cs | 38 ++++++++++++++-------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Encryption/AesEncryption/Encrypting.cs b/src/Encryption/AesEncryption/Encrypting.cs index 12216cc..4215fd0 100644 --- a/src/Encryption/AesEncryption/Encrypting.cs +++ b/src/Encryption/AesEncryption/Encrypting.cs @@ -1,8 +1,8 @@ using System; +using System.Security.Cryptography; using SafeCrypt.AesEncryption; using SafeCrypt.Helpers; using SafeCrypt.Models; -using SafeCrypt.src.Encryption.AesEncryption.Models; namespace SafeCrypt.AESEncryption { @@ -26,20 +26,13 @@ public EncryptionData EncryptToHexString(EncryptionParameters param) { 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(); @@ -67,8 +60,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param) SecretKey = param.SecretKey }; } - - + /// /// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm. /// @@ -121,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) From d65378d6d79962d6e4c410d09f14fec1120f152a Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 17 Dec 2023 01:00:55 +0100 Subject: [PATCH 02/14] feat: add cipher mode to base aes encryption --- src/Encryption/AesEncryption/BaseAesEncryption.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index 1154227..0665f99 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -1,4 +1,4 @@ -using SafeCrypt.src.Encryption.AesEncryption.Models; +using SafeCrypt.Models; using System; using System.IO; using System.Reflection; @@ -7,7 +7,7 @@ namespace SafeCrypt.AesEncryption { - internal class BaseAesEncryption + public class BaseAesEncryption { /// /// Encrypts the provided data using the Advanced Encryption Standard (AES) algorithm. @@ -25,7 +25,7 @@ internal class BaseAesEncryption /// /// Thrown for general encryption-related exceptions. /// - internal static byte[] EncryptAES(ByteEncryptionParameters param) + internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC) { try { @@ -35,6 +35,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); @@ -73,7 +74,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param) /// /// Thrown if the input encrypted data, key, or initialization vector is null. /// - internal static byte[] DecryptAES(ByteDecryptionParameters param) + internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC) { try { @@ -83,6 +84,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); From 1024fae3b304fa3b49dfcb5537d091e6abfe9193 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 17 Dec 2023 01:02:07 +0100 Subject: [PATCH 03/14] feat: include cipher mode in encryption and decryption, default cipher mode is set to CBC --- src/Encryption/AesEncryption/Decrypting.cs | 10 +++++----- src/Encryption/AesEncryption/Encrypting.cs | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Encryption/AesEncryption/Decrypting.cs b/src/Encryption/AesEncryption/Decrypting.cs index 610f942..eda6bbb 100644 --- a/src/Encryption/AesEncryption/Decrypting.cs +++ b/src/Encryption/AesEncryption/Decrypting.cs @@ -1,14 +1,14 @@ using SafeCrypt.AesEncryption; using SafeCrypt.Helpers; using SafeCrypt.Models; -using SafeCrypt.src.Encryption.AesEncryption.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(); @@ -44,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param) Data = param.DataToDecrypt.HexadecimalStringToByteArray() }; - var response = DecryptAES(byteEncryptionParameters); + var response = DecryptAES(byteEncryptionParameters, mode); return new DecryptionData { @@ -54,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(); @@ -82,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 { diff --git a/src/Encryption/AesEncryption/Encrypting.cs b/src/Encryption/AesEncryption/Encrypting.cs index 4215fd0..10900ad 100644 --- a/src/Encryption/AesEncryption/Encrypting.cs +++ b/src/Encryption/AesEncryption/Encrypting.cs @@ -22,7 +22,7 @@ public class AesEncryption : BaseAesEncryption /// The secret key used for encryption. /// The initialization vector used for encryption. /// The encrypted data as a byte array. - public EncryptionData EncryptToHexString(EncryptionParameters param) + public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new EncryptionData(); @@ -51,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 { @@ -83,7 +83,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param) /// /// Thrown if the base64secretKey is not a valid Base64-encoded string. /// - 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)) @@ -103,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 { From 1b4509d4a0738d2635b2771b442e7a628d3e660b Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 17 Dec 2023 20:57:16 +0100 Subject: [PATCH 04/14] cleanup: remove unused usings --- src/Encryption/AesEncryption/BaseAesEncryption.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index 0665f99..546adb5 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -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 { From af63fd6e8ff29db46c7208211b729a46d257c31d Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 17 Dec 2023 20:57:59 +0100 Subject: [PATCH 05/14] feat: set validator class visibility to internal --- src/Helpers/Validators.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Helpers/Validators.cs b/src/Helpers/Validators.cs index a677b3d..f06d02d 100644 --- a/src/Helpers/Validators.cs +++ b/src/Helpers/Validators.cs @@ -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 { /// /// Validates that the specified ByteEncryptionParameters instance is not null @@ -15,7 +13,7 @@ public static class Validators /// /// Thrown if the specified parameters instance is null or if any of its required properties are null. /// - public static void ValidateNotNull(EncryptionParameters parameters) + internal static void ValidateNotNull(EncryptionParameters parameters) { if (parameters == null) { @@ -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) { @@ -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) { @@ -88,7 +86,7 @@ public static void ValidateNotNull(StringEncryptionParameters parameters) /// /// The string to validate. /// True if the string is a valid Base64-encoded key; otherwise, false. - public static bool IsBase64String(string keyAsString) + internal static bool IsBase64String(string keyAsString) { if (string.IsNullOrEmpty(keyAsString)) { @@ -111,7 +109,7 @@ public static bool IsBase64String(string keyAsString) /// /// The length of the data to be encrypted. /// True if the block size is valid; otherwise, false. - public static bool IsValidBlockSize(int dataLength) + internal static bool IsValidBlockSize(int dataLength) { // AES block size is 128 bits (16 bytes) return dataLength % 16 == 0; From 1d3e017a32b66435660ddb12d2902fe1135b9a80 Mon Sep 17 00:00:00 2001 From: "eanolue@sbsc.com" Date: Mon, 18 Dec 2023 04:47:52 +0100 Subject: [PATCH 06/14] removed unnecessary using statements --- src/Encryption/AesEncryption/BaseAesEncryption.cs | 2 -- .../AesEncryption/Models/ByteEncryptionParameters.cs | 5 +---- .../AesEncryption/Models/Decrypt/DecryptionData.cs | 4 +--- .../AesEncryption/Models/Encrypt/EncryptionData.cs | 4 +--- .../AesEncryption/Models/Encrypt/EncryptionParameters.cs | 6 ++---- src/Enums/ReturnType.cs | 6 +----- src/Helpers/Validators.cs | 2 -- 7 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index 9a35ca9..d44a5fd 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -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 { diff --git a/src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs b/src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs index f5be539..0f05841 100644 --- a/src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs +++ b/src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Text; +using System.ComponentModel.DataAnnotations; namespace SafeCrypt.Models { diff --git a/src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs b/src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs index 55f62d4..2f318cb 100644 --- a/src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs +++ b/src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace SafeCrypt.Models { diff --git a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs b/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs index dcc6f13..1196ebf 100644 --- a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs +++ b/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace SafeCrypt.Models { diff --git a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs b/src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs index fd2363a..e329366 100644 --- a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs +++ b/src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Text; +using System.ComponentModel.DataAnnotations; + namespace SafeCrypt.Models { diff --git a/src/Enums/ReturnType.cs b/src/Enums/ReturnType.cs index 2a7007a..4352d34 100644 --- a/src/Enums/ReturnType.cs +++ b/src/Enums/ReturnType.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace SafeCrypt.Enums +namespace SafeCrypt.Enums { public enum ReturnType { diff --git a/src/Helpers/Validators.cs b/src/Helpers/Validators.cs index a677b3d..2fad05f 100644 --- a/src/Helpers/Validators.cs +++ b/src/Helpers/Validators.cs @@ -1,7 +1,5 @@ using SafeCrypt.Models; using System; -using System.Collections.Generic; -using System.Text; namespace SafeCrypt.Helpers { From dc7eccc7fc6c40cef5e28a0a0e0355c04da1d62b Mon Sep 17 00:00:00 2001 From: "eanolue@sbsc.com" Date: Mon, 18 Dec 2023 04:52:07 +0100 Subject: [PATCH 07/14] clean up --- src/Encryption/AesEncryption/BaseAesEncryption.cs | 5 ++--- .../AesEncryption/Models/Encrypt/EncryptionData.cs | 1 + src/Helpers/Converters.cs | 1 + src/Helpers/Validators.cs | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index d44a5fd..a9d46ce 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -52,7 +52,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param) } } } - catch (Exception ex) + catch (Exception) { throw; } @@ -102,9 +102,8 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param) } } } - catch (Exception ex) + catch (Exception) { - throw; } } diff --git a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs b/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs index 1196ebf..8af7b78 100644 --- a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs +++ b/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs @@ -23,6 +23,7 @@ public class EncryptionData public string SecretKey { get; set; } public bool HasError { get; set; } + public List Errors { get; set; } = new List(); } } diff --git a/src/Helpers/Converters.cs b/src/Helpers/Converters.cs index 57a6e3d..d254060 100644 --- a/src/Helpers/Converters.cs +++ b/src/Helpers/Converters.cs @@ -37,6 +37,7 @@ public static byte[] HexadecimalStringToByteArray(this string input) } return output; } + /// /// Converts a string to its hexadecimal representation. /// diff --git a/src/Helpers/Validators.cs b/src/Helpers/Validators.cs index 2fad05f..19d8702 100644 --- a/src/Helpers/Validators.cs +++ b/src/Helpers/Validators.cs @@ -35,6 +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) { if (parameters == null) From a8c76e0c26424e3d9a5814b99e993f2da22f2d7b Mon Sep 17 00:00:00 2001 From: "eanolue@sbsc.com" Date: Thu, 21 Dec 2023 11:16:24 +0100 Subject: [PATCH 08/14] reverted the ex based on discussion with Raph --- src/Encryption/AesEncryption/BaseAesEncryption.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index a9d46ce..a17a6b9 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -52,7 +52,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param) } } } - catch (Exception) + catch (Exception ex) { throw; } @@ -102,7 +102,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param) } } } - catch (Exception) + catch (Exception ex) { throw; } From 7f41ac1d001d0ee3d2d4dca5dcf24afb269f8e22 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 25 Dec 2023 20:25:41 +0100 Subject: [PATCH 09/14] feat: add method generate random iv key as string --- src/Helpers/KeyGenerators.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Helpers/KeyGenerators.cs b/src/Helpers/KeyGenerators.cs index 1629806..a260d18 100644 --- a/src/Helpers/KeyGenerators.cs +++ b/src/Helpers/KeyGenerators.cs @@ -30,6 +30,12 @@ public static byte[] GenerateRandomIVKeyAsBytes(int length) return randomBytes; } + public static string GenerateRandomIVKeyAsString() + { + byte[] randomBytes = GenerateRandomIVKeyAsBytes(16); + return BitConverter.ToString(randomBytes).Replace("-", ""); + } + /// /// Generates a valid AES secret key with the specified key size. /// From f1ae704dba1d50bcd9c8b4eae23c89a87e628e45 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Mon, 25 Dec 2023 20:26:29 +0100 Subject: [PATCH 10/14] feat: add code documentation for GenerateRandomIVKeyAsString method --- src/Helpers/KeyGenerators.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Helpers/KeyGenerators.cs b/src/Helpers/KeyGenerators.cs index a260d18..44a2c5a 100644 --- a/src/Helpers/KeyGenerators.cs +++ b/src/Helpers/KeyGenerators.cs @@ -30,6 +30,18 @@ public static byte[] GenerateRandomIVKeyAsBytes(int length) return randomBytes; } + /// + /// Generates a random initialization vector (IV) key as a hexadecimal string. + /// + /// + /// A hexadecimal string representation of the randomly generated IV key. + /// + /// + /// This method internally uses the method + /// to obtain a random byte array and then converts it to a hexadecimal string using + /// . Any hyphens in the resulting string are removed + /// using . + /// public static string GenerateRandomIVKeyAsString() { byte[] randomBytes = GenerateRandomIVKeyAsBytes(16); From 3f8bb82d5ada567431892e8eb74ff1fbfdd482dd Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 18 Jan 2024 12:08:07 +0100 Subject: [PATCH 11/14] feat: project restructuring --- SafeCrypt.sln | 32 ++++++++++----- .../AesEncryption/BaseAesEncryption.cs | 0 .../Encryption/AesEncryption/Decrypting.cs | 0 .../Encryption/AesEncryption/Encrypting.cs | 0 .../Models/ByteEncryptionParameters.cs | 0 .../Models/Decrypt/DecryptionData.cs | 0 .../Models/Decrypt/DecryptionParameters.cs | 0 .../Models/Encrypt/EncryptionData.cs | 0 .../Models/Encrypt/EncryptionParameters.cs | 0 src/{ => SafeCrypt.Lib}/Enums/ReturnType.cs | 0 src/{ => SafeCrypt.Lib}/Helpers/Converters.cs | 0 .../Helpers/KeyGenerators.cs | 0 src/{ => SafeCrypt.Lib}/Helpers/Validators.cs | 0 LICENSE => src/SafeCrypt.Lib/LICENSE | 0 README.md => src/SafeCrypt.Lib/README.md | 0 .../SafeCrypt.Lib/SafeCrypt.csproj | 4 +- src/SafeCrypt.Test/Program.cs | 40 +++++++++++++++++++ src/SafeCrypt.Test/SafeCrypt.App.csproj | 15 +++++++ 18 files changed, 79 insertions(+), 12 deletions(-) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/BaseAesEncryption.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Decrypting.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Encrypting.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs (100%) rename src/{ => SafeCrypt.Lib}/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs (100%) rename src/{ => SafeCrypt.Lib}/Enums/ReturnType.cs (100%) rename src/{ => SafeCrypt.Lib}/Helpers/Converters.cs (100%) rename src/{ => SafeCrypt.Lib}/Helpers/KeyGenerators.cs (100%) rename src/{ => SafeCrypt.Lib}/Helpers/Validators.cs (100%) rename LICENSE => src/SafeCrypt.Lib/LICENSE (100%) rename README.md => src/SafeCrypt.Lib/README.md (100%) rename SafeCrypt.csproj => src/SafeCrypt.Lib/SafeCrypt.csproj (95%) create mode 100644 src/SafeCrypt.Test/Program.cs create mode 100644 src/SafeCrypt.Test/SafeCrypt.App.csproj diff --git a/SafeCrypt.sln b/SafeCrypt.sln index cd54c71..c639949 100644 --- a/SafeCrypt.sln +++ b/SafeCrypt.sln @@ -3,9 +3,15 @@ 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 @@ -13,18 +19,24 @@ Global 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 diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs similarity index 100% rename from src/Encryption/AesEncryption/BaseAesEncryption.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs diff --git a/src/Encryption/AesEncryption/Decrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs similarity index 100% rename from src/Encryption/AesEncryption/Decrypting.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs diff --git a/src/Encryption/AesEncryption/Encrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs similarity index 100% rename from src/Encryption/AesEncryption/Encrypting.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs diff --git a/src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs similarity index 100% rename from src/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/ByteEncryptionParameters.cs diff --git a/src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs similarity index 100% rename from src/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs diff --git a/src/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs similarity index 100% rename from src/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs diff --git a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs similarity index 100% rename from src/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs diff --git a/src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs similarity index 100% rename from src/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs rename to src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs diff --git a/src/Enums/ReturnType.cs b/src/SafeCrypt.Lib/Enums/ReturnType.cs similarity index 100% rename from src/Enums/ReturnType.cs rename to src/SafeCrypt.Lib/Enums/ReturnType.cs diff --git a/src/Helpers/Converters.cs b/src/SafeCrypt.Lib/Helpers/Converters.cs similarity index 100% rename from src/Helpers/Converters.cs rename to src/SafeCrypt.Lib/Helpers/Converters.cs diff --git a/src/Helpers/KeyGenerators.cs b/src/SafeCrypt.Lib/Helpers/KeyGenerators.cs similarity index 100% rename from src/Helpers/KeyGenerators.cs rename to src/SafeCrypt.Lib/Helpers/KeyGenerators.cs diff --git a/src/Helpers/Validators.cs b/src/SafeCrypt.Lib/Helpers/Validators.cs similarity index 100% rename from src/Helpers/Validators.cs rename to src/SafeCrypt.Lib/Helpers/Validators.cs diff --git a/LICENSE b/src/SafeCrypt.Lib/LICENSE similarity index 100% rename from LICENSE rename to src/SafeCrypt.Lib/LICENSE diff --git a/README.md b/src/SafeCrypt.Lib/README.md similarity index 100% rename from README.md rename to src/SafeCrypt.Lib/README.md diff --git a/SafeCrypt.csproj b/src/SafeCrypt.Lib/SafeCrypt.csproj similarity index 95% rename from SafeCrypt.csproj rename to src/SafeCrypt.Lib/SafeCrypt.csproj index a6ccafb..6facb42 100644 --- a/SafeCrypt.csproj +++ b/src/SafeCrypt.Lib/SafeCrypt.csproj @@ -49,11 +49,11 @@ Thank you for using the SafeCrypt Library! - + True \ - + True \ diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs new file mode 100644 index 0000000..6df6ccb --- /dev/null +++ b/src/SafeCrypt.Test/Program.cs @@ -0,0 +1,40 @@ +// See https://aka.ms/new-console-template for more information + +using SafeCrypt.AESDecryption; +using SafeCrypt.AESEncryption; +using SafeCrypt.Models; + +var dataToEncrypt = "Data to Encrypt"; +var secret = "hghjuytsdfraestwsgtere=="; + +// Encryption process +var encryptor = new AesEncryption(); +// this method generates a random IV key for the encryption process +// the IV is returned in the response with other properties +var response = encryptor.EncryptToBase64String(dataToEncrypt, secret); + +//Console.WriteLine("............Encryption Started............"); + +//Console.WriteLine($"Encrypted data: {response.EncryptedData}"); +//Console.WriteLine($"IV key: {response.Iv}"); +//Console.WriteLine($"Secret key: {response.SecretKey}"); + + +// Decryption process +var decryptorParam = new DecryptionParameters +{ + IV = response.Iv, + SecretKey = secret, + DataToDecrypt = response.EncryptedData +}; + +var decryptor = new AesDecryption(); +var decryptionData = decryptor.DecryptFromBase64String(decryptorParam); + +Console.WriteLine("............Decryption Started............"); +Console.WriteLine($"Decrypted data: { decryptionData.DecryptedData }"); +Console.WriteLine($"IV key: {decryptionData.Iv}"); +Console.WriteLine($"Secret key: {decryptionData.SecretKey}"); + + +Console.WriteLine("Hello, World!"); diff --git a/src/SafeCrypt.Test/SafeCrypt.App.csproj b/src/SafeCrypt.Test/SafeCrypt.App.csproj new file mode 100644 index 0000000..c1c02fd --- /dev/null +++ b/src/SafeCrypt.Test/SafeCrypt.App.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + safecrypt_testapp + enable + enable + + + + + + + From 9eeded6d025aaf2e49ce9ee4245de9f0ac88118b Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 18 Jan 2024 13:10:13 +0100 Subject: [PATCH 12/14] feat: update readme location --- SafeCrypt.sln | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SafeCrypt.sln b/SafeCrypt.sln index c639949..7b43d2e 100644 --- a/SafeCrypt.sln +++ b/SafeCrypt.sln @@ -4,6 +4,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 17.8.34322.80 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0B7C0C60-9850-4554-AF85-86C0378B6B16}" + ProjectSection(SolutionItems) = preProject + ..\..\..\Downloads\MitLicense.txt = ..\..\..\Downloads\MitLicense.txt + src\SafeCrypt.Lib\README.md = src\SafeCrypt.Lib\README.md + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SafeCrypt.Lib", "SafeCrypt.Lib", "{8507D130-9F07-426C-8EE6-0AC714CF72E5}" EndProject From 5110f3a4940036631839af2f0d8383db5011d0de Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 18 Jan 2024 13:12:51 +0100 Subject: [PATCH 13/14] feat: change readme location --- src/SafeCrypt.Lib/LICENSE => LICENSE | 0 src/SafeCrypt.Lib/README.md => README.md | 0 SafeCrypt.sln | 4 ---- 3 files changed, 4 deletions(-) rename src/SafeCrypt.Lib/LICENSE => LICENSE (100%) rename src/SafeCrypt.Lib/README.md => README.md (100%) diff --git a/src/SafeCrypt.Lib/LICENSE b/LICENSE similarity index 100% rename from src/SafeCrypt.Lib/LICENSE rename to LICENSE diff --git a/src/SafeCrypt.Lib/README.md b/README.md similarity index 100% rename from src/SafeCrypt.Lib/README.md rename to README.md diff --git a/SafeCrypt.sln b/SafeCrypt.sln index 7b43d2e..c639949 100644 --- a/SafeCrypt.sln +++ b/SafeCrypt.sln @@ -4,10 +4,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 17.8.34322.80 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0B7C0C60-9850-4554-AF85-86C0378B6B16}" - ProjectSection(SolutionItems) = preProject - ..\..\..\Downloads\MitLicense.txt = ..\..\..\Downloads\MitLicense.txt - src\SafeCrypt.Lib\README.md = src\SafeCrypt.Lib\README.md - EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SafeCrypt.Lib", "SafeCrypt.Lib", "{8507D130-9F07-426C-8EE6-0AC714CF72E5}" EndProject From 68719cb852aae93835977088d1a5f9f81fe52f91 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Thu, 18 Jan 2024 13:15:32 +0100 Subject: [PATCH 14/14] feat: add readme file to pack --- src/SafeCrypt.Lib/SafeCrypt.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/SafeCrypt.Lib/SafeCrypt.csproj b/src/SafeCrypt.Lib/SafeCrypt.csproj index f670fc3..cf1749e 100644 --- a/src/SafeCrypt.Lib/SafeCrypt.csproj +++ b/src/SafeCrypt.Lib/SafeCrypt.csproj @@ -47,6 +47,10 @@ Thank you for using the SafeCrypt Library! True \ + + True + \ +