Skip to content

Commit 31241c0

Browse files
committed
changed file management system for large files, doesnt work for them tho
1 parent f52d3a0 commit 31241c0

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Backend/AESCryptoManager.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Security.Cryptography;
34

45
namespace Encryption_App.Backend
@@ -28,14 +29,18 @@ 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

32+
long len = new FileInfo(inputFile).Length;
33+
3134
using (var outFileStream = new FileStream(outFile, FileMode.Create))
3235
using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
3336
using (var inFileStream = new FileStream(inputFile, FileMode.Create))
3437
{
35-
36-
sbyte data;
37-
while ((data = (sbyte)inFileStream.ReadByte()) != -1)
38-
cs.WriteByte((byte)data);
38+
long its = 0L;
39+
while (len > its)
40+
{
41+
cs.WriteByte((byte)inFileStream.ReadByte());
42+
its++;
43+
}
3944
}
4045
}
4146
}
@@ -66,13 +71,16 @@ public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
6671
try
6772
{
6873
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))
74+
using (var cs = new CryptoStream(outFileStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
75+
using (var inFileStream = new FileStream(inputFile, FileMode.Open))
7176
{
72-
73-
sbyte data;
74-
while ((data = (sbyte)inFileStream.ReadByte()) != -1)
75-
cs.WriteByte((byte)data);
77+
ulong len = Convert.ToUInt64(new FileInfo(inputFile).Length);
78+
ulong its = 0UL;
79+
while (len > its)
80+
{
81+
cs.WriteByte((byte)inFileStream.ReadByte());
82+
its++;
83+
}
7684
}
7785
}
7886
catch (CryptographicException)

src/UI/MainWindow.xaml.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,25 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
6767
{
6868
var pwd = InpTxtBox.Text;
6969
var tempFilePath = FileTxtBox.Text;
70+
Console.WriteLine(System.IO.Path.GetTempPath() + "tempdata.ini");
7071
var encryptor = new AesCryptoManager();
7172
encryptor.EncryptBytes(tempFilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
72-
File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", tempFilePath, true);
73+
File.Copy(Path.GetTempPath() + "tempdata.ini", tempFilePath, true);
74+
7375
}
7476

7577
private void Decrypt_Click(object sender, RoutedEventArgs e)
7678
{
7779
var pwd = PwdTxtBox.Text;
7880
var outFilePath = DecryptFileLocBox.Text;
79-
8081
var f = new FileInfo(outFilePath);
81-
8282
var decryptor = new AesCryptoManager();
8383
var worked = decryptor.DecryptBytes(outFilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
84-
if (worked) { File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", outFilePath, true); }
84+
if (worked)
85+
{
86+
File.Copy(Path.GetTempPath() + "tempdata.ini", outFilePath, true);
87+
Console.WriteLine("yay");
88+
}
8589

8690
MessageBox.Show(!worked ? "Wrong Password" : "Successfully Decrypted");
8791
}

0 commit comments

Comments
 (0)