11using SafeCrypt . AesEncryption ;
22using SafeCrypt . Helpers ;
3+ using SafeCrypt . Models ;
4+ using SafeCrypt . src . Encryption . AesEncryption . Models ;
35using System ;
46
5- namespace SafeCrypt . Decrypt
7+ namespace SafeCrypt . AESDecryption
68{
7- public class Decrypting : BaseAesEncryption
9+ public class AesDecryption : BaseAesEncryption
810 {
9- public byte [ ] AesDecrypt ( string data , string secretKey , string iv )
11+ public DecryptionData DeEncryptFromHexString ( DecryptionParameters param )
1012 {
11- NullChecks ( data , secretKey , iv ) ;
13+ var responseData = new DecryptionData ( ) ;
1214
13- var ( aesKey , aesIv ) = ConvertKeysToBytesAndGetKeys ( secretKey , iv ) ;
15+ Validators . ValidateNotNull ( param ) ;
1416
15- var aesData = data . HexadecimalStringToByteArray ( ) ;
16- return DecryptAES ( aesData , aesKey , aesIv ) ;
17+ // validate is base64
18+ if ( ! Validators . IsBase64String ( param . SecretKey ) )
19+ {
20+ AddError ( responseData , $ "SecretKey: { param . SecretKey } is not a base64 string") ;
21+ return responseData ;
22+ }
23+
24+ if ( ! Validators . IsBase64String ( param . IV ) )
25+ {
26+ AddError ( responseData , $ "IV: { param . IV } is not a base64 string") ;
27+ return responseData ;
28+ }
29+ // Convert input string to bytes
30+ byte [ ] dataBytes = param . IV . ConvertKeysToBytes ( ) ;
31+
32+ // Validate block size based on AES algorithm's requirements
33+ if ( ! Validators . IsValidBlockSize ( dataBytes . Length ) )
34+ {
35+ AddError ( responseData , $ "IV: { param . IV } is not a valid block size for this algorithm") ;
36+ return responseData ;
37+ }
38+
39+ // Delegate the encryption to the underlying AES encryption method
40+ var byteEncryptionParameters = new ByteDecryptionParameters
41+ {
42+ SecretKey = Convert . FromBase64String ( param . SecretKey ) ,
43+ IV = dataBytes ,
44+ Data = param . DataToDecrypt . HexadecimalStringToByteArray ( )
45+ } ;
46+
47+ var response = DecryptAES ( byteEncryptionParameters ) ;
48+
49+ return new DecryptionData
50+ {
51+ DecryptedData = response . BytesToString ( ) ,
52+ Iv = param . IV ,
53+ SecretKey = param . SecretKey
54+ } ;
1755 }
1856
19- public byte [ ] Decrypt ( byte [ ] data , byte [ ] secretKey , byte [ ] iv )
20- => DecryptAES ( data , secretKey , iv ) ;
57+ public DecryptionData DecryptFromBase64String ( DecryptionParameters param )
58+ {
59+ var responseData = new DecryptionData ( ) ;
60+
61+ Validators . ValidateNotNull ( param ) ;
62+
63+
64+ if ( ! Validators . IsBase64String ( param . SecretKey ) )
65+ {
66+ AddError ( responseData , $ "SecretKey: { param . SecretKey } is not a base64 string") ;
67+ return responseData ;
68+ }
69+
70+ if ( ! Validators . IsBase64String ( param . IV ) )
71+ {
72+ AddError ( responseData , $ "IV: { param . IV } is not a base64 string") ;
73+ return responseData ;
74+ }
75+
76+ try
77+ {
78+ var byteDecryptionParameters = new ByteDecryptionParameters
79+ {
80+ SecretKey = Convert . FromBase64String ( param . SecretKey ) ,
81+ IV = Convert . FromBase64String ( param . IV ) ,
82+ Data = Convert . FromBase64String ( param . DataToDecrypt )
83+ } ;
84+
85+ var response = DecryptAES ( byteDecryptionParameters ) ;
86+
87+ return new DecryptionData
88+ {
89+ DecryptedData = response . BytesToString ( ) ,
90+ Iv = param . IV ,
91+ SecretKey = param . SecretKey
92+ } ;
93+ }
94+ catch ( Exception ex )
95+ {
96+ // Handle decryption failure or padding errors
97+ AddError ( responseData , $ "Decryption error: { ex . Message } ") ;
98+ return responseData ;
99+ }
100+ }
21101
22102
23103 private void NullChecks ( string data , string secretKey , string iv )
@@ -37,5 +117,11 @@ private void NullChecks(string data, string secretKey, string iv)
37117
38118 return ( secretKey . ConvertKeysToBytes ( ) , iv . ConvertKeysToBytes ( ) ) ;
39119 }
120+
121+ private void AddError ( DecryptionData responseData , string error )
122+ {
123+ responseData . HasError = true ;
124+ responseData . Errors . Add ( error ) ;
125+ }
40126 }
41127}
0 commit comments