11using SafeCrypt . AES ;
2+ using SafeCrypt . Helpers ;
23using SafeCrypt . Models ;
34
45namespace SafeCrypt . App . Usage ;
@@ -7,42 +8,86 @@ internal static class AesUsage
78{
89 internal static async void Execute ( )
910 {
10- Console . WriteLine ( "------- AES Test Started -------" ) ;
11+ // Console.WriteLine("------- AES Test Started -------");
1112
12- var dataToEncrypt = "Data to Encrypt" ;
13- var secret = "hghjuytsdfraestwsgtere==" ;
13+ var aesIv = KeyGenerators . GenerateHexadecimalIVKey ( ) ;
14+ var secret = KeyGenerators . GenerateAesSecretKey ( 256 ) ;
15+ var dataToEncrypt = "Hello World" ;
1416
15- // Encryption process
16- // this method generates a random IV key for the encryption process
17- // the IV is returned in the response with other properties
18- var response = await Aes . EncryptToBase64StringAsync ( dataToEncrypt , secret ) ;
17+ var data = new EncryptionParameters
18+ {
19+ Data = dataToEncrypt ,
20+ IV = aesIv ,
21+ SecretKey = secret
22+ } ;
23+
24+ Console . WriteLine ( $ "Hex Encryption Started") ;
25+ Console . WriteLine ( ) ;
26+ Console . WriteLine ( ) ;
27+ var encryptionResult = await Aes . EncryptToHexStringAsync ( data ) ;
1928
20- Console . WriteLine ( "............Encryption Started............" ) ;
29+ if ( encryptionResult . Errors . Count > 0 )
30+ {
31+ // List errors here
32+ }
2133
22- Console . WriteLine ( $ "Encrypted data: { response . EncryptedData } ") ;
23- Console . WriteLine ( $ "IV key: { response . Iv } ") ;
24- Console . WriteLine ( $ "Secret key: { response . SecretKey } ") ;
34+ Console . WriteLine ( $ "Hex Encrypted data: { encryptionResult . EncryptedData } ") ;
35+ Console . WriteLine ( $ "IV key: { encryptionResult . Iv } ") ;
36+ Console . WriteLine ( $ "Secret key: { encryptionResult . SecretKey } ") ;
2537
2638 Console . WriteLine ( ) ;
39+ Console . WriteLine ( ) ;
40+ Console . WriteLine ( $ "Hex Decryption Started") ;
41+ // Perform decryption using the same IV and secret
42+ var decryptionResult = await Aes . DecryptFromHexStringAsync ( new DecryptionParameters
43+ {
44+ Data = encryptionResult . EncryptedData ,
45+ IV = aesIv ,
46+ SecretKey = secret
47+ } ) ;
48+
49+ Console . WriteLine ( $ "Hex Decrypted data: { decryptionResult . DecryptedData } ") ;
50+ Console . WriteLine ( $ "IV key: { decryptionResult . Iv } ") ;
51+ Console . WriteLine ( $ "Secret key: { decryptionResult . SecretKey } ") ;
52+
53+ // Using the EncryptToBase64StringAsync and DecryptFromBase64StringAsync methods
54+
55+ var base64AesIv = KeyGenerators . GenerateBase64IVKey ( ) ;
2756
28- // Decryption process
29- var decryptorParam = new DecryptionParameters
57+ var base64dataToEncrypt = new EncryptionParameters
3058 {
31- IV = response . Iv ,
32- SecretKey = secret ,
33- Data = response . EncryptedData
59+ Data = dataToEncrypt ,
60+ IV = base64AesIv ,
61+ SecretKey = secret
3462 } ;
3563
36- var decryptionData = await Aes . DecryptFromBase64StringAsync ( decryptorParam ) ;
37-
38- Console . WriteLine ( "............Decryption Started............" ) ;
39- Console . WriteLine ( $ "Decrypted data: { decryptionData . DecryptedData } ") ;
40- Console . WriteLine ( $ "IV key: { decryptionData . Iv } ") ;
41- Console . WriteLine ( $ "Secret key: { decryptionData . SecretKey } ") ;
42-
4364 Console . WriteLine ( ) ;
65+ Console . WriteLine ( ) ;
66+ Console . WriteLine ( $ "Base64 Encryption Started") ;
67+ Console . WriteLine ( ) ;
68+ Console . WriteLine ( ) ;
69+ var encryptedResult = await Aes . EncryptToBase64StringAsync ( base64dataToEncrypt ) ;
70+ Console . WriteLine ( $ "Base64 Encrypted data: { encryptedResult . EncryptedData } ") ;
71+ Console . WriteLine ( $ "IV key: { encryptedResult . Iv } ") ;
72+ Console . WriteLine ( $ "Secret key: { encryptedResult . SecretKey } ") ;
73+ Console . WriteLine ( ) ;
74+ Console . WriteLine ( ) ;
75+
76+ Console . WriteLine ( $ "Base64 Decryption Started") ;
77+
78+
79+ var decryptionResponse = await Aes . DecryptFromBase64StringAsync ( new DecryptionParameters
80+ {
81+ Data = encryptedResult . EncryptedData ,
82+ IV = base64AesIv ,
83+ SecretKey = secret
84+ } ) ;
85+
86+ Console . WriteLine ( $ "Base64 Decrypted data: { decryptionResponse . DecryptedData } ") ;
87+ Console . WriteLine ( $ "IV key: { decryptionResponse . Iv } ") ;
88+ Console . WriteLine ( $ "Secret key: { decryptionResponse . SecretKey } ") ;
4489
45- Console . WriteLine ( "------- AES Test Ended -------" ) ;
90+ // Console.WriteLine("------- AES Test Ended -------");
4691
4792 }
4893}
0 commit comments