Skip to content

Commit 1024fae

Browse files
committed
feat: include cipher mode in encryption and decryption, default cipher mode is set to CBC
1 parent d65378d commit 1024fae

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/Encryption/AesEncryption/Decrypting.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using SafeCrypt.AesEncryption;
22
using SafeCrypt.Helpers;
33
using SafeCrypt.Models;
4-
using SafeCrypt.src.Encryption.AesEncryption.Models;
54
using System;
5+
using System.Security.Cryptography;
66

77
namespace SafeCrypt.AESDecryption
88
{
99
public class AesDecryption : BaseAesEncryption
1010
{
11-
public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
11+
public DecryptionData DeEncryptFromHexString(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
1212
{
1313
var responseData = new DecryptionData();
1414

@@ -44,7 +44,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
4444
Data = param.DataToDecrypt.HexadecimalStringToByteArray()
4545
};
4646

47-
var response = DecryptAES(byteEncryptionParameters);
47+
var response = DecryptAES(byteEncryptionParameters, mode);
4848

4949
return new DecryptionData
5050
{
@@ -54,7 +54,7 @@ public DecryptionData DeEncryptFromHexString(DecryptionParameters param)
5454
};
5555
}
5656

57-
public DecryptionData DecryptFromBase64String(DecryptionParameters param)
57+
public DecryptionData DecryptFromBase64String(DecryptionParameters param, CipherMode mode = CipherMode.CBC)
5858
{
5959
var responseData = new DecryptionData();
6060

@@ -82,7 +82,7 @@ public DecryptionData DecryptFromBase64String(DecryptionParameters param)
8282
Data = Convert.FromBase64String(param.DataToDecrypt)
8383
};
8484

85-
var response = DecryptAES(byteDecryptionParameters);
85+
var response = DecryptAES(byteDecryptionParameters, mode);
8686

8787
return new DecryptionData
8888
{

src/Encryption/AesEncryption/Encrypting.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class AesEncryption : BaseAesEncryption
2222
/// <param name="secretKey">The secret key used for encryption.</param>
2323
/// <param name="iv">The initialization vector used for encryption.</param>
2424
/// <returns>The encrypted data as a byte array.</returns>
25-
public EncryptionData EncryptToHexString(EncryptionParameters param)
25+
public EncryptionData EncryptToHexString(EncryptionParameters param, CipherMode mode = CipherMode.CBC)
2626
{
2727
var responseData = new EncryptionData();
2828

@@ -51,7 +51,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
5151
Data = param.DataToEncrypt.ConvertToHexString().HexadecimalStringToByteArray()
5252
};
5353

54-
var response = EncryptAES(byteEncryptionParameters);
54+
var response = EncryptAES(byteEncryptionParameters, mode);
5555

5656
return new EncryptionData
5757
{
@@ -83,7 +83,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
8383
/// <exception cref="FormatException">
8484
/// Thrown if the base64secretKey is not a valid Base64-encoded string.
8585
/// </exception>
86-
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey)
86+
public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string base64secretKey, CipherMode mode = CipherMode.CBC)
8787
{
8888
// validate is base64
8989
if (!Validators.IsBase64String(base64secretKey))
@@ -103,7 +103,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
103103
Data = dataToBeEncrypted.ConvertToHexString().HexadecimalStringToByteArray()
104104
};
105105

106-
var response = EncryptAES(byteEncryptionParameters);
106+
var response = EncryptAES(byteEncryptionParameters, mode);
107107

108108
return new EncryptionData
109109
{

0 commit comments

Comments
 (0)