From 374a754ed6fd0b814a07d7e51dce10007a443208 Mon Sep 17 00:00:00 2001 From: Oluwasegun Akinpelu Date: Mon, 8 Jan 2024 13:39:10 +0100 Subject: [PATCH 1/6] Implemented Asynchroonous methods and also updated the readme to account for the implementation --- README.md | 10 +++++----- src/Encryption/AesEncryption/BaseAesEncryption.cs | 9 +++++---- src/Encryption/AesEncryption/Decrypting.cs | 9 +++++---- src/Encryption/AesEncryption/Encrypting.cs | 9 +++++---- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 083efc5..72329ec 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ using SafeCrypt.Models; class Program { - static void Main() + static async Task Main() { var aesEncryptor = new AesEncryption(); @@ -64,7 +64,7 @@ class Program }; - var data = aesDecryptor.DecryptFromBase64String(parameterToDecrypt) + var data = await aesDecryptor.DecryptFromBase64String(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.EncryptToBase64String(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.DecryptFromBase64String(decryptorParam); Console.WriteLine(decryptionData.DecryptedData); Console.WriteLine(decryptionData.Iv); diff --git a/src/Encryption/AesEncryption/BaseAesEncryption.cs b/src/Encryption/AesEncryption/BaseAesEncryption.cs index 9a35ca9..897ef71 100644 --- a/src/Encryption/AesEncryption/BaseAesEncryption.cs +++ b/src/Encryption/AesEncryption/BaseAesEncryption.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Security.Cryptography; using System.Text; +using System.Threading.Tasks; namespace SafeCrypt.AesEncryption { @@ -25,7 +26,7 @@ public class BaseAesEncryption /// /// Thrown for general encryption-related exceptions. /// - internal static byte[] EncryptAES(ByteEncryptionParameters param) + internal static async Task EncryptAES(ByteEncryptionParameters param) { try { @@ -45,7 +46,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param) 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 @@ -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 async Task DecryptAES(ByteDecryptionParameters param) { try { @@ -97,7 +98,7 @@ internal static byte[] DecryptAES(ByteDecryptionParameters param) 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/Encryption/AesEncryption/Decrypting.cs b/src/Encryption/AesEncryption/Decrypting.cs index 4fe642b..2a20390 100644 --- a/src/Encryption/AesEncryption/Decrypting.cs +++ b/src/Encryption/AesEncryption/Decrypting.cs @@ -2,12 +2,13 @@ using SafeCrypt.Helpers; using SafeCrypt.Models; using System; +using System.Threading.Tasks; namespace SafeCrypt.AESDecryption { public class AesDecryption : BaseAesEncryption { - public DecryptionData DeEncryptFromHexString(DecryptionParameters param) + public async Task DeEncryptFromHexString(DecryptionParameters param) { var responseData = new DecryptionData(); @@ -43,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param) Data = param.DataToDecrypt.HexadecimalStringToByteArray() }; - var response = DecryptAES(byteEncryptionParameters); + var response = await DecryptAES(byteEncryptionParameters); return new DecryptionData { @@ -53,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param) }; } - public DecryptionData DecryptFromBase64String(DecryptionParameters param) + public async Task DecryptFromBase64String(DecryptionParameters param) { var responseData = new DecryptionData(); @@ -81,7 +82,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param) Data = Convert.FromBase64String(param.DataToDecrypt) }; - var response = DecryptAES(byteDecryptionParameters); + var response = await DecryptAES(byteDecryptionParameters); return new DecryptionData { diff --git a/src/Encryption/AesEncryption/Encrypting.cs b/src/Encryption/AesEncryption/Encrypting.cs index 3f94479..39efa66 100644 --- a/src/Encryption/AesEncryption/Encrypting.cs +++ b/src/Encryption/AesEncryption/Encrypting.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using SafeCrypt.AesEncryption; using SafeCrypt.Helpers; using SafeCrypt.Models; @@ -21,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 async Task EncryptToHexString(EncryptionParameters param) { var responseData = new EncryptionData(); @@ -57,7 +58,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param) Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = EncryptAES(byteEncryptionParameters); + var response = await EncryptAES(byteEncryptionParameters); return new EncryptionData { @@ -90,7 +91,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 async Task EncryptToBase64String(string dataToBeEncrypted, string base64secretKey) { // validate is base64 if (!Validators.IsBase64String(base64secretKey)) @@ -110,7 +111,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray() }; - var response = EncryptAES(byteEncryptionParameters); + var response = await EncryptAES(byteEncryptionParameters); return new EncryptionData { From 758507d446497b4ee390ea993505ecc3511955b3 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 21 Jan 2024 16:29:59 +0100 Subject: [PATCH 2/6] feat: convert base aes encryption logic to run async --- .../Encryption/AesEncryption/BaseAesEncryption.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs b/src/SafeCrypt.Lib/Encryption/AesEncryption/BaseAesEncryption.cs index e6923f0..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 { @@ -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 { From c09249074cb4daddc221180ebe90460980fca7e1 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 21 Jan 2024 16:30:37 +0100 Subject: [PATCH 3/6] feat: update decryption logic to run async --- .../Encryption/AesEncryption/Decrypting.cs | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) 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 { From d546978d34fdfb7602d78462b2a95a6417abdcc3 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 21 Jan 2024 16:30:51 +0100 Subject: [PATCH 4/6] feat: update encryption logic to run async --- .../Encryption/AesEncryption/Encrypting.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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 { From 83ff1ca389219cc7b1589ac0cf1a7a876ee956fd Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 21 Jan 2024 16:31:49 +0100 Subject: [PATCH 5/6] feat: update console logic to run async algorithm --- src/SafeCrypt.Test/Program.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 }"); From 7e73a80dad473ae23a59eb878b8a2c3932fd3208 Mon Sep 17 00:00:00 2001 From: Raphael Anyanwu Date: Sun, 21 Jan 2024 16:32:06 +0100 Subject: [PATCH 6/6] doc: update readme doc --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 72329ec..6965cd2 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ class Program { 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 = await aesDecryptor.DecryptFromBase64String(parameterToDecrypt) + var data = await aesDecryptor.DecryptFromBase64StringAsync(parameterToDecrypt) Console.WriteLine($"Decrypted Data: {data.DecryptedData}"); Console.WriteLine($"Initialization Vector: {data.Iv}"); @@ -96,7 +96,7 @@ class Program var encryptor = new AesEncryption(); - var response = await 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 = await decryptor.DecryptFromBase64String(decryptorParam); + var decryptionData = await decryptor.DecryptFromBase64StringAsync(decryptorParam); Console.WriteLine(decryptionData.DecryptedData); Console.WriteLine(decryptionData.Iv);