Skip to content

Commit 83cbdd8

Browse files
committed
no text
1 parent f52d3a0 commit 83cbdd8

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

src/Backend/AESCryptoManager.cs

Lines changed: 49 additions & 29 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,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+

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)