Skip to content

Commit c7baeac

Browse files
committed
removed abstract parent
unnecessary, also refactored
1 parent ec4f164 commit c7baeac

File tree

4 files changed

+81
-64
lines changed

4 files changed

+81
-64
lines changed

src/Backend/AESCryptoManager.cs

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,92 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.Security.Cryptography;
43

54
namespace Encryption_App.Backend
65
{
7-
internal class AesCryptoManager
6+
public class AesCryptoManager
87
{
9-
public void EncryptBytes(string inputFile, string outFile, byte[] passwordBytes)
8+
/// <summary>
9+
/// Encrypts data from one file to another using AES
10+
/// </summary>
11+
/// <param name="inputFile">The file path to the unencrypted data</param>
12+
/// <param name="outputFile">The file path to output the encrypted data to</param>
13+
/// <param name="keyBytes">The bytes of the key</param>
14+
/// <param name="salt">The byte of the salt. Must be at least </param>
15+
/// <returns>true if successful, else false</returns>
16+
public bool EncryptFileBytes(string inputFile, string outputFile, byte[] keyBytes, byte[] salt)
1017
{
1118

1219
var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
1320

14-
using (var aes = new AesManaged())
21+
// Any cryptographic exception indicates the data is invalid or an incorrect password has been inputted
22+
try
1523
{
1624

17-
// AESManaged properties
18-
aes.KeySize = 256;
19-
aes.BlockSize = 128;
20-
aes.Padding = PaddingMode.PKCS7;
21-
aes.Mode = CipherMode.CBC;
25+
// Creates the instance used to encrypt. This implements IDisposable so is inside a using statement
26+
using (var aes = new AesCryptoServiceProvider())
27+
{
2228

29+
// AESManaged properties
30+
aes.KeySize = 256;
31+
aes.BlockSize = 128;
32+
aes.Padding = PaddingMode.PKCS7;
33+
aes.Mode = CipherMode.CBC;
2334

24-
// Derives a key using PBKDF2 from the password and a salts
25-
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
2635

27-
// Set actual IV and key
28-
aes.Key = key.GetBytes(aes.KeySize / 8);
29-
aes.IV = key.GetBytes(aes.BlockSize / 8);
36+
// Derives a key using PBKDF2 from the password and a salt
37+
var key = new Rfc2898DeriveBytes(keyBytes, saltBytes, 100000);
3038

31-
using (var outFileStream = File.Create(outFile))
32-
using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
33-
using (var inFile = new BinaryReader(File.OpenRead(inputFile)))
34-
{
35-
while (true)
39+
// Set actual IV and key
40+
aes.Key = key.GetBytes(aes.KeySize / 8);
41+
aes.IV = key.GetBytes(aes.BlockSize / 8);
42+
43+
// Creates the streams necessary for reading and writing data
44+
using (var outFileStream = File.Create(outputFile))
45+
using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
46+
using (var inFile = new BinaryReader(File.OpenRead(inputFile))) // BinaryReader is not a stream, but it's only argument is
3647
{
37-
try
38-
{
39-
var data = (byte)inFile.ReadByte();
40-
cs.WriteByte(data);
41-
}
42-
catch (EndOfStreamException)
48+
// Continuously reads the stream until it hits an EndOfStream exception
49+
while (true)
4350
{
44-
break;
51+
try
52+
{
53+
var data = inFile.ReadByte();
54+
cs.WriteByte(data);
55+
}
56+
catch (EndOfStreamException)
57+
{
58+
break;
59+
}
4560
}
4661
}
47-
4862
}
49-
5063
}
51-
}
64+
catch (CryptographicException) // If something went wrong, we get it here
65+
{
66+
return false;
67+
}
5268

53-
public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
69+
return true;
70+
}
71+
72+
/// <summary>
73+
/// Encrypts data from one file to another using AES
74+
/// </summary>
75+
/// <param name="inputFile">The file path to the unencrypted data</param>
76+
/// <param name="outputFile">The file path to output the encrypted data to</param>
77+
/// <param name="keyBytes">The bytes of the key</param>
78+
/// <returns>true if successful, else false</returns>
79+
public bool DecryptFileBytes(string inputFile, string outputFile, byte[] keyBytes)
5480
{
5581

5682
var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
5783

84+
// Any cryptographic exception indicates the data is invalid or an incorrect password has been inputted
5885
try
5986
{
60-
using (var aes = new AesManaged())
87+
88+
// Creates the instance used to decrypt. This implements IDisposable so is inside a using statement
89+
using (var aes = new AesCryptoServiceProvider())
6190
{
6291

6392
// AESManaged properties
@@ -67,42 +96,40 @@ public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
6796
aes.Mode = CipherMode.CBC;
6897

6998

70-
// Derives a key using PBKDF2 from the password and a salts
71-
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
72-
99+
// Derives a key using PBKDF2 from the password and a salt
100+
var key = new Rfc2898DeriveBytes(keyBytes, saltBytes, 100000);
73101

74102
// Set actual IV and key
75103
aes.Key = key.GetBytes(aes.KeySize / 8);
76104
aes.IV = key.GetBytes(aes.BlockSize / 8);
77105

78-
using (var outFileStream = File.Create(outFile))
106+
// Creates the streams necessary for reading and writing data
107+
using (var outFileStream = File.Create(outputFile))
79108
using (var cs = new CryptoStream(outFileStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
80-
using (var inFile = new BinaryReader(File.OpenRead(inputFile)))
109+
using (var inFile = new BinaryReader(File.OpenRead(inputFile))) // BinaryReader is not a stream, but it's only argument is one
81110
{
111+
// Continuously reads the stream until it hits an EndOfStream exception
82112
while (true)
83113
{
84114
try
85115
{
86-
var data = (byte)inFile.ReadByte();
116+
var data = inFile.ReadByte();
87117
cs.WriteByte(data);
88118
}
89119
catch (EndOfStreamException)
90120
{
91121
break;
92122
}
93123
}
94-
95124
}
96125
}
97126
}
98-
99-
catch (CryptographicException)
127+
catch (CryptographicException) // If something went wrong, we get it here
100128
{
101129
return false;
102130
}
103131

104132
return true;
105133
}
106134
}
107-
}
108-
135+
}

src/Backend/CryptoManager.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/UI/MainWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
<Window x:Class="Encryption_App.MainWindow"
1+
<Window x:Class="Encryption_App.UI.MainWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:local="clr-namespace:Encryption_App"
6+
xmlns:local="clr-namespace:Encryption_App.UI"
77
mc:Ignorable="d"
88
Title="MainWindow" Height="450" Width="392">
99
<Grid>
1010

1111
<TabControl HorizontalAlignment="Left" Height="397" VerticalAlignment="Top" Width="792" Margin="0,22,0,0">
12-
<TabItem Header="Encrypytion">
12+
<TabItem Header="Encryption">
1313
<Grid Background="#FFE5E5E5" Margin="0,0,405,0">
1414
<Grid.ColumnDefinitions>
1515
<ColumnDefinition Width="91*"/>

src/UI/MainWindow.xaml.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
using System.Windows;
66
using Encryption_App.Backend;
77

8-
namespace Encryption_App
8+
namespace Encryption_App.UI
99
{
10+
/// <inheritdoc cref="Window"/>
1011
/// <summary>
1112
/// Interaction logic for MainWindow.xaml
1213
/// </summary>
13-
public partial class MainWindow : Window
14+
public partial class MainWindow
1415
{
1516
readonly List<string> _dropDownItems = new List<string> { "Choose Option...", "Encrypt a file", "Encrypt a file for sending to someone" };
1617

17-
1818
public MainWindow()
1919
{
2020
InitializeComponent();
2121
DropDown.ItemsSource = _dropDownItems;
2222
DropDown.SelectedIndex = 0;
2323
}
2424

25-
private void MenuItem_Click(object sender, RoutedEventArgs e)
25+
private void MenuItem_Click(RoutedEventArgs e)
2626
{
2727

2828
}
@@ -66,9 +66,9 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
6666
{
6767
var pwd = InpTxtBox.Text;
6868
var tempFilePath = FileTxtBox.Text;
69-
Console.WriteLine(System.IO.Path.GetTempPath() + "tempdata.ini");
69+
Console.WriteLine(Path.GetTempPath() + "tempdata.ini");
7070
var encryptor = new AesCryptoManager();
71-
encryptor.EncryptBytes(tempFilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
71+
encryptor.EncryptFileBytes(tempFilePath, Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
7272
File.Copy(Path.GetTempPath() + "tempdata.ini", tempFilePath, true);
7373

7474
}
@@ -77,15 +77,14 @@ private void Decrypt_Click(object sender, RoutedEventArgs e)
7777
{
7878
var pwd = PwdTxtBox.Text;
7979
var outFilePath = DecryptFileLocBox.Text;
80-
var f = new FileInfo(outFilePath);
8180
var decryptor = new AesCryptoManager();
82-
var worked = decryptor.DecryptBytes(outFilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
81+
var worked = decryptor.DecryptFileBytes(outFilePath, Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
8382
if (worked)
8483
{
8584
File.Copy(Path.GetTempPath() + "tempdata.ini", outFilePath, true);
8685
}
8786

88-
MessageBox.Show(!worked ? "Wrong Password" : "Successfully Decrypted");
87+
MessageBox.Show(worked ? "Successfully Decrypted" : "Wrong password or corrupted file");
8988
}
9089
}
9190
}

0 commit comments

Comments
 (0)