1- using System ;
2- using System . IO ;
1+ using System . IO ;
32using System . Security . Cryptography ;
43
54namespace Encryption_App . Backend
65{
7- internal class AesCryptoManager
6+ public class AesCryptoManager
87 {
9- public void EncryptBytes ( string inputFile , string outFile , byte [ ] passwordBytes )
8+ /// <summary>
9+ /// Encrypts data from one file to another using AES
10+ /// </summary>
11+ /// <param name="inputFile">The file path to the unencrypted data</param>
12+ /// <param name="outputFile">The file path to output the encrypted data to</param>
13+ /// <param name="keyBytes">The bytes of the key</param>
14+ /// <param name="salt">The byte of the salt. Must be at least </param>
15+ /// <returns>true if successful, else false</returns>
16+ public bool EncryptFileBytes ( string inputFile , string outputFile , byte [ ] keyBytes , byte [ ] salt )
1017 {
1118
1219 var saltBytes = new byte [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
1320
14- using ( var aes = new AesManaged ( ) )
21+ // Any cryptographic exception indicates the data is invalid or an incorrect password has been inputted
22+ try
1523 {
1624
17- // AESManaged properties
18- aes . KeySize = 256 ;
19- aes . BlockSize = 128 ;
20- aes . Padding = PaddingMode . PKCS7 ;
21- aes . Mode = CipherMode . CBC ;
25+ // Creates the instance used to encrypt. This implements IDisposable so is inside a using statement
26+ using ( var aes = new AesCryptoServiceProvider ( ) )
27+ {
2228
29+ // AESManaged properties
30+ aes . KeySize = 256 ;
31+ aes . BlockSize = 128 ;
32+ aes . Padding = PaddingMode . PKCS7 ;
33+ aes . Mode = CipherMode . CBC ;
2334
24- // Derives a key using PBKDF2 from the password and a salts
25- var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
2635
27- // Set actual IV and key
28- aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
29- aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
36+ // Derives a key using PBKDF2 from the password and a salt
37+ var key = new Rfc2898DeriveBytes ( keyBytes , saltBytes , 100000 ) ;
3038
31- using ( var outFileStream = File . Create ( outFile ) )
32- using ( var cs = new CryptoStream ( outFileStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
33- using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) )
34- {
35- while ( true )
39+ // Set actual IV and key
40+ aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
41+ aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
42+
43+ // Creates the streams necessary for reading and writing data
44+ using ( var outFileStream = File . Create ( outputFile ) )
45+ using ( var cs = new CryptoStream ( outFileStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
46+ using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) ) // BinaryReader is not a stream, but it's only argument is
3647 {
37- try
38- {
39- var data = ( byte ) inFile . ReadByte ( ) ;
40- cs . WriteByte ( data ) ;
41- }
42- catch ( EndOfStreamException )
48+ // Continuously reads the stream until it hits an EndOfStream exception
49+ while ( true )
4350 {
44- break ;
51+ try
52+ {
53+ var data = inFile . ReadByte ( ) ;
54+ cs . WriteByte ( data ) ;
55+ }
56+ catch ( EndOfStreamException )
57+ {
58+ break ;
59+ }
4560 }
4661 }
47-
4862 }
49-
5063 }
51- }
64+ catch ( CryptographicException ) // If something went wrong, we get it here
65+ {
66+ return false ;
67+ }
5268
53- public bool DecryptBytes ( string inputFile , string outFile , byte [ ] passwordBytes )
69+ return true ;
70+ }
71+
72+ /// <summary>
73+ /// Encrypts data from one file to another using AES
74+ /// </summary>
75+ /// <param name="inputFile">The file path to the unencrypted data</param>
76+ /// <param name="outputFile">The file path to output the encrypted data to</param>
77+ /// <param name="keyBytes">The bytes of the key</param>
78+ /// <returns>true if successful, else false</returns>
79+ public bool DecryptFileBytes ( string inputFile , string outputFile , byte [ ] keyBytes )
5480 {
5581
5682 var saltBytes = new byte [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
5783
84+ // Any cryptographic exception indicates the data is invalid or an incorrect password has been inputted
5885 try
5986 {
60- using ( var aes = new AesManaged ( ) )
87+
88+ // Creates the instance used to decrypt. This implements IDisposable so is inside a using statement
89+ using ( var aes = new AesCryptoServiceProvider ( ) )
6190 {
6291
6392 // AESManaged properties
@@ -67,42 +96,40 @@ public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
6796 aes . Mode = CipherMode . CBC ;
6897
6998
70- // Derives a key using PBKDF2 from the password and a salts
71- var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
72-
99+ // Derives a key using PBKDF2 from the password and a salt
100+ var key = new Rfc2898DeriveBytes ( keyBytes , saltBytes , 100000 ) ;
73101
74102 // Set actual IV and key
75103 aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
76104 aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
77105
78- using ( var outFileStream = File . Create ( outFile ) )
106+ // Creates the streams necessary for reading and writing data
107+ using ( var outFileStream = File . Create ( outputFile ) )
79108 using ( var cs = new CryptoStream ( outFileStream , aes . CreateDecryptor ( ) , CryptoStreamMode . Write ) )
80- using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) )
109+ using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) ) // BinaryReader is not a stream, but it's only argument is one
81110 {
111+ // Continuously reads the stream until it hits an EndOfStream exception
82112 while ( true )
83113 {
84114 try
85115 {
86- var data = ( byte ) inFile . ReadByte ( ) ;
116+ var data = inFile . ReadByte ( ) ;
87117 cs . WriteByte ( data ) ;
88118 }
89119 catch ( EndOfStreamException )
90120 {
91121 break ;
92122 }
93123 }
94-
95124 }
96125 }
97126 }
98-
99- catch ( CryptographicException )
127+ catch ( CryptographicException ) // If something went wrong, we get it here
100128 {
101129 return false ;
102130 }
103131
104132 return true ;
105133 }
106134 }
107- }
108-
135+ }
0 commit comments