Skip to content

Commit dd20cf1

Browse files
committed
feat: add byte encryption paramter class to encapsulate the iv, secret and data
1 parent 4ac5162 commit dd20cf1

3 files changed

Lines changed: 142 additions & 32 deletions

File tree

src/Encryption/AesEncryption/BaseAesEncryption.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using SafeCrypt.src.Encryption.AesEncryption.Models;
2+
using System;
23
using System.IO;
34
using System.Security.Cryptography;
45

@@ -9,9 +10,9 @@ public class BaseAesEncryption
910
/// <summary>
1011
/// Encrypts the provided data using the Advanced Encryption Standard (AES) algorithm.
1112
/// </summary>
12-
/// <param name="data">The data to be encrypted.</param>
13-
/// <param name="key">The secret key used for encryption.</param>
14-
/// <param name="iv">The initialization vector used for encryption.</param>
13+
/// <param name="param.Data">The data to be encrypted.</param>
14+
/// <param name="param.SecretKey">The secret key used for encryption.</param>
15+
/// <param name="param.IV">The initialization vector used for encryption.</param>
1516
/// <returns>The encrypted data as a byte array.</returns>
1617
/// <remarks>
1718
/// The method uses the AES algorithm to encrypt the input data with the provided key and initialization vector.
@@ -22,16 +23,16 @@ public class BaseAesEncryption
2223
/// <exception cref="Exception">
2324
/// Thrown for general encryption-related exceptions.
2425
/// </exception>
25-
public virtual byte[] EncryptAES(byte[] data, byte[] key, byte[] iv)
26+
public virtual byte[] EncryptAES(ByteEncryptionParameters param)
2627
{
2728
try
2829
{
2930
// Create an instance of the AES algorithm
3031
using (Aes aes = Aes.Create())
3132
{
3233
// Set the key and initialization vector
33-
aes.Key = key;
34-
aes.IV = iv;
34+
aes.Key = param.SecretKey;
35+
aes.IV = param.IV;
3536
// Create an encryptor using the key and initialization vector
3637
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
3738

@@ -42,7 +43,7 @@ public virtual byte[] EncryptAES(byte[] data, byte[] key, byte[] iv)
4243
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
4344
{
4445
// Write the data to be encrypted to the CryptoStream
45-
cryptoStream.Write(data, 0, data.Length);
46+
cryptoStream.Write(param.Data, 0, param.Data.Length);
4647
cryptoStream.FlushFinalBlock();
4748

4849
// Return the encrypted data as a byte array

src/Encryption/AesEncryption/Encrypting.cs

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ public class Encrypting : BaseAesEncryption
1010
/// <summary>
1111
/// Encrypts the provided data using the specified secret key and initialization vector (IV).
1212
/// </summary>
13-
/// <param name="data">The data to be encrypted.</param>
14-
/// <param name="secretKey">The secret key used for encryption.</param>
15-
/// <param name="iv">The initialization vector used for encryption.</param>
13+
/// <param name="parameters">The encryption parameters.</param>
1614
/// <returns>The encrypted data as a byte array.</returns>
1715
/// <remarks>
1816
/// This method serves as a wrapper around the underlying AES encryption logic provided by the
@@ -23,62 +21,138 @@ public class Encrypting : BaseAesEncryption
2321
/// <param name="secretKey">The secret key used for encryption.</param>
2422
/// <param name="iv">The initialization vector used for encryption.</param>
2523
/// <returns>The encrypted data as a byte array.</returns>
26-
public byte[] Encrypt(byte[] data, byte[] secretKey, byte[] iv)
24+
public byte[] Encrypt(ByteEncryptionParameters param)
2725
{
2826
// Delegate the encryption to the underlying AES encryption method
29-
return EncryptAES(data, secretKey, iv);
27+
return EncryptAES(param);
3028
}
3129

3230

33-
public byte[] Encrypt(string data, string secretKey, string iv)
31+
public byte[] Encrypt(StringEncryptionParameters param)
3432
{
35-
NullChecks(data, secretKey, iv);
33+
NullChecks(param.Data, param.SecretKey, param.IV);
3634

37-
var aesKey = secretKey.ConvertKeysToBytes();
38-
var aesIv = iv.ConvertKeysToBytes();
35+
var byteEncryptionParameters = new ByteEncryptionParameters
36+
{
37+
SecretKey = param.SecretKey.ConvertKeysToBytes(),
38+
IV = param.IV.ConvertKeysToBytes(),
39+
Data = param.Data.HexadecimalStringToByteArray()
40+
};
3941

40-
var aesData = data.HexadecimalStringToByteArray();
41-
return EncryptAES(aesData, aesKey, aesIv);
42-
}
42+
return EncryptAES(byteEncryptionParameters);
43+
}
4344

44-
public AesEncryptionData Encrypt(string data, string secretKey)
45+
/// <summary>
46+
/// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
47+
/// </summary>
48+
/// <param name="data">The string data to be encrypted.</param>
49+
/// <param name="secretKey">The secret key used for encryption.</param>
50+
/// <returns>An <see cref="AesEncrypted"/> object containing the encrypted data and initialization vector (IV).</returns>
51+
/// <remarks>
52+
/// This method encrypts the input string data using the specified secret key and a randomly generated initialization
53+
/// vector (IV) for AES encryption in Cipher Block Chaining (CBC) mode. The resulting encrypted data and IV are encapsulated
54+
/// in an <see cref="AesEncrypted"/> object, which is then returned.
55+
/// </remarks>
56+
/// <param name="data">The string data to be encrypted.</param>
57+
/// <param name="secretKey">The secret key used for encryption.</param>
58+
/// <returns>An <see cref="AesEncrypted"/> object containing the encrypted data and initialization vector (IV).</returns>
59+
/// <exception cref="ArgumentNullException">
60+
/// Thrown if the input data or secret key is null.
61+
/// </exception>
62+
public AesEncrypted Encrypt(string data, string secretKey)
4563
{
4664
NullChecks(data, secretKey);
4765

4866
// Generate a random 16-byte IV for AES in CBC mode
4967
var aesIv = GenerateRandomIVKeyAsBytes(16);
5068

51-
var aesKey = Encoding.UTF8.GetBytes(secretKey);
52-
var aesData = data.HexadecimalStringToByteArray();
69+
//var aesKey = Encoding.UTF8.GetBytes(secretKey);
70+
//var aesData = data.HexadecimalStringToByteArray();
71+
72+
var byteEncryptionParameters = new ByteEncryptionParameters
73+
{
74+
SecretKey = Encoding.UTF8.GetBytes(secretKey),
75+
IV = aesIv,
76+
Data = data.HexadecimalStringToByteArray()
77+
};
5378

54-
var response = EncryptAES(aesData, aesKey, aesIv);
79+
//var response = EncryptAES(aesData, aesKey, aesIv);
80+
var response = EncryptAES(byteEncryptionParameters);
5581

56-
var responseData = new AesEncryptionData
82+
var responseData = new AesEncrypted
5783
{
5884
Data = response,
5985
Iv = aesIv
6086
};
6187

6288
return responseData;
6389
}
64-
65-
public string EncryptByteToHexString(byte[] data, byte[] secretKey, byte[] iv)
90+
/// <summary>
91+
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
92+
/// and returns the encrypted data as a hexadecimal string.
93+
/// </summary>
94+
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
95+
/// <returns>The encrypted data represented as a hexadecimal string.</returns>
96+
/// <remarks>
97+
/// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
98+
/// using the AES algorithm. The resulting encrypted data is then converted to a hexadecimal string before being
99+
/// returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
100+
/// </remarks>
101+
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
102+
/// <returns>The encrypted data represented as a hexadecimal string.</returns>
103+
/// <exception cref="ArgumentNullException">
104+
/// Thrown if the input parameters or byte data is null.
105+
/// </exception>
106+
public string EncryptByteToHexString(ByteEncryptionParameters param)
66107
{
67-
var cipherText = EncryptAES(data, secretKey, iv);
108+
var cipherText = EncryptAES(param);
68109

110+
// Convert the encrypted data to a hexadecimal string
69111
return cipherText.ByteArrayToHexString();
70112
}
71113

72-
public string EncryptByteToBase64String(byte[] data, byte[] secretKey, byte[] iv)
114+
/// <summary>
115+
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
116+
/// and returns the encrypted data as a Base64-encoded string.
117+
/// </summary>
118+
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
119+
/// <returns>The encrypted data represented as a Base64-encoded string.</returns>
120+
/// <remarks>
121+
/// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
122+
/// using the AES algorithm. The resulting encrypted data is then converted to a Base64-encoded string before
123+
/// being returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
124+
/// </remarks>
125+
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
126+
/// <returns>The encrypted data represented as a Base64-encoded string.</returns>
127+
/// <exception cref="ArgumentNullException">
128+
/// Thrown if the input parameters or byte data is null.
129+
/// </exception>
130+
public string EncryptByteToBase64String(ByteEncryptionParameters param)
73131
{
74-
var cipherText = EncryptAES(data, secretKey, iv);
132+
var cipherText = EncryptAES(param);
75133

76134
return Convert.ToBase64String(cipherText);
77135
}
78136

79-
public string EncryptByteToString(byte[] data, byte[] secretKey, byte[] iv)
137+
/// <summary>
138+
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
139+
/// and returns the encrypted data as a string using UTF-8 encoding.
140+
/// </summary>
141+
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
142+
/// <returns>The encrypted data represented as a string using UTF-8 encoding.</returns>
143+
/// <remarks>
144+
/// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
145+
/// using the AES algorithm. The resulting encrypted data is then converted to a string using UTF-8
146+
/// encoding before being returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
147+
/// </remarks>
148+
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
149+
/// <returns>The encrypted data represented as a string using UTF-8 encoding.</returns>
150+
/// <exception cref="ArgumentNullException">
151+
/// Thrown if the input parameters or byte data is null.
152+
/// </exception>
153+
public string EncryptByteToString(ByteEncryptionParameters param)
80154
{
81-
var cipherText = EncryptAES(data, secretKey, iv);
155+
var cipherText = EncryptAES(param);
82156

83157
return cipherText.BytesToString();
84158
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Text;
5+
6+
namespace SafeCrypt.src.Encryption.AesEncryption.Models
7+
{
8+
/// <summary>
9+
/// Represents the parameters required for encryption.
10+
/// </summary>
11+
public class ByteEncryptionParameters
12+
{
13+
/// <summary>
14+
/// Gets or sets the data to be encrypted.
15+
/// </summary>
16+
[Required]
17+
public byte[] Data { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the secret key used for encryption.
21+
/// </summary>
22+
[Required]
23+
public byte[] SecretKey { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the initialization vector (IV) used for encryption.
27+
/// </summary>
28+
[Required]
29+
public byte[] IV { get; set; }
30+
}
31+
32+
33+
34+
35+
}

0 commit comments

Comments
 (0)