-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
278 lines (243 loc) · 6.31 KB
/
Main.cs
File metadata and controls
278 lines (243 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System.Security.Cryptography;
namespace MuEncode;
public partial class Main : Form
{
public ErrorStream err;
public Main()
{
InitializeComponent();
WindowExpanded = false;
err = new ErrorStream(label_Note);
}
private void Form1_Load(object sender, EventArgs e)
{
label_Note.Text = "";
radio_Encode.Checked = true;
textbox_Input.PlaceholderText = "Input message to be encoded";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
label_Note.Text = "Easter Egg! ( )";
System.Media.SystemSounds.Beep.Play();
}
// When either radio button is checked, change the placeholder text
private void EncodeCheckedChanged(object sender, EventArgs e)
{
if (radio_Encode.Checked)
{
textbox_Input.PlaceholderText = "Type message to be encoded";
}
}
private void DecodeCheckedChanged(object sender, EventArgs e)
{
if (radio_Decode.Checked)
{
textbox_Input.PlaceholderText = "Type message to be decoded";
if (comboBox1.SelectedIndex == 3)
ExpandWindow();
}
}
/// <summary>
/// Main part of the program, runs when the "Run" button is clicked
/// </summary>
private void RunClicked(object sender, EventArgs e)
{
bool encode = radio_Encode.Checked;
label_Note.Text = "";
string output = textbox_Input.Text;
// check for empty input
if (string.IsNullOrEmpty(output.Trim()))
{
label_Note.Text = "Input is empty, please try again";
return;
}
if (comboBox1.SelectedIndex == -1)
{
label_Note.Text = "Please select a mode";
}
using (Encoder enc = new(err))
{
// if encode is checked
if (encode)
{
// morse code operations
if (comboBox1.SelectedIndex < 3)
{
if (comboBox1.SelectedIndex == 1)
output = enc.CharShift(output, encode);
if (comboBox1.SelectedIndex == 2)
output = enc.MultiShift(output, encode);
output = enc.MorseCode(output, encode, out bool invalidChar);
if (invalidChar)
InvalidCharError();
}
else
{
using Aes a = Aes.Create();
if (string.IsNullOrEmpty(textBox_IV.Text) || string.IsNullOrEmpty(textBox_Key.Text))
{
textBox_IV.Text = Convert.ToBase64String(a.IV);
textBox_Key.Text = Convert.ToBase64String(a.Key);
}
else
{
try
{
a.Key = Convert.FromBase64String(textBox_Key.Text);
a.IV = Convert.FromBase64String(textBox_IV.Text);
}
catch
{
label_Note.Text = "Key or IV invalid, new pair generated.";
textBox_IV.Text = Convert.ToBase64String(a.IV);
textBox_Key.Text = Convert.ToBase64String(a.Key);
}
}
output = enc.AES(output, a.Key, a.IV, encode);
}
}
// if decode is checked
else
{
if (comboBox1.SelectedIndex < 3)
{
output = enc.MorseCode(output, encode, out bool invalidChar);
if (comboBox1.SelectedIndex == 1)
output = enc.CharShift(output, encode);
if (comboBox1.SelectedIndex == 2)
output = enc.MultiShift(output, encode);
if (invalidChar)
InvalidCharError();
}
else
{
try
{
byte[] key = Convert.FromBase64String(textBox_Key.Text);
byte[] IV = Convert.FromBase64String(textBox_IV.Text);
output = enc.AES(output, key, IV, encode);
}
catch (FormatException)
{
label_Note.Text = "Incorrect cipher";
}
}
}
}
textbox_Output.Text = output;
if (checkBox_Clip.Checked)
Clipboard.SetText(textbox_Output.Text);
}
// When the state of clipboard checkbox changes, if it is checked copy output to clipboard.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_Clip.Checked && string.IsNullOrWhiteSpace(textbox_Output.Text) == false)
{
Clipboard.SetText(textbox_Output.Text);
label_Note.Text = "Output copied!";
}
}
// When 'clear' button is clicked, sets the output text to null.
private void button_ClearClick(object sender, EventArgs e)
{
textbox_Output.Text = "";
}
/// <summary>
/// Quick shorthand for when user has inputted invalid characters.
/// Changes the 'note' to an error message and plays asterisk.
/// </summary>
private void InvalidCharError()
{
label_Note.Text = "Encountered invalid character(s)";
System.Media.SystemSounds.Asterisk.Play();
}
private void link_ClearInput_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
textbox_Input.Text = "";
}
#region Window Manipulation Functions
private bool WindowExpanded;
private void ExpandWindow()
{
if (!WindowExpanded)
{
this.Width += 245;
textbox_Output.Width += 245;
button_Run.Left += 245;
comboBox1.Left += 245;
button_ClearOutput.Left += 245;
textBox_IV.Visible = true;
textBox_IV.Left += 260;
textBox_IV.Width += 100;
textBox_Key.Visible = true;
textBox_Key.Left += 260;
textBox_Key.Width += 100;
label_IV.Visible = true;
label_IV.Left += 260;
label_Key.Visible = true;
label_Key.Left += 260;
label_ClearPair.Visible = true;
label_ClearPair.Left += 260;
WindowExpanded = true;
}
}
private void ShrinkWindow()
{
if (WindowExpanded)
{
this.Width -= 245;
textbox_Output.Width -= 245;
button_Run.Left -= 245;
comboBox1.Left -= 245;
button_ClearOutput.Left -= 245;
textBox_IV.Visible = false;
textBox_IV.Left -= 260;
textBox_IV.Width -= 100;
textBox_Key.Visible = false;
textBox_Key.Left -= 260;
textBox_Key.Width -= 100;
label_IV.Visible = false;
label_IV.Left -= 260;
label_Key.Visible = false;
label_Key.Left -= 260;
label_ClearPair.Visible = false;
label_ClearPair.Left -= 260;
WindowExpanded = false;
}
}
#endregion
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 3)
{
ExpandWindow();
}
else if (this.Width > 500)
{
ShrinkWindow();
}
}
private void label_Key_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox_Key.Text))
{
Clipboard.SetText(textBox_Key.Text);
label_Note.Text = "Key Copied!";
}
}
private void label_IV_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBox_IV.Text))
{
Clipboard.SetText(textBox_IV.Text);
label_Note.Text = "IV Copied!";
}
}
private void label_ClearPair_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
textBox_IV.Text = ""; textBox_Key.Text = "";
}
private void textbox_Input_TextChanged(object sender, EventArgs e)
{
}
}