diff --git a/README.md b/README.md index 083efc5..6965cd2 100644 --- a/README.md +++ b/README.md @@ -45,11 +45,11 @@ using SafeCrypt.Models; class Program { - static void Main() + static async Task Main() { var aesEncryptor = new AesEncryption(); - var encryptedData = aesEncryptor.EncryptToBase64String("Hello, World!", "gdjdtsraewsuteastwerse==" + var encryptedData = await aesEncryptor.EncryptToBase64StringAsync("Hello, World!", "gdjdtsraewsuteastwerse==" Console.WriteLine($"Encrypted Data: {encryptedData.EncryptedData}"); Console.WriteLine($"Initialization Vector: {encryptedData.Iv}"); @@ -64,7 +64,7 @@ class Program }; - var data = aesDecryptor.DecryptFromBase64String(parameterToDecrypt) + var data = await aesDecryptor.DecryptFromBase64StringAsync(parameterToDecrypt) Console.WriteLine($"Decrypted Data: {data.DecryptedData}"); Console.WriteLine($"Initialization Vector: {data.Iv}"); @@ -80,7 +80,7 @@ using SafeCrypt.Models; class Program { - static void Main() + static async Task Main() { var dataToEncrypt = "Data to Encrypt"; @@ -96,7 +96,7 @@ class Program var encryptor = new AesEncryption(); - var response = encryptor.EncryptToBase64String(encryptionParam.DataToEncrypt, secret); + var response = await encryptor.EncryptToBase64StringAsync(encryptionParam.DataToEncrypt, secret); Console.WriteLine(response.EncryptedData); Console.WriteLine(response.Iv); @@ -113,7 +113,7 @@ class Program var decryptor = new AesDecryption(); - var decryptionData = decryptor.DecryptFromBase64String(decryptorParam); + var decryptionData = await decryptor.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 b9febab..e4d1da7 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Security.Cryptography; +using System.Threading.Tasks; namespace SafeCrypt.AesEncryption { @@ -23,7 +24,7 @@ public class BaseAesEncryption /// /// Thrown for general encryption-related exceptions. /// - internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC) + internal static async Task EncryptAsync(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC) { try { @@ -44,7 +45,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mod using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { // Write the data to be encrypted to the CryptoStream - cryptoStream.Write(param.Data, 0, param.Data.Length); + await cryptoStream.WriteAsync(param.Data, 0, param.Data.Length); cryptoStream.FlushFinalBlock(); // Return the encrypted data as a byte array @@ -72,7 +73,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mod /// /// Thrown if the input encrypted data, key, or initialization vector is null. /// - internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC) + internal static async Task DecryptAsync(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC) { try { @@ -97,7 +98,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mod using (MemoryStream decryptedStream = new MemoryStream()) { // Copy the decrypted data from the CryptoStream to the MemoryStream - cryptoStream.CopyTo(decryptedStream); + await cryptoStream.CopyToAsync(decryptedStream); return decryptedStream.ToArray(); } } diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs index eda6bbb..ae42d21 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs @@ -3,12 +3,23 @@ using SafeCrypt.Models; using System; using System.Security.Cryptography; +using System.Threading.Tasks; namespace SafeCrypt.AESDecryption { public class AesDecryption : BaseAesEncryption { - public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherMode mode = CipherMode.CBC) + /// + /// Asynchronously decrypts data from a hexadecimal string using the specified decryption parameters and cipher mode. + /// + /// Decryption parameters containing secret key, IV, and data to decrypt. + /// Cipher mode used for decryption (default is CipherMode.CBC). + /// + /// A representing the asynchronous operation. + /// 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) { var responseData = new DecryptionData(); @@ -44,7 +55,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherM Data = param.DataToDecrypt.HexadecimalStringToByteArray() }; - var response = DecryptAES(byteEncryptionParameters, mode); + var response = await DecryptAsync(byteEncryptionParameters, mode); return new DecryptionData { @@ -54,7 +65,17 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherM }; } - public DecryptionData DecryptFromBase64String(DecryptionParameters param, CipherMode mode = CipherMode.CBC) + /// + /// Asynchronously decrypts data from a Base64-encoded string using the specified decryption parameters and cipher mode. + /// + /// Decryption parameters containing secret key, IV, and data to decrypt. + /// Cipher mode used for decryption (default is CipherMode.CBC). + /// + /// A representing the asynchronous operation. + /// 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) { var responseData = new DecryptionData(); @@ -82,7 +103,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param, Cipher Data = Convert.FromBase64String(param.DataToDecrypt) }; - var response = DecryptAES(byteDecryptionParameters, mode); + var response = await DecryptAsync(byteDecryptionParameters, mode); return new DecryptionData { diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs index 10900ad..ca76d78 100644 --- a/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs +++ b/src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs @@ -1,5 +1,6 @@ using System; using System.Security.Cryptography; +using System.Threading.Tasks; using SafeCrypt.AesEncryption; using SafeCrypt.Helpers; using SafeCrypt.Models; @@ -9,7 +10,7 @@ namespace SafeCrypt.AESEncryption public class AesEncryption : BaseAesEncryption { /// - /// Encrypts the provided data using the specified secret key and initialization vector (IV). + /// Asynchronously encrypts the provided data using the specified secret key and initialization vector (IV). /// /// The encryption parameters. /// The encrypted data as a byte array. @@ -22,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 EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC) + public async Task EncryptToHexStringAsync(EncryptionParameters param, CipherMode mode = CipherMode.CBC) { var responseData = new EncryptionData(); @@ -51,7 +52,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = EncryptAES(byteEncryptionParameters, mode); + var response = await EncryptAsync(byteEncryptionParameters, mode); return new EncryptionData { @@ -62,7 +63,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode } /// - /// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm. + /// Asynchronously encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm. /// /// The string data to be encrypted. /// The Base64-encoded secret key used for encryption. @@ -83,7 +84,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode /// /// Thrown if the base64secretKey is not a valid Base64-encoded string. /// - public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC) + public async Task EncryptToBase64StringAsync(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC) { // validate is base64 if (!Validators.IsBase64String(base64secretKey)) @@ -103,7 +104,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = EncryptAES(byteEncryptionParameters, mode); + var response = await EncryptAsync(byteEncryptionParameters, mode); return new EncryptionData { diff --git a/src/SafeCrypt.Test/Program.cs b/src/SafeCrypt.Test/Program.cs index 6df6ccb..79b733e 100644 --- a/src/SafeCrypt.Test/Program.cs +++ b/src/SafeCrypt.Test/Program.cs @@ -11,13 +11,13 @@ 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); +var response = await encryptor.EncryptToBase64StringAsync(dataToEncrypt, secret); -//Console.WriteLine("............Encryption Started............"); +Console.WriteLine("............Encryption Started............"); -//Console.WriteLine($"Encrypted data: {response.EncryptedData}"); -//Console.WriteLine($"IV key: {response.Iv}"); -//Console.WriteLine($"Secret key: {response.SecretKey}"); +Console.WriteLine($"Encrypted data: {response.EncryptedData}"); +Console.WriteLine($"IV key: {response.Iv}"); +Console.WriteLine($"Secret key: {response.SecretKey}"); // Decryption process @@ -29,7 +29,7 @@ }; var decryptor = new AesDecryption(); -var decryptionData = decryptor.DecryptFromBase64String(decryptorParam); +var decryptionData = await decryptor.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine("............Decryption Started............"); Console.WriteLine($"Decrypted data: { decryptionData.DecryptedData }");