Skip to content

Commit cf060b0

Browse files
committed
refactor: verify encryption algorithm
1 parent ab54c4c commit cf060b0

1 file changed

Lines changed: 46 additions & 121 deletions

File tree

src/Encryption/AesEncryption/Encrypting.cs

Lines changed: 46 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SafeCrypt.src.Encryption.AesEncryption.Models;
55
using SafeCrypt.AesEncryption;
66
using System.ComponentModel.DataAnnotations;
7+
using System.Security.Cryptography;
78

89
namespace SafeCrypt.Encrypt
910
{
@@ -23,35 +24,53 @@ public class Encrypting : BaseAesEncryption
2324
/// <param name="secretKey">The secret key used for encryption.</param>
2425
/// <param name="iv">The initialization vector used for encryption.</param>
2526
/// <returns>The encrypted data as a byte array.</returns>
26-
public byte[] Encrypt(EncryptionParameters param)
27+
public EncryptionData EncryptToHexString(EncryptionParameters param)
2728
{
29+
var responseData = new EncryptionData();
30+
2831
Validators.ValidateNotNull(param);
2932

33+
// validate is base64
34+
if (!Validators.IsBase64String(param.SecretKey))
35+
{
36+
AddError(responseData, $"SecretKey: {param.SecretKey} is not a base64 string");
37+
return responseData;
38+
}
39+
40+
if (!Validators.IsBase64String(param.IV))
41+
{
42+
AddError(responseData, $"IV: {param.IV} is not a base64 string");
43+
return responseData;
44+
}
45+
// Convert input string to bytes
46+
byte[] dataBytes = param.IV.ConvertKeysToBytes();
47+
48+
// Validate block size based on AES algorithm's requirements
49+
if (!Validators.IsValidBlockSize(dataBytes.Length))
50+
{
51+
AddError(responseData, $"IV: {param.IV} is not a valid block size for this algorithm");
52+
return responseData;
53+
}
54+
3055
// Delegate the encryption to the underlying AES encryption method
3156
var byteEncryptionParameters = new ByteEncryptionParameters
3257
{
33-
SecretKey = param.SecretKey.ConvertKeysToBytes(),
34-
IV = param.IV.ConvertKeysToBytes(),
35-
Data = param.DataToEncrypt.HexadecimalStringToByteArray()
58+
SecretKey = Convert.FromBase64String(param.SecretKey),
59+
IV = dataBytes,
60+
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
3661
};
37-
return EncryptAES(byteEncryptionParameters);
38-
}
3962

40-
41-
public byte[] Encrypt(StringEncryptionParameters param)
42-
{
43-
Validators.ValidateNotNull(param);
63+
var response = EncryptAES(byteEncryptionParameters);
4464

45-
var byteEncryptionParameters = new ByteEncryptionParameters
65+
return new EncryptionData
4666
{
47-
SecretKey = param.SecretKey.ConvertKeysToBytes(),
48-
IV = param.IV.ConvertKeysToBytes(),
49-
Data = param.Data.HexadecimalStringToByteArray()
67+
EncryptedData = response.ByteArrayToHexString(),
68+
Iv = param.IV,
69+
SecretKey = param.SecretKey
5070
};
51-
52-
return EncryptAES(byteEncryptionParameters);
5371
}
54-
72+
73+
5574
/// <summary>
5675
/// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
5776
/// </summary>
@@ -85,7 +104,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
85104
NullChecks(data: dataToBeEncrypted, base64secretKey);
86105

87106
// Generate a random 16-byte IV for AES in CBC mode
88-
var aesIv = GenerateRandomIVKeyAsBytes(16);
107+
var aesIv = KeyGenerators.GenerateRandomIVKeyAsBytes(16);
89108

90109
var byteEncryptionParameters = new ByteEncryptionParameters
91110
{
@@ -96,112 +115,13 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
96115

97116
var response = EncryptAES(byteEncryptionParameters);
98117

99-
var responseData = new EncryptionData
118+
return new EncryptionData
100119
{
101120
EncryptedData = Convert.ToBase64String(response),
102-
Iv = Convert.ToBase64String(aesIv)
103-
};
104-
105-
return responseData;
106-
}
107-
108-
/// <summary>
109-
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
110-
/// and returns the encrypted data as a hexadecimal string.
111-
/// </summary>
112-
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
113-
/// <returns>The encrypted data represented as a hexadecimal string.</returns>
114-
/// <remarks>
115-
/// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
116-
/// using the AES algorithm. The resulting encrypted data is then converted to a hexadecimal string before being
117-
/// returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
118-
/// </remarks>
119-
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
120-
/// <returns>The encrypted data represented as a hexadecimal string.</returns>
121-
/// <exception cref="ArgumentNullException">
122-
/// Thrown if the input parameters or byte data is null.
123-
/// </exception>
124-
public string EncryptByteToHexString(EncryptionParameters param)
125-
{
126-
Validators.ValidateNotNull(param);
127-
128-
var byteEncryptionParameters = new ByteEncryptionParameters
129-
{
130-
SecretKey = param.SecretKey.ConvertKeysToBytes(),
131-
IV = param.IV.ConvertKeysToBytes(),
132-
Data = param.DataToEncrypt.HexadecimalStringToByteArray()
133-
};
134-
135-
var cipherText = EncryptAES(byteEncryptionParameters);
136-
137-
// Convert the encrypted data to a hexadecimal string
138-
return cipherText.ByteArrayToHexString();
139-
}
140-
141-
/// <summary>
142-
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
143-
/// and returns the encrypted data as a Base64-encoded string.
144-
/// </summary>
145-
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
146-
/// <returns>The encrypted data represented as a Base64-encoded string.</returns>
147-
/// <remarks>
148-
/// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
149-
/// using the AES algorithm. The resulting encrypted data is then converted to a Base64-encoded string before
150-
/// being returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
151-
/// </remarks>
152-
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
153-
/// <returns>The encrypted data represented as a Base64-encoded string.</returns>
154-
/// <exception cref="ArgumentNullException">
155-
/// Thrown if the input parameters or byte data is null.
156-
/// </exception>
157-
public string EncryptByteToBase64String(EncryptionParameters param)
158-
{
159-
Validators.ValidateNotNull(param);
160-
161-
var byteEncryptionParameters = new ByteEncryptionParameters
162-
{
163-
SecretKey = param.SecretKey.ConvertKeysToBytes(),
164-
IV = param.IV.ConvertKeysToBytes(),
165-
Data = param.DataToEncrypt.HexadecimalStringToByteArray()
166-
};
167-
168-
var cipherText = EncryptAES(byteEncryptionParameters);
169-
170-
return Convert.ToBase64String(cipherText);
171-
}
172-
173-
/// <summary>
174-
/// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
175-
/// and returns the encrypted data as a string using UTF-8 encoding.
176-
/// </summary>
177-
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
178-
/// <returns>The encrypted data represented as a string using UTF-8 encoding.</returns>
179-
/// <remarks>
180-
/// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
181-
/// using the AES algorithm. The resulting encrypted data is then converted to a string using UTF-8
182-
/// encoding before being returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
183-
/// </remarks>
184-
/// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
185-
/// <returns>The encrypted data represented as a string using UTF-8 encoding.</returns>
186-
/// <exception cref="ArgumentNullException">
187-
/// Thrown if the input parameters or byte data is null.
188-
/// </exception>
189-
public string EncryptByteToString(EncryptionParameters param)
190-
{
191-
Validators.ValidateNotNull(param);
192-
193-
var byteEncryptionParameters = new ByteEncryptionParameters
194-
{
195-
SecretKey = param.SecretKey.ConvertKeysToBytes(),
196-
IV = param.IV.ConvertKeysToBytes(),
197-
Data = param.DataToEncrypt.HexadecimalStringToByteArray()
121+
Iv = Convert.ToBase64String(aesIv),
122+
SecretKey = base64secretKey
198123
};
199-
200-
var cipherText = EncryptAES(byteEncryptionParameters);
201-
202-
return cipherText.BytesToString();
203124
}
204-
205125

206126
private void NullChecks(string data, string secretKey)
207127
{
@@ -211,6 +131,11 @@ private void NullChecks(string data, string secretKey)
211131
if (secretKey == null )
212132
throw new ArgumentNullException(nameof(secretKey));
213133
}
214-
}
215134

135+
private void AddError(EncryptionData responseData, string error)
136+
{
137+
responseData.HasError = true;
138+
responseData.Errors.Add(error);
139+
}
140+
}
216141
}

0 commit comments

Comments
 (0)