Skip to content

Commit 2a5a82c

Browse files
committed
fixed
1 parent e960c48 commit 2a5a82c

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

UI/MainWindow.xaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1+
<Window x:Class="Encryption_App.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Encryption_App"
7+
mc:Ignorable="d"
8+
Title="MainWindow" Height="450" Width="392">
9+
<Grid>
110

11+
<TabControl HorizontalAlignment="Left" Height="397" VerticalAlignment="Top" Width="792" Margin="0,22,0,0">
12+
<TabItem Header="Encrypytion">
13+
<Grid Background="#FFE5E5E5" Margin="0,0,405,0">
14+
<Grid.ColumnDefinitions>
15+
<ColumnDefinition Width="91*"/>
16+
<ColumnDefinition Width="290*"/>
17+
</Grid.ColumnDefinitions>
18+
<ComboBox HorizontalAlignment="Left" Margin="26,53.04,0,0" VerticalAlignment="Top" Width="286" Height="25.96" x:Name="DropDown" Grid.ColumnSpan="2"/>
19+
<CheckBox Content="File?" HorizontalAlignment="Left" Margin="177.695,122.941,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.14,0.464" Click="CheckBox_Click" Grid.Column="1"/>
20+
<TextBox HorizontalAlignment="Left" Height="23" Margin="26,214.04,0,0" TextWrapping="Wrap" Text="File Path" VerticalAlignment="Top" Width="216" x:Name="FileTxtBox" Grid.ColumnSpan="2"/>
21+
<Button Content="Browse" HorizontalAlignment="Left" Margin="156,214.04,0,0" VerticalAlignment="Top" Width="75" Height="23" Click="Button_Click" Grid.Column="1"/>
22+
<Button Content="Encrypt" HorizontalAlignment="Left" Margin="50,317.08,0,0" VerticalAlignment="Top" Width="101" Height="41.96" Click="Encrypt_Click" Grid.Column="1"/>
23+
<TextBox HorizontalAlignment="Left" Height="23" Margin="26,143.039,0,0" TextWrapping="Wrap" Text="Input Pwd" VerticalAlignment="Top" Width="286" x:Name="InpTxtBox" Grid.ColumnSpan="2"/>
24+
</Grid>
25+
</TabItem>
26+
<TabItem Header="Decryption">
27+
<Grid Background="#FFE5E5E5">
28+
<Grid.ColumnDefinitions>
29+
<ColumnDefinition Width="6*"/>
30+
<ColumnDefinition Width="125*"/>
31+
</Grid.ColumnDefinitions>
32+
<Button Content="Decrypt" HorizontalAlignment="Left" Margin="7,198.04,0,0" VerticalAlignment="Top" Width="284" Height="143" Grid.Column="1" Click="Decrypt_Click"/>
33+
<TextBox HorizontalAlignment="Left" Height="23" Margin="35,35.04,0,0" TextWrapping="Wrap" Text="File Path" VerticalAlignment="Top" Width="238" x:Name="DFileTxtBox" Grid.ColumnSpan="2"/>
34+
<Button Content="Browse" HorizontalAlignment="Left" Margin="242,35.04,0,0" VerticalAlignment="Top" Width="75" Height="23" Grid.Column="1" Click="FilePath_Click"/>
35+
<TextBox Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="7,114.04,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top" Width="195" x:Name="PwdTxtBox"/>
36+
37+
</Grid>
38+
</TabItem>
39+
</TabControl>
40+
41+
</Grid>
42+
</Window>

UI/MainWindow.xaml.cs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Security.Cryptography;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows;
9+
using System.Windows.Controls;
10+
using System.Windows.Data;
11+
using System.Windows.Documents;
12+
using System.Windows.Input;
13+
using System.Windows.Media;
14+
using System.Windows.Media.Imaging;
15+
using System.Windows.Navigation;
16+
using System.Windows.Shapes;
17+
18+
namespace Encryption_App
19+
{
20+
/// <summary>
21+
/// Interaction logic for MainWindow.xaml
22+
/// </summary>
23+
public partial class MainWindow : Window
24+
{
25+
List<String> DropDownItems = new List<string> { "Choose Option...", "Cow", "Chicken" };
26+
27+
28+
public MainWindow()
29+
{
30+
InitializeComponent();
31+
DropDown.ItemsSource = DropDownItems;
32+
DropDown.SelectedIndex = 0;
33+
34+
}
35+
36+
private void MenuItem_Click(object sender, RoutedEventArgs e)
37+
{
38+
39+
}
40+
41+
private void CheckBox_Click(object sender, RoutedEventArgs e)
42+
{
43+
44+
}
45+
46+
private void FilePath_Click(object sender, RoutedEventArgs e)
47+
{
48+
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog
49+
{
50+
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)
51+
};
52+
53+
bool? result = openFileDialog.ShowDialog();
54+
55+
if (result == true)
56+
{
57+
DFileTxtBox.Text = openFileDialog.FileName;
58+
}
59+
}
60+
61+
private void Button_Click(object sender, RoutedEventArgs e)
62+
{
63+
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog
64+
{
65+
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)
66+
};
67+
68+
bool? result = openFileDialog.ShowDialog();
69+
70+
if(result == true)
71+
{
72+
FileTxtBox.Text = openFileDialog.FileName;
73+
}
74+
}
75+
76+
private void Encrypt_Click(object sender, RoutedEventArgs e)
77+
{
78+
string pwd = InpTxtBox.Text;
79+
string filePath = FileTxtBox.Text;
80+
string cryptFilePath = filePath + ".crypt";
81+
82+
// Set your salt here, change it to meet your flavor:
83+
// The salt bytes must be at least 8 bytes.
84+
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
85+
using (var inFile = File.OpenRead(filePath))
86+
{
87+
using (var file = File.Open(cryptFilePath, FileMode.Create))
88+
{
89+
using (AesManaged AES = new AesManaged())
90+
{
91+
AES.KeySize = 256;
92+
AES.BlockSize = 128;
93+
94+
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
95+
AES.Key = key.GetBytes(AES.KeySize / 8);
96+
AES.IV = key.GetBytes(AES.BlockSize / 8);
97+
98+
AES.Mode = CipherMode.CBC;
99+
byte[] bArray = new byte[16];
100+
using (var cs = new CryptoStream(file, AES.CreateEncryptor(), CryptoStreamMode.Write))
101+
{
102+
using (var br = new BinaryReader(inFile))
103+
{
104+
long fileSize = new FileInfo(filePath).Length;
105+
for (int count = 0; count < Math.Floor((decimal)fileSize/16); count++)
106+
{
107+
bArray = br.ReadBytes((int)count*16);
108+
cs.Write(bArray, 0, bArray.Length);
109+
cs.Flush();
110+
}
111+
cs.Close();
112+
MessageBox.Show("Successfully Encrypted");
113+
}
114+
}
115+
}
116+
}
117+
}
118+
119+
}
120+
121+
private void Decrypt_Click(object sender, RoutedEventArgs e)
122+
{
123+
string pwd = PwdTxtBox.Text;
124+
string filePath = DFileTxtBox.Text;
125+
string cryptFilePath = filePath + ".crypt";
126+
127+
// Set your salt here, change it to meet your flavor:
128+
// The salt bytes must be at least 8 bytes.
129+
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
130+
using (var inFile = File.OpenRead(filePath))
131+
{
132+
using (var file = File.Open(cryptFilePath, FileMode.Create))
133+
{
134+
using (AesManaged AES = new AesManaged())
135+
{
136+
//AES.KeySize = 256;
137+
AES.BlockSize = 128;
138+
AES.Padding = PaddingMode.PKCS7;
139+
140+
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
141+
AES.Key = key.GetBytes(AES.KeySize / 8);
142+
AES.IV = key.GetBytes(AES.BlockSize / 8);
143+
144+
AES.Mode = CipherMode.CBC;
145+
byte[] bArray = new byte[16];
146+
using (var cs = new CryptoStream(file, AES.CreateDecryptor(), CryptoStreamMode.Write))
147+
{
148+
using (var br = new BinaryReader(inFile))
149+
{
150+
long fileSize = new FileInfo(filePath).Length;
151+
for (int count = 0; count < Math.Floor((decimal)fileSize / 16); count++)
152+
{
153+
bArray = br.ReadBytes((int)count * 16);
154+
cs.Write(bArray, 0, bArray.Length);
155+
cs.Flush();
156+
}
157+
cs.Close();
158+
MessageBox.Show("Successfully Encrypted");
159+
}
160+
}
161+
}
162+
}
163+
}
164+
165+
}
166+
}
167+
}

backend/Encryptor.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.IO;
7+
using System.Security.Cryptography;
8+
9+
namespace Encryption_App
10+
{
11+
class Encryptor
12+
{
13+
public void SymEncrypt(string Data)
14+
{
15+
16+
}
17+
18+
public void SymEncrypt(byte[] bArray, byte[] pwdBytes)
19+
{
20+
byte[] encryptedBytes = null;
21+
22+
// Set your salt here, change it to meet your flavor:
23+
// The salt bytes must be at least 8 bytes.
24+
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
25+
26+
using (MemoryStream ms = new MemoryStream())
27+
{
28+
using (RijndaelManaged AES = new RijndaelManaged())
29+
{
30+
AES.KeySize = 256;
31+
AES.BlockSize = 128;
32+
33+
var key = new Rfc2898DeriveBytes(pwdBytes, saltBytes, 1000);
34+
AES.Key = key.GetBytes(AES.KeySize / 8);
35+
AES.IV = key.GetBytes(AES.BlockSize / 8);
36+
37+
AES.Mode = CipherMode.CBC;
38+
39+
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
40+
{
41+
cs.Write(bArray, 0, bArray.Length);
42+
cs.Close();
43+
}
44+
encryptedBytes = ms.ToArray();
45+
}
46+
}
47+
48+
49+
}
50+
51+
}
52+
}

0 commit comments

Comments
 (0)