diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs index 71aae3f..72b8213 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs @@ -23,7 +23,7 @@ public static async Task DecryptFromHexStringAsync(DecryptionPar { var responseData = new DecryptionData(); - Validators.ValidateNotNull(param); + Validator.ValidateNotNull(param); // validate is base64 if (!Validators.IsBase64String(param.SecretKey)) @@ -52,7 +52,7 @@ public static async Task DecryptFromHexStringAsync(DecryptionPar { SecretKey = Convert.FromBase64String(param.SecretKey), IV = dataBytes, - Data = param.DataToDecrypt.HexadecimalStringToByteArray() + Data = param.Data.HexadecimalStringToByteArray() }; var response = await BaseAesEncryption.DecryptAsync(byteEncryptionParameters, mode); @@ -79,7 +79,7 @@ public static async Task DecryptFromBase64StringAsync(Decryption { var responseData = new DecryptionData(); - Validators.ValidateNotNull(param); + Validator.ValidateNotNull(param); if (!Validators.IsBase64String(param.SecretKey)) @@ -100,7 +100,7 @@ public static async Task DecryptFromBase64StringAsync(Decryption { SecretKey = Convert.FromBase64String(param.SecretKey), IV = Convert.FromBase64String(param.IV), - Data = Convert.FromBase64String(param.DataToDecrypt) + Data = Convert.FromBase64String(param.Data) }; var response = await BaseAesEncryption.DecryptAsync(byteDecryptionParameters, mode); diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs index b68b94c..23e7991 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs @@ -49,7 +49,7 @@ public static async Task EncryptToHexStringAsync(EncryptionParam { SecretKey = Convert.FromBase64String(param.SecretKey), IV = dataBytes, - Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray() + Data = param.Data.ConvertToHexString().HexadecimalStringToByteArray() }; var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode); @@ -118,7 +118,7 @@ private static EncryptionData ValidateEncryptionParameters(EncryptionParameters { var responseData = new EncryptionData(); - Validators.ValidateNotNull(param); + Validator.ValidateNotNull(param); // validate is base64 if (!Validators.IsBase64String(param.SecretKey)) diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/BaseAesEncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/BaseAesEncryptionParameters.cs new file mode 100644 index 0000000..b44ed1d --- /dev/null +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/BaseAesEncryptionParameters.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace SafeCrypt.Models +{ + public class BaseAesEncryptionParameters + { + /// + /// Gets or sets the data to be encrypted. + /// + [Required] + public string Data { get; set; } + + /// + /// Gets or sets the secret key used for encryption. + /// + [Required] + public string SecretKey { get; set; } + + /// + /// Gets or sets the initialization vector (IV) used for encryption. + /// + [Required] + public string IV { get; set; } + } + + public class BaseAesData + { + /// + /// Gets or sets the initialization vector (IV) used for encryption. + /// + public string Iv { get; set; } + + /// + /// Gets or sets the secret key used for encryption. Should be a base64 string + /// + public string SecretKey { get; set; } + + public bool HasError { get; set; } + + public List Errors { get; set; } = new List(); + } +} diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs index 2f318cb..2ecefe5 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionData.cs @@ -2,24 +2,11 @@ namespace SafeCrypt.Models { - public class DecryptionData + public class DecryptionData : BaseAesData { /// - /// Gets or sets the data to be encrypted. + /// Gets or sets the data to be decrypted. /// public string DecryptedData { get; set; } - - /// - /// Gets or sets the initialization vector (IV) used for encryption. - /// - public string Iv { get; set; } - - /// - /// Gets or sets the secret key used for encryption. Should be a base64 string - /// - public string SecretKey { get; set; } - - public bool HasError { get; set; } - public List Errors { get; set; } = new List(); } } diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs index 859d2fa..b78f4d3 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Decrypt/DecryptionParameters.cs @@ -1,28 +1,6 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Text; - -namespace SafeCrypt.Models +namespace SafeCrypt.Models { - public class DecryptionParameters + public class DecryptionParameters : BaseAesEncryptionParameters { - /// - /// Gets or sets the data to be decrypted. - /// - [Required] - public string DataToDecrypt { get; set; } - - /// - /// Gets or sets the secret key used for decryption. - /// - [Required] - public string SecretKey { get; set; } - - /// - /// Gets or sets the initialization vector (IV) used for decryption. - /// - [Required] - public string IV { get; set; } } } diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs index 8af7b78..d4b0c9d 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionData.cs @@ -2,28 +2,11 @@ namespace SafeCrypt.Models { - /// - /// Represents the data and initialization vector (IV) used in encryption. - /// - public class EncryptionData + public class EncryptionData : BaseAesData { /// /// Gets or sets the data to be encrypted. /// public string EncryptedData { get; set; } - - /// - /// Gets or sets the initialization vector (IV) used for encryption. - /// - public string Iv { get; set; } - - /// - /// Gets or sets the secret key used for encryption. Should be a base64 string - /// - public string SecretKey { get; set; } - - public bool HasError { get; set; } - - public List Errors { get; set; } = new List(); } } diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs index e329366..5a31a82 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Models/Encrypt/EncryptionParameters.cs @@ -1,48 +1,10 @@ -using System.ComponentModel.DataAnnotations; - - -namespace SafeCrypt.Models +namespace SafeCrypt.Models { - public class EncryptionParameters + public class EncryptionParameters : BaseAesEncryptionParameters { - /// - /// Gets or sets the data to be encrypted. - /// - [Required] - public string DataToEncrypt { get; set; } - - /// - /// Gets or sets the secret key used for encryption. - /// - [Required] - public string SecretKey { get; set; } - - /// - /// Gets or sets the initialization vector (IV) used for encryption. - /// - [Required] - public string IV { get; set; } } - public class StringEncryptionParameters + public class StringEncryptionParameters : BaseAesEncryptionParameters { - /// - /// Gets or sets the data to be encrypted. - /// - [Required] - public string Data { get; set; } - - /// - /// Gets or sets the secret key used for encryption. - /// - [Required] - public string SecretKey { get; set; } - - /// - /// Gets or sets the initialization vector (IV) used for encryption. - /// - [Required] - public string IV { get; set; } } - } diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs index 95d21d8..24cdb97 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/Models/RsaEncryptionResult.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace SafeCrypt.RsaEncryption.Models { diff --git a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs index a5cd195..d0a0171 100644 --- a/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/RsaEncryption/RsaEncryption.cs @@ -76,7 +76,6 @@ internal static async Task DecryptAsync(byte[] encryptedDat }); result.DecryptedData = decryptedData; - } catch (Exception ex) { diff --git a/src/SafeCrypt.Lib/Helpers/Validators.cs b/src/SafeCrypt.Lib/Helpers/Validators.cs index f06d02d..41a39d6 100644 --- a/src/SafeCrypt.Lib/Helpers/Validators.cs +++ b/src/SafeCrypt.Lib/Helpers/Validators.cs @@ -4,83 +4,7 @@ namespace SafeCrypt.Helpers { internal static class Validators - { - /// - /// Validates that the specified ByteEncryptionParameters instance is not null - /// and that its required properties (Data, SecretKey, IV) are not null. - /// - /// The ByteEncryptionParameters instance to validate. - /// - /// Thrown if the specified parameters instance is null or if any of its required properties are null. - /// - internal static void ValidateNotNull(EncryptionParameters parameters) - { - if (parameters == null) - { - throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null."); - } - - if (parameters.DataToEncrypt == null) - { - throw new ArgumentNullException(nameof(parameters.DataToEncrypt), "DataToEncrypt property cannot be null."); - } - - if (parameters.SecretKey == null) - { - throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null."); - } - - if (parameters.IV == null) - { - throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null."); - } - } - internal static void ValidateNotNull(DecryptionParameters parameters) - { - if (parameters == null) - { - throw new ArgumentNullException(nameof(parameters), "ByteEncryptionParameters instance cannot be null."); - } - - if (parameters.DataToDecrypt == null) - { - throw new ArgumentNullException(nameof(parameters.DataToDecrypt), "DataToDecrypt property cannot be null."); - } - - if (parameters.SecretKey == null) - { - throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null."); - } - - if (parameters.IV == null) - { - throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null."); - } - } - - internal static void ValidateNotNull(StringEncryptionParameters parameters) - { - if (parameters == null) - { - throw new ArgumentNullException(nameof(parameters), "StringEncryptionParameters instance cannot be null."); - } - - if (parameters.Data == null) - { - throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null."); - } - - if (parameters.SecretKey == null) - { - throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null."); - } - - if (parameters.IV == null) - { - throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null."); - } - } - + { /// /// Validates whether the provided string is a valid Base64-encoded key. /// @@ -128,4 +52,38 @@ private static bool IsValidByteArray(byte[] byteArray) return byteArray != null && byteArray.Length > 0; } } + + internal static class Validator where TParameters : BaseAesEncryptionParameters + { + /// + /// Validates that the specified ByteEncryptionParameters instance is not null + /// and that its required properties (Data, SecretKey, IV) are not null. + /// + /// The ByteEncryptionParameters instance to validate. + /// + /// Thrown if the specified parameters instance is null or if any of its required properties are null. + /// + internal static void ValidateNotNull(TParameters parameters) + { + if (parameters == null) + { + throw new ArgumentNullException(nameof(parameters), $"{typeof(TParameters).Name} instance cannot be null."); + } + + if (parameters.Data == null) + { + throw new ArgumentNullException(nameof(parameters.Data), "Data property cannot be null."); + } + + if (parameters.SecretKey == null) + { + throw new ArgumentNullException(nameof(parameters.SecretKey), "SecretKey property cannot be null."); + } + + if (parameters.IV == null) + { + throw new ArgumentNullException(nameof(parameters.IV), "IV property cannot be null."); + } + } + } } diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index 9a69938..54c6d28 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -1,36 +1,8 @@ // See https://aka.ms/new-console-template for more information +using SafeCrypt.App.Usage; -using SafeCrypt.AES; -using SafeCrypt.Models; +RsaUsage.Execute(); -var dataToEncrypt = "Data to Encrypt"; -var secret = "hghjuytsdfraestwsgtere=="; - -// Encryption process -// this method generates a random IV key for the encryption process -// the IV is returned in the response with other properties -var response = await Aes.EncryptToBase64StringAsync(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 decryptionData = await Aes.DecryptFromBase64StringAsync(decryptorParam); - -Console.WriteLine("............Decryption Started............"); -Console.WriteLine($"Decrypted data: {decryptionData.DecryptedData}"); -Console.WriteLine($"IV key: {decryptionData.Iv}"); -Console.WriteLine($"Secret key: {decryptionData.SecretKey}"); +AesUsage.Execute(); Console.ReadLine(); diff --git a/src/SafeCrypt.Test/Usage/AesUsage.cs b/src/SafeCrypt.Test/Usage/AesUsage.cs new file mode 100644 index 0000000..6b83c1f --- /dev/null +++ b/src/SafeCrypt.Test/Usage/AesUsage.cs @@ -0,0 +1,48 @@ +using SafeCrypt.AES; +using SafeCrypt.Models; + +namespace SafeCrypt.App.Usage; + +internal static class AesUsage +{ + internal static async void Execute() + { + Console.WriteLine("------- AES Test Started -------"); + + var dataToEncrypt = "Data to Encrypt"; + var secret = "hghjuytsdfraestwsgtere=="; + + // Encryption process + // this method generates a random IV key for the encryption process + // the IV is returned in the response with other properties + var response = await Aes.EncryptToBase64StringAsync(dataToEncrypt, secret); + + Console.WriteLine("............Encryption Started............"); + + Console.WriteLine($"Encrypted data: {response.EncryptedData}"); + Console.WriteLine($"IV key: {response.Iv}"); + Console.WriteLine($"Secret key: {response.SecretKey}"); + + Console.WriteLine(); + + // Decryption process + var decryptorParam = new DecryptionParameters + { + IV = response.Iv, + SecretKey = secret, + Data = response.EncryptedData + }; + + var decryptionData = await Aes.DecryptFromBase64StringAsync(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(); + + Console.WriteLine("------- AES Test Ended -------"); + + } +} diff --git a/src/SafeCrypt.Test/Usage/RsaUsage.cs b/src/SafeCrypt.Test/Usage/RsaUsage.cs index b9bb1ad..f14488c 100644 --- a/src/SafeCrypt.Test/Usage/RsaUsage.cs +++ b/src/SafeCrypt.Test/Usage/RsaUsage.cs @@ -4,17 +4,21 @@ namespace SafeCrypt.App.Usage; -internal class RsaUsage +internal static class RsaUsage { - internal protected async void Usage() + internal static async void Execute() { + Console.WriteLine("------- RSA Test Started -------"); + // Example: Generate RSA keys var rsaKeyPair = KeyGenerators.GenerateRsaKeys(2048); - string rsaPublicKey = rsaKeyPair.Item1; string rsaPrivateKey = rsaKeyPair.Item2; Console.WriteLine($"pubic key {rsaPublicKey}"); + + Console.WriteLine(); + Console.WriteLine($"private key {rsaPrivateKey}"); // Example: Encrypt and Decrypt using RSA @@ -30,6 +34,8 @@ internal protected async void Usage() Console.WriteLine($"Original Data: {originalData}"); + Console.WriteLine(); + Console.WriteLine("Original byte array: " + BitConverter.ToString(encryptedData.EncryptedData)); string EncryptedDataconvertedString = Convert.ToBase64String(encryptedData.EncryptedData); @@ -49,6 +55,8 @@ internal protected async void Usage() var decryptedData = await Rsa.DecryptAsync(decryptionModel); Console.WriteLine($"{decryptedData.DecryptedData}"); - Console.ReadLine(); + Console.WriteLine(); + + Console.WriteLine("------- RSA Test Ended -------"); } }