Skip to content
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand All @@ -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}");
Expand All @@ -80,7 +80,7 @@ using SafeCrypt.Models;

class Program
{
static void Main()
static async Task Main()
{
var dataToEncrypt = "Data to Encrypt";

Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace SafeCrypt.AesEncryption
{
Expand All @@ -23,7 +24,7 @@ public class BaseAesEncryption
/// <exception cref="Exception">
/// Thrown for general encryption-related exceptions.
/// </exception>
internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC)
internal static async Task<byte[]> EncryptAsync(ByteEncryptionParameters param, CipherMode mode = CipherMode.CBC)
{
try
{
Expand All @@ -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
Expand Down Expand Up @@ -72,7 +73,7 @@ internal static byte[] EncryptAES(ByteEncryptionParameters param, CipherMode mod
/// <exception cref="ArgumentNullException">
/// Thrown if the input encrypted data, key, or initialization vector is null.
/// </exception>
internal static byte[] DecryptAES(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC)
internal static async Task<byte[]> DecryptAsync(ByteDecryptionParameters param, CipherMode mode = CipherMode.CBC)
{
try
{
Expand All @@ -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();
}
}
Expand Down
29 changes: 25 additions & 4 deletions src/SafeCrypt.Lib/Encryption/AesEncryption/Decrypting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
/// Asynchronously decrypts data from a hexadecimal string using the specified decryption parameters and cipher mode.
/// </summary>
/// <param name="param">Decryption parameters containing secret key, IV, and data to decrypt.</param>
/// <param name="mode">Cipher mode used for decryption (default is CipherMode.CBC).</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation.
/// The task result is a <see cref="DecryptionData"/> object containing the decrypted data, IV, and secret key.
/// If decryption fails, the <see cref="DecryptionData"/> object will contain error information.
/// </returns>
public async Task<DecryptionData> DecryptFromHexStringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
{
var responseData = new DecryptionData();

Expand Down Expand Up @@ -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
{
Expand All @@ -54,7 +65,17 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherM
};
}

public DecryptionData DecryptFromBase64String(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
/// <summary>
/// Asynchronously decrypts data from a Base64-encoded string using the specified decryption parameters and cipher mode.
/// </summary>
/// <param name="param">Decryption parameters containing secret key, IV, and data to decrypt.</param>
/// <param name="mode">Cipher mode used for decryption (default is CipherMode.CBC).</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation.
/// The task result is a <see cref="DecryptionData"/> object containing the decrypted data, IV, and secret key.
/// If decryption fails, the <see cref="DecryptionData"/> object will contain error information.
/// </returns>
public async Task<DecryptionData> DecryptFromBase64StringAsync(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
{
var responseData = new DecryptionData();

Expand Down Expand Up @@ -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
{
Expand Down
13 changes: 7 additions & 6 deletions src/SafeCrypt.Lib/Encryption/AesEncryption/Encrypting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Security.Cryptography;
using System.Threading.Tasks;
using SafeCrypt.AesEncryption;
using SafeCrypt.Helpers;
using SafeCrypt.Models;
Expand All @@ -9,7 +10,7 @@ namespace SafeCrypt.AESEncryption
public class AesEncryption : BaseAesEncryption
{
/// <summary>
/// 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).
/// </summary>
/// <param name="parameters">The encryption parameters.</param>
/// <returns>The encrypted data as a byte array.</returns>
Expand All @@ -22,7 +23,7 @@ public class AesEncryption : BaseAesEncryption
/// <param name="secretKey">The secret key used for encryption.</param>
/// <param name="iv">The initialization vector used for encryption.</param>
/// <returns>The encrypted data as a byte array.</returns>
public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC)
public async Task<EncryptionData> EncryptToHexStringAsync(EncryptionParameters param, CipherMode mode = CipherMode.CBC)
{
var responseData = new EncryptionData();

Expand Down Expand Up @@ -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
{
Expand All @@ -62,7 +63,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode
}

/// <summary>
/// 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.
/// </summary>
/// <param name="dataToBeEncrypted">The string data to be encrypted.</param>
/// <param name="base64secretKey">The Base64-encoded secret key used for encryption.</param>
Expand All @@ -83,7 +84,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode
/// <exception cref="FormatException">
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
/// </exception>
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC)
public async Task<EncryptionData> EncryptToBase64StringAsync(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC)
{
// validate is base64
if (!Validators.IsBase64String(base64secretKey))
Expand All @@ -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
{
Expand Down
12 changes: 6 additions & 6 deletions src/SafeCrypt.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }");
Expand Down