Skip to content

Commit b90366a

Browse files
authored
Merge pull request #22 from johnkellyoxford/experimental
Experimental
2 parents f52d3a0 + 2467675 commit b90366a

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

src/Backend/AESCryptoManager.cs

Lines changed: 53 additions & 31 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
@@ -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+

src/UI/MainWindow.xaml.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public MainWindow()
2020
InitializeComponent();
2121
DropDown.ItemsSource = _dropDownItems;
2222
DropDown.SelectedIndex = 0;
23-
2423
}
2524

2625
private void MenuItem_Click(object sender, RoutedEventArgs e)
@@ -67,21 +66,24 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
6766
{
6867
var pwd = InpTxtBox.Text;
6968
var tempFilePath = FileTxtBox.Text;
69+
Console.WriteLine(System.IO.Path.GetTempPath() + "tempdata.ini");
7070
var encryptor = new AesCryptoManager();
7171
encryptor.EncryptBytes(tempFilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
72-
File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", tempFilePath, true);
72+
File.Copy(Path.GetTempPath() + "tempdata.ini", tempFilePath, true);
73+
7374
}
7475

7576
private void Decrypt_Click(object sender, RoutedEventArgs e)
7677
{
7778
var pwd = PwdTxtBox.Text;
7879
var outFilePath = DecryptFileLocBox.Text;
79-
8080
var f = new FileInfo(outFilePath);
81-
8281
var decryptor = new AesCryptoManager();
8382
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); }
83+
if (worked)
84+
{
85+
File.Copy(Path.GetTempPath() + "tempdata.ini", outFilePath, true);
86+
}
8587

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

0 commit comments

Comments
 (0)