diff --git a/README.md b/README.md index 6965cd2..bbd703c 100644 --- a/README.md +++ b/README.md @@ -39,23 +39,19 @@ Now, you can reference the SafeCrypt library in your C# project. To use the library in your C# application, instantiate the `AesEncryption` or `AesDecryption` class and call the provided methods. Here's a simple example: ```csharp -using SafeCrypt.AESDecryption; -using SafeCrypt.AESEncryption; +using SafeCrypt.AES; using SafeCrypt.Models; class Program { static async Task Main() { - var aesEncryptor = new AesEncryption(); - var encryptedData = await aesEncryptor.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" + var encryptedData = await Aes.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" Console.WriteLine($"Encrypted Data: {encryptedData.EncryptedData}"); Console.WriteLine($"Initialization Vector: {encryptedData.Iv}"); - - var aesDecryptor = new AesDecryption(); - + var parameterToDecrypt = new DecryptionParameters { DataToDecrypt = encryptedData.EncryptedData, @@ -64,7 +60,7 @@ class Program }; - var data = await aesDecryptor.DecryptFromBase64StringAsync(parameterToDecrypt) + var data = await Aes.DecryptFromBase64StringAsync(parameterToDecrypt) Console.WriteLine($"Decrypted Data: {data.DecryptedData}"); Console.WriteLine($"Initialization Vector: {data.Iv}"); @@ -74,8 +70,7 @@ class Program ------------------------------------------------------------------------------------------------------- -using SafeCrypt.AESDecryption; -using SafeCrypt.AESEncryption; +using SafeCrypt.AES; using SafeCrypt.Models; class Program @@ -94,9 +89,8 @@ class Program SecretKey = secret }; - var encryptor = new AesEncryption(); - var response = await encryptor.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); + var response = await Aes.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); Console.WriteLine(response.EncryptedData); Console.WriteLine(response.Iv); @@ -112,8 +106,7 @@ class Program }; - var decryptor = new AesDecryption(); - var decryptionData = await decryptor.DecryptFromBase64StringAsync(decryptorParam); + var decryptionData = await Aes.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine(decryptionData.DecryptedData); Console.WriteLine(decryptionData.Iv); diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs index e4d1da7..46ff7d7 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs @@ -6,7 +6,7 @@ namespace SafeCrypt.AesEncryption { - public class BaseAesEncryption + public static class BaseAesEncryption { /// /// Encrypts the provided data using the Advanced Encryption Standard (AES) algorithm. diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs index ae42d21..82164e4 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs @@ -5,9 +5,9 @@ using System.Security.Cryptography; using System.Threading.Tasks; -namespace SafeCrypt.AESDecryption +namespace SafeCrypt.AES { - public class AesDecryption : BaseAesEncryption + public static partial class Aes { /// /// Asynchronously decrypts data from a hexadecimal string using the specified decryption parameters and cipher mode. @@ -19,7 +19,7 @@ public class AesDecryption : BaseAesEncryption /// The task result is a object containing the decrypted data, IV, and secret key. /// If decryption fails, the object will contain error information. /// - public async Task DecryptFromHexStringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) + public static async Task DecryptFromHexStringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new DecryptionData(); @@ -55,7 +55,7 @@ public async Task DecryptFromHexStringAsync(DecryptionParameters Data = param.DataToDecrypt.HexadecimalStringToByteArray() }; - var response = await DecryptAsync(byteEncryptionParameters, mode); + var response = await BaseAesEncryption.DecryptAsync(byteEncryptionParameters, mode); return new DecryptionData { @@ -75,7 +75,7 @@ public async Task DecryptFromHexStringAsync(DecryptionParameters /// The task result is a object containing the decrypted data, IV, and secret key. /// If decryption fails, the object will contain error information. /// - public async Task DecryptFromBase64StringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) + public static async Task DecryptFromBase64StringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new DecryptionData(); @@ -103,7 +103,7 @@ public async Task DecryptFromBase64StringAsync(DecryptionParamet Data = Convert.FromBase64String(param.DataToDecrypt) }; - var response = await DecryptAsync(byteDecryptionParameters, mode); + var response = await BaseAesEncryption.DecryptAsync(byteDecryptionParameters, mode); return new DecryptionData { @@ -121,7 +121,7 @@ public async Task DecryptFromBase64StringAsync(DecryptionParamet } - private void NullChecks(string data, string secretKey, string iv) + private static void NullChecks(string data, string secretKey, string iv) { if (data == null || data.Length <= 0) throw new ArgumentNullException(nameof(data)); @@ -133,13 +133,13 @@ private void NullChecks(string data, string secretKey, string iv) throw new ArgumentNullException(nameof(iv)); } - private (byte[], byte[]) ConvertKeysToBytesAndGetKeys(string secretKey, string iv) + private static (byte[], byte[]) ConvertKeysToBytesAndGetKeys(string secretKey, string iv) { return (secretKey.ConvertKeysToBytes(), iv.ConvertKeysToBytes()); } - private void AddError(DecryptionData responseData, string error) + private static void AddError(DecryptionData responseData, string error) { responseData.HasError = true; responseData.Errors.Add(error); diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs index ca76d78..293af57 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs @@ -5,9 +5,9 @@ using SafeCrypt.Helpers; using SafeCrypt.Models; -namespace SafeCrypt.AESEncryption +namespace SafeCrypt.AES { - public class AesEncryption : BaseAesEncryption + public static partial class Aes { /// /// Asynchronously encrypts the provided data using the specified secret key and initialization vector (IV). @@ -23,7 +23,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 async Task EncryptToHexStringAsync(EncryptionParameters param, CipherMode mode = CipherMode.CBC) + public static async Task EncryptToHexStringAsync(EncryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new EncryptionData(); @@ -52,7 +52,7 @@ public async Task EncryptToHexStringAsync(EncryptionParameters p Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = await EncryptAsync(byteEncryptionParameters, mode); + var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode); return new EncryptionData { @@ -84,7 +84,7 @@ public async Task EncryptToHexStringAsync(EncryptionParameters p /// /// Thrown if the base64secretKey is not a valid Base64-encoded string. /// - public async Task EncryptToBase64StringAsync(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC) + public static async Task EncryptToBase64StringAsync(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC) { // validate is base64 if (!Validators.IsBase64String(base64secretKey)) @@ -104,7 +104,7 @@ public async Task EncryptToBase64StringAsync(string dataToBeEncr Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = await EncryptAsync(byteEncryptionParameters, mode); + var response = await BaseAesEncryption.EncryptAsync(byteEncryptionParameters, mode); return new EncryptionData { @@ -114,7 +114,7 @@ public async Task EncryptToBase64StringAsync(string dataToBeEncr }; } - private EncryptionData ValidateEncryptionParameters(EncryptionParameters param) + private static EncryptionData ValidateEncryptionParameters(EncryptionParameters param) { var responseData = new EncryptionData(); @@ -134,7 +134,7 @@ private EncryptionData ValidateEncryptionParameters(EncryptionParameters param) return responseData; } - private void NullChecks(string data, string secretKey) + private static void NullChecks(string data, string secretKey) { if (data == null || data.Length <= 0) throw new ArgumentNullException(nameof(data)); @@ -143,7 +143,7 @@ private void NullChecks(string data, string secretKey) throw new ArgumentNullException(nameof(secretKey)); } - private void AddError(EncryptionData responseData, string error) + private static void AddError(EncryptionData responseData, string error) { responseData.HasError = true; responseData.Errors.Add(error); diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index 79b733e..b50bb32 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -1,17 +1,15 @@ // See https://aka.ms/new-console-template for more information -using SafeCrypt.AESDecryption; -using SafeCrypt.AESEncryption; +using SafeCrypt.AES; 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 = await encryptor.EncryptToBase64StringAsync(dataToEncrypt, secret); +var response = await Aes.EncryptToBase64StringAsync(dataToEncrypt, secret); Console.WriteLine("............Encryption Started............"); @@ -28,8 +26,7 @@ DataToDecrypt = response.EncryptedData }; -var decryptor = new AesDecryption(); -var decryptionData = await decryptor.DecryptFromBase64StringAsync(decryptorParam); +var decryptionData = await Aes.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine("............Decryption Started............"); Console.WriteLine($"Decrypted data: { decryptionData.DecryptedData }");