1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using System . Security . Cryptography ;
34
45namespace Encryption_App . Backend
@@ -28,15 +29,23 @@ public void EncryptBytes(string inputFile, string outFile, byte[] passwordBytes)
2829 aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
2930 aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
3031
31- using ( var outFileStream = new FileStream ( outFile , FileMode . Create ) )
32+ long len = new FileInfo ( inputFile ) . Length ;
33+
34+ using ( var outFileStream = File . Create ( outFile ) )
3235 using ( var cs = new CryptoStream ( outFileStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
33- using ( var inFileStream = new FileStream ( inputFile , FileMode . Create ) )
36+ using ( var inFile = File . OpenRead ( inputFile ) )
3437 {
3538
36- sbyte data ;
37- while ( ( data = ( sbyte ) inFileStream . ReadByte ( ) ) != - 1 )
38- cs . WriteByte ( ( byte ) data ) ;
39+ using ( var br = new BinaryReader ( inFile ) )
40+ {
41+
42+ sbyte data ;
43+ while ( ( data = ( sbyte ) inFile . ReadByte ( ) ) != - 1 )
44+ cs . WriteByte ( ( byte ) data ) ;
45+
46+ }
3947 }
48+
4049 }
4150 }
4251
@@ -45,42 +54,53 @@ public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
4554
4655 var saltBytes = new byte [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;
4756
48- using ( var aes = new AesManaged ( ) )
57+ try
4958 {
59+ using ( var aes = new AesManaged ( ) )
60+ {
5061
51- // AESManaged properties
52- aes . KeySize = 256 ;
53- aes . BlockSize = 128 ;
54- aes . Padding = PaddingMode . PKCS7 ;
55- aes . Mode = CipherMode . CBC ;
62+ // AESManaged properties
63+ aes . KeySize = 256 ;
64+ aes . BlockSize = 128 ;
65+ aes . Padding = PaddingMode . PKCS7 ;
66+ aes . Mode = CipherMode . CBC ;
5667
5768
58- // Derives a key using PBKDF2 from the password and a salt
59- var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
69+ // Derives a key using PBKDF2 from the password and a salts
70+ var key = new Rfc2898DeriveBytes ( passwordBytes , saltBytes , 100000 ) ;
6071
6172
62- // Set actual IV and key
63- aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
64- aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
73+ // Set actual IV and key
74+ aes . Key = key . GetBytes ( aes . KeySize / 8 ) ;
75+ aes . IV = key . GetBytes ( aes . BlockSize / 8 ) ;
6576
66- try
67- {
68- using ( var outFileStream = new FileStream ( outFile , FileMode . Create ) )
77+ long len = new FileInfo ( inputFile ) . Length ;
78+
79+ using ( var outFileStream = File . Create ( outFile ) )
6980 using ( var cs = new CryptoStream ( outFileStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
70- using ( var inFileStream = new FileStream ( inputFile , FileMode . Create ) )
81+ using ( var inFile = File . OpenRead ( inputFile ) )
7182 {
7283
73- sbyte data ;
74- while ( ( data = ( sbyte ) inFileStream . ReadByte ( ) ) != - 1 )
75- cs . WriteByte ( ( byte ) data ) ;
84+ using ( var br = new BinaryReader ( inFile ) )
85+ {
86+
87+ sbyte data ;
88+ while ( ( data = ( sbyte ) inFile . ReadByte ( ) ) != - 1 )
89+ cs . WriteByte ( ( byte ) data ) ;
90+
91+ }
7692 }
93+
7794 }
78- catch ( CryptographicException )
79- {
80- return false ;
81- }
82- return true ;
8395 }
96+
97+ catch ( CryptographicException )
98+ {
99+ return false ;
100+ }
101+
102+ return true ;
84103 }
85104 }
86105}
106+
0 commit comments