1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using System . Security . Cryptography ;
34
45namespace Encryption_App . Backend
@@ -23,20 +24,29 @@ public void EncryptBytes(string inputFile, string outFile, byte[] passwordBytes)
2324 // Derives a key using PBKDF2 from the password and a salts
2425 var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
2526
26-
2727 // Set actual IV and key
2828 aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
2929 aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
3030
31- using ( var outFileStream = new FileStream ( outFile , FileMode . Create ) )
31+ using ( var outFileStream = File . Create ( outFile ) )
3232 using ( var cs = new CryptoStream ( outFileStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
33- using ( var inFileStream = new FileStream ( inputFile , FileMode . Create ) )
33+ using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) )
3434 {
35+ while ( true )
36+ {
37+ try
38+ {
39+ var data = ( byte ) inFile . ReadByte ( ) ;
40+ cs . WriteByte ( data ) ;
41+ }
42+ catch ( EndOfStreamException )
43+ {
44+ break ;
45+ }
46+ }
3547
36- sbyte data ;
37- while ( ( data = ( sbyte ) inFileStream . ReadByte ( ) ) != - 1 )
38- cs . WriteByte ( ( byte ) data ) ;
3948 }
49+
4050 }
4151 }
4252
@@ -45,42 +55,54 @@ public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
4555
4656 var saltBytes = new byte [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
4757
48- using ( var aes = new AesManaged ( ) )
58+ try
4959 {
60+ using ( var aes = new AesManaged ( ) )
61+ {
5062
51- // AESManaged properties
52- aes . KeySize = 256 ;
53- aes . BlockSize = 128 ;
54- aes . Padding = PaddingMode . PKCS7 ;
55- aes . Mode = CipherMode . CBC ;
63+ // AESManaged properties
64+ aes . KeySize = 256 ;
65+ aes . BlockSize = 128 ;
66+ aes . Padding = PaddingMode . PKCS7 ;
67+ aes . Mode = CipherMode . CBC ;
5668
5769
58- // Derives a key using PBKDF2 from the password and a salt
59- var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
70+ // Derives a key using PBKDF2 from the password and a salts
71+ var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
6072
6173
62- // Set actual IV and key
63- aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
64- aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
74+ // Set actual IV and key
75+ aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
76+ aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
6577
66- try
67- {
68- using ( var outFileStream = new FileStream ( outFile , FileMode . Create ) )
69- using ( var cs = new CryptoStream ( outFileStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
70- using ( var inFileStream = new FileStream ( inputFile , FileMode . Create ) )
78+ using ( var outFileStream = File . Create ( outFile ) )
79+ using ( var cs = new CryptoStream ( outFileStream , aes . CreateDecryptor ( ) , CryptoStreamMode . Write ) )
80+ using ( var inFile = new BinaryReader ( File . OpenRead ( inputFile ) ) )
7181 {
82+ while ( true )
83+ {
84+ try
85+ {
86+ var data = ( byte ) inFile . ReadByte ( ) ;
87+ cs . WriteByte ( data ) ;
88+ }
89+ catch ( EndOfStreamException )
90+ {
91+ break ;
92+ }
93+ }
7294
73- sbyte data ;
74- while ( ( data = ( sbyte ) inFileStream . ReadByte ( ) ) != - 1 )
75- cs . WriteByte ( ( byte ) data ) ;
7695 }
7796 }
78- catch ( CryptographicException )
79- {
80- return false ;
81- }
82- return true ;
8397 }
98+
99+ catch ( CryptographicException )
100+ {
101+ return false ;
102+ }
103+
104+ return true ;
84105 }
85106 }
86107}
108+
0 commit comments