22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5+ using System . Runtime . InteropServices ;
56using System . Security . Cryptography ;
67using System . Text ;
78using System . Threading . Tasks ;
@@ -19,68 +20,126 @@ public static string GetRandomString(int length)
1920 . Select ( s => s [ random . Next ( s . Length ) ] ) . ToArray ( ) ) ;
2021 }
2122
22- public void Encrypt ( string inputFile , string outputFile )
23+ [ DllImport ( "KERNEL32.DLL" , EntryPoint = "RtlZeroMemory" ) ]
24+ public static extern bool ZeroMemory ( IntPtr Destination , int Length ) ;
25+
26+ public static byte [ ] GenerateRandomSalt ( )
2327 {
28+ byte [ ] data = new byte [ 32 ] ;
2429
25- try
30+ using ( RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider ( ) )
2631 {
27- string password = @"myKey123" ;
32+ for ( int i = 0 ; i < 10 ; i ++ )
33+ {
34+ rng . GetBytes ( data ) ;
35+ }
36+ }
37+
38+ return data ;
39+ }
40+
41+ public static void FileEncrypt ( string inputFile , string password )
42+ {
43+ byte [ ] salt = GenerateRandomSalt ( ) ;
44+
45+ FileStream fsCrypt = new FileStream ( inputFile + Properties . Settings . Default . extension , FileMode . Create ) ;
46+
47+ byte [ ] passwordBytes = System . Text . Encoding . UTF8 . GetBytes ( password ) ;
2848
29- UnicodeEncoding UE = new UnicodeEncoding ( ) ;
30- byte [ ] key = UE . GetBytes ( password ) ;
49+ RijndaelManaged AES = new RijndaelManaged ( ) ;
50+ AES . KeySize = 256 ;
51+ AES . BlockSize = 128 ;
52+ AES . Padding = PaddingMode . PKCS7 ;
3153
32- string cryptFile = outputFile ;
33- FileStream fsCrypt = new FileStream ( cryptFile , FileMode . Create ) ;
54+ var key = new Rfc2898DeriveBytes ( passwordBytes , salt , 50000 ) ;
55+ AES . Key = key . GetBytes ( AES . KeySize / 8 ) ;
56+ AES . IV = key . GetBytes ( AES . BlockSize / 8 ) ;
3457
35- RijndaelManaged RMCrypto = new RijndaelManaged ( ) ;
58+ AES . Mode = CipherMode . CFB ;
3659
37- CryptoStream cs = new CryptoStream ( fsCrypt ,
38- RMCrypto . CreateEncryptor ( key , key ) ,
39- CryptoStreamMode . Write ) ;
60+ fsCrypt . Write ( salt , 0 , salt . Length ) ;
4061
41- FileStream fsIn = new FileStream ( inputFile , FileMode . Open ) ;
62+ CryptoStream cs = new CryptoStream ( fsCrypt , AES . CreateEncryptor ( ) , CryptoStreamMode . Write ) ;
4263
43- int data ;
44- while ( ( data = fsIn . ReadByte ( ) ) != - 1 )
45- cs . WriteByte ( ( byte ) data ) ;
64+ FileStream fsIn = new FileStream ( inputFile , FileMode . Open ) ;
4665
66+ byte [ ] buffer = new byte [ 1048576 ] ;
67+ int read ;
68+
69+ try
70+ {
71+ while ( ( read = fsIn . Read ( buffer , 0 , buffer . Length ) ) > 0 )
72+ {
73+ Application . DoEvents ( ) ;
74+ cs . Write ( buffer , 0 , read ) ;
75+ }
4776
4877 fsIn . Close ( ) ;
49- cs . Close ( ) ;
50- fsCrypt . Close ( ) ;
5178 }
52- catch
79+ catch ( Exception ex )
5380 {
54- MessageBox . Show ( "Encryption failed!" , "Error" ) ;
81+ Console . WriteLine ( "Error: " + ex . Message ) ;
82+ }
83+ finally
84+ {
85+ cs . Close ( ) ;
86+ fsCrypt . Close ( ) ;
5587 }
5688 }
5789
58- public void Decrypt ( string inputFile , string outputFile )
90+ public static void FileDecrypt ( string inputFile , string outputFile , string password )
5991 {
60- {
61- string password = @"myKey123" ; // Your Key Here
92+ byte [ ] passwordBytes = System . Text . Encoding . UTF8 . GetBytes ( password ) ;
93+ byte [ ] salt = new byte [ 32 ] ;
6294
63- UnicodeEncoding UE = new UnicodeEncoding ( ) ;
64- byte [ ] key = UE . GetBytes ( password ) ;
95+ FileStream fsCrypt = new FileStream ( inputFile , FileMode . Open ) ;
96+ fsCrypt . Read ( salt , 0 , salt . Length ) ;
6597
66- FileStream fsCrypt = new FileStream ( inputFile , FileMode . Open ) ;
98+ RijndaelManaged AES = new RijndaelManaged ( ) ;
99+ AES . KeySize = 256 ;
100+ AES . BlockSize = 128 ;
101+ var key = new Rfc2898DeriveBytes ( passwordBytes , salt , 50000 ) ;
102+ AES . Key = key . GetBytes ( AES . KeySize / 8 ) ;
103+ AES . IV = key . GetBytes ( AES . BlockSize / 8 ) ;
104+ AES . Padding = PaddingMode . PKCS7 ;
105+ AES . Mode = CipherMode . CFB ;
67106
68- RijndaelManaged RMCrypto = new RijndaelManaged ( ) ;
107+ CryptoStream cs = new CryptoStream ( fsCrypt , AES . CreateDecryptor ( ) , CryptoStreamMode . Read ) ;
69108
70- CryptoStream cs = new CryptoStream ( fsCrypt ,
71- RMCrypto . CreateDecryptor ( key , key ) ,
72- CryptoStreamMode . Read ) ;
109+ FileStream fsOut = new FileStream ( outputFile , FileMode . Create ) ;
73110
74- FileStream fsOut = new FileStream ( outputFile , FileMode . Create ) ;
111+ int read ;
112+ byte [ ] buffer = new byte [ 1048576 ] ;
75113
76- int data ;
77- while ( ( data = cs . ReadByte ( ) ) != - 1 )
78- fsOut . WriteByte ( ( byte ) data ) ;
114+ try
115+ {
116+ while ( ( read = cs . Read ( buffer , 0 , buffer . Length ) ) > 0 )
117+ {
118+ Application . DoEvents ( ) ;
119+ fsOut . Write ( buffer , 0 , read ) ;
120+ }
121+ }
122+ catch ( CryptographicException ex_CryptographicException )
123+ {
124+ Console . WriteLine ( "CryptographicException error: " + ex_CryptographicException . Message ) ;
125+ }
126+ catch ( Exception ex )
127+ {
128+ Console . WriteLine ( "Error: " + ex . Message ) ;
129+ }
79130
80- fsOut . Close ( ) ;
131+ try
132+ {
81133 cs . Close ( ) ;
134+ }
135+ catch ( Exception ex )
136+ {
137+ Console . WriteLine ( "Error by closing CryptoStream: " + ex . Message ) ;
138+ }
139+ finally
140+ {
141+ fsOut . Close ( ) ;
82142 fsCrypt . Close ( ) ;
83-
84143 }
85144 }
86145 }
0 commit comments