1- using System . IO ;
1+ using System ;
2+ using System . Diagnostics ;
3+ using System . IO ;
24using System . Security . Cryptography ;
5+ using utils ;
6+ using static System . Diagnostics . Stopwatch ;
37
4- namespace Encryption_App . Backend
8+ namespace CryptoTools
59{
610 public class AesCryptoManager
711 {
@@ -11,9 +15,8 @@ public class AesCryptoManager
1115 /// <param name="inputFile">The file path to the unencrypted data</param>
1216 /// <param name="outputFile">The file path to output the encrypted data to</param>
1317 /// <param name="keyBytes">The bytes of the key</param>
14- /// <param name="salt">The byte of the salt. Must be at least </param>
1518 /// <returns>true if successful, else false</returns>
16- public bool EncryptFileBytes ( string inputFile , string outputFile , byte [ ] keyBytes , byte [ ] salt )
19+ public bool EncryptFileBytes ( string inputFile , string outputFile , byte [ ] keyBytes )
1720 {
1821
1922 var saltBytes = new byte [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
@@ -32,6 +35,14 @@ public bool EncryptFileBytes(string inputFile, string outputFile, byte[] keyByte
3235 aes . Padding = PaddingMode . PKCS7 ;
3336 aes . Mode = CipherMode . CBC ;
3437
38+ #if DEBUG
39+ // Debug values
40+ if ( ! Stopwatch . IsHighResolution ) { throw new Exception ( "You don't have a high-res sysclock" ) ; }
41+ var iterations = 0L ;
42+ var fullIterationTime = 0.0D ;
43+ var watch = StartNew ( ) ;
44+ var avgIterationMilliseconds = 0D ;
45+ #endif
3546
3647 // Derives a key using PBKDF2 from the password and a salt
3748 var key = new Rfc2898DeriveBytes ( keyBytes , saltBytes , 100000 ) ;
@@ -43,18 +54,40 @@ public bool EncryptFileBytes(string inputFile, string outputFile, byte[] keyByte
4354 // Creates the streams necessary for reading and writing data
4455 using ( var outFileStream = File . Create ( outputFile ) )
4556 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
57+ using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) ) // BinaryReader is not a stream, but it's only argument is one
4758 {
4859 // Continuously reads the stream until it hits an EndOfStream exception
4960 while ( true )
5061 {
5162 try
5263 {
64+ #if DEBUG
65+ var offset = watch . Elapsed . TotalMilliseconds ;
66+ #endif
5367 var data = inFile . ReadByte ( ) ;
5468 cs . WriteByte ( data ) ;
69+ #if DEBUG
70+ var perIterationMilliseconds = watch . Elapsed . TotalMilliseconds - offset ;
71+ avgIterationMilliseconds = ( avgIterationMilliseconds * iterations + perIterationMilliseconds ) / ( iterations + 1 ) ;
72+ fullIterationTime += perIterationMilliseconds ;
73+ iterations ++ ;
74+ #endif
5575 }
5676 catch ( EndOfStreamException )
5777 {
78+ #if DEBUG
79+ var totalMilliseconds = watch . Elapsed . TotalMilliseconds ;
80+ var totalSeconds = totalMilliseconds / 1000 ;
81+ double perIterationSeconds = avgIterationMilliseconds / 1000 ,
82+ perIterationMilliseconds = avgIterationMilliseconds ;
83+ var toWrite = new [ ] { "Time to encrypt (s):" + totalSeconds , "Time to encrypt (ms):" + totalMilliseconds ,
84+ "Average iteration length (s):" + perIterationSeconds . ToString ( "0." + new string ( '#' , 339 ) ) , "Average iteration length (ms):" + perIterationMilliseconds . ToString ( "0." + new string ( '#' , 339 ) ) ,
85+ "Time of all iterations, combined (s):" + fullIterationTime / 1000 , "Time of all iterations, combined (ms):" + fullIterationTime ,
86+ "Iterations:" + iterations } ;
87+
88+ Utils . WriteToDiagnosticsFile ( toWrite ) ;
89+ #endif
90+
5891 break ;
5992 }
6093 }
@@ -68,7 +101,7 @@ public bool EncryptFileBytes(string inputFile, string outputFile, byte[] keyByte
68101
69102 return true ;
70103 }
71-
104+
72105 /// <summary>
73106 /// Encrypts data from one file to another using AES
74107 /// </summary>
@@ -95,6 +128,14 @@ public bool DecryptFileBytes(string inputFile, string outputFile, byte[] keyByte
95128 aes . Padding = PaddingMode . PKCS7 ;
96129 aes . Mode = CipherMode . CBC ;
97130
131+ #if DEBUG
132+ // Debug values
133+ if ( ! Stopwatch . IsHighResolution ) { throw new Exception ( "You don't have a high-res sysclock" ) ; }
134+ var iterations = 0L ;
135+ var fullIterationTime = 0.0D ;
136+ var watch = StartNew ( ) ;
137+ var avgIterationMilliseconds = 0D ;
138+ #endif
98139
99140 // Derives a key using PBKDF2 from the password and a salt
100141 var key = new Rfc2898DeriveBytes ( keyBytes , saltBytes , 100000 ) ;
@@ -113,11 +154,33 @@ public bool DecryptFileBytes(string inputFile, string outputFile, byte[] keyByte
113154 {
114155 try
115156 {
157+ #if DEBUG
158+ var offset = watch . Elapsed . TotalMilliseconds ;
159+ #endif
116160 var data = inFile . ReadByte ( ) ;
117161 cs . WriteByte ( data ) ;
162+ #if DEBUG
163+ var perIterationMilliseconds = watch . Elapsed . TotalMilliseconds - offset ;
164+ avgIterationMilliseconds = ( avgIterationMilliseconds * iterations + perIterationMilliseconds ) / ( iterations + 1 ) ;
165+ fullIterationTime += perIterationMilliseconds ;
166+ iterations ++ ;
167+ #endif
118168 }
119169 catch ( EndOfStreamException )
120170 {
171+ #if DEBUG
172+ var totalMilliseconds = watch . Elapsed . TotalMilliseconds ;
173+ var totalSeconds = totalMilliseconds / 1000 ;
174+ double perIterationSeconds = avgIterationMilliseconds / 1000 ,
175+ perIterationMilliseconds = avgIterationMilliseconds ;
176+ var toWrite = new [ ] { "Time to encrypt (s):" + totalSeconds , "Time to encrypt (ms):" + totalMilliseconds ,
177+ "Average iteration length (s):" + perIterationSeconds . ToString ( "0." + new string ( '#' , 339 ) ) , "Average iteration length (ms):" + perIterationMilliseconds . ToString ( "0." + new string ( '#' , 339 ) ) ,
178+ "Time of all iterations, combined (s):" + fullIterationTime / 1000 , "Time of all iterations, combined (ms):" + fullIterationTime ,
179+ "Iterations:" + iterations } ;
180+
181+ Utils . WriteToDiagnosticsFile ( toWrite ) ;
182+ #endif
183+
121184 break ;
122185 }
123186 }
0 commit comments