File tree Expand file tree Collapse file tree 2 files changed +39
-7
lines changed
Expand file tree Collapse file tree 2 files changed +39
-7
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,14 @@ public byte[] SymEncrypt(byte[] data, byte[] pwd)
3535 {
3636 using ( var bw = new BinaryWriter ( cs ) )
3737 {
38- bw . Write ( data , 0 , data . Length ) ;
38+ try
39+ {
40+ bw . Write ( data , 0 , data . Length ) ;
41+ }
42+ catch ( Exception ex )
43+ {
44+ return null ;
45+ }
3946 }
4047 }
4148 return ms . ToArray ( ) ;
@@ -53,7 +60,7 @@ public byte[] SymDecrypt(byte[] data, byte[] pwd)
5360 {
5461 AES . KeySize = 256 ;
5562 AES . BlockSize = 128 ;
56- AES . Padding = PaddingMode . PKCS7 ;
63+ AES . Padding = PaddingMode . None ;
5764 AES . Mode = CipherMode . CBC ;
5865
5966 var key = new Rfc2898DeriveBytes ( pwd , saltBytes , 1000 ) ;
@@ -62,7 +69,14 @@ public byte[] SymDecrypt(byte[] data, byte[] pwd)
6269
6370 using ( var cs = new CryptoStream ( ms , AES . CreateDecryptor ( ) , CryptoStreamMode . Write ) )
6471 {
65- cs . Write ( data , 0 , data . Length ) ;
72+ try
73+ {
74+ cs . Write ( data , 0 , data . Length ) ;
75+ }
76+ catch ( Exception ex )
77+ {
78+ return null ;
79+ }
6680 }
6781 return ms . ToArray ( ) ;
6882 }
Original file line number Diff line number Diff line change 11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . IO ;
45using System . Linq ;
56using System . Security . Cryptography ;
@@ -81,9 +82,17 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
8182 Encryptor encryptor = new Encryptor ( ) ;
8283 byte [ ] encryptedData = encryptor . SymEncrypt ( data , Encoding . UTF8 . GetBytes ( pwd ) ) ;
8384
84- using ( var bw = new BinaryWriter ( File . Create ( filePath ) ) )
85+ if ( encryptedData . Length == 0 )
8586 {
86- bw . Write ( encryptedData ) ;
87+ MessageBox . Show ( "Encryption Failed" ) ;
88+ }
89+ else
90+ {
91+ using ( var bw = new BinaryWriter ( File . Create ( filePath ) ) )
92+ {
93+ bw . Write ( encryptedData ) ;
94+ }
95+ MessageBox . Show ( "Successfully Encrypted" ) ;
8796 }
8897 }
8998
@@ -100,10 +109,19 @@ private void Decrypt_Click(object sender, RoutedEventArgs e)
100109 Encryptor encryptor = new Encryptor ( ) ;
101110
102111 data = encryptor . SymDecrypt ( data , Encoding . UTF8 . GetBytes ( pwd ) ) ;
112+ Debug . Write ( "Stuff" ) ;
103113
104- using ( var bw = new BinaryWriter ( File . Create ( filePath ) ) )
114+ if ( data . Length == 0 )
105115 {
106- bw . Write ( data ) ;
116+ MessageBox . Show ( "Decryption Failed" ) ;
117+ }
118+ else
119+ {
120+ using ( var bw = new BinaryWriter ( File . Create ( filePath ) ) )
121+ {
122+ bw . Write ( data ) ;
123+ }
124+ MessageBox . Show ( "Successfully Decrypted" ) ;
107125 }
108126 }
109127 }
You can’t perform that action at this time.
0 commit comments