-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
230 lines (188 loc) · 5.99 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
namespace Enigma
{
public class Rotor
{
private const Int32 MAX = 3;
public Int32 X;
public Int32 Y;
public Int32 Z;
private Int32 _sum;
public Int32 Sum
{
get { return _sum = X + Y + Z; }
}
public Int32 Max
{
get { return MAX; }
}
public void SetRotors(Int32 Rotor1, Int32 Rotor2, Int32 Rotor3)
{
this.X = Rotor1;
this.Y = Rotor2;
this.Z = Rotor3;
}
public void IncrementRotors()
{
X++;
// Increment rotors
if (X > MAX)
{
X = 1;
Y++;
}
if (Y > MAX)
{
Y = 1;
Z++;
}
if (Z > MAX)
{
Z = 1;
}
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// Initialize the rotor object
Rotor r = new Rotor();
public MainWindow()
{
InitializeComponent();
// Populate rotor list from 1-26
for (int i = 1; i <= r.Max; i++)
{
R1.Items.Add(i.ToString());
R2.Items.Add(i.ToString());
R3.Items.Add(i.ToString());
}
// First index should be selected by default
// Possible modification: random index by default
R1.SelectedIndex = 0;
R2.SelectedIndex = 0;
R3.SelectedIndex = 0;
}
private void Encrypt(TextBox txtDecrypted)
{
Char[] message = txtDecrypted.Text.ToCharArray();
Int32 alphaIndex = 0;
Boolean isSpace = false;
Int32 alphaShifted = 0;
Int32 shift = 0;
String encrypted = "";
foreach (char c in message)
{
alphaIndex = (char.ToUpper(c) - 64);
shift = r.Sum;
if (alphaIndex < 0)
isSpace = true;
alphaShifted = (alphaIndex + shift) + 64;
if (alphaShifted > 90)
alphaShifted -= 26;
if (alphaShifted < 65)
alphaShifted += 26;
if (isSpace)
alphaShifted = 95;
encrypted += char.ToString((char)alphaShifted);
r.IncrementRotors();
isSpace = false;
// Write output
// Debug.WriteLine(alphaIndex.ToString() + " + " + shift.ToString() + " = " + (alphaShifted).ToString());
}
txtEncrypted.Text = encrypted.ToString();
}
private void Decrypt(TextBox txtEncrypted)
{
Char[] message = txtEncrypted.Text.ToCharArray();
Int32 alphaIndex = 0;
Boolean isSpace = false;
Int32 alphaShifted = 0;
Int32 shift = 0;
String encrypted = "";
foreach (char c in message)
{
alphaIndex = (char.ToUpper(c) - 64);
shift = r.Sum;
if (alphaIndex == 31)
isSpace = true;
alphaShifted = (alphaIndex - shift) + 64;
if (alphaShifted > 90)
alphaShifted -= 26;
if (alphaShifted < 65)
alphaShifted += 26;
if (isSpace)
alphaShifted = 32;
encrypted += char.ToString((char)alphaShifted);
r.IncrementRotors();
isSpace = false;
// Write output
//Debug.WriteLine(alphaIndex.ToString() + " - " + shift.ToString() + " = " + (alphaShifted - 64).ToString() + " (" + (alphaShifted).ToString() +" = '" + (char)alphaShifted + "')");
}
txtDecrypted.Text = encrypted.ToString();
}
private void UpdateRotorUI()
{
R1.SelectedIndex = r.X - 1;
R2.SelectedIndex = r.Y - 1;
R3.SelectedIndex = r.Z - 1;
}
private void WriteDebuggingInfo()
{
// Show Rotor values
Debug.WriteLine(r.X.ToString() + " " + r.Y.ToString() + " " + r.Z.ToString() + " = " + r.Sum);
//Debug.WriteLine("----");
}
// Click Events
private void btnEncrypt_Click(object sender, RoutedEventArgs e)
{
Encrypt(txtDecrypted);
UpdateRotorUI();
}
private void btnDecrypt_Click(object sender, RoutedEventArgs e)
{
Decrypt(txtEncrypted);
UpdateRotorUI();
}
private void btnSetRotor_Click(object sender, RoutedEventArgs e)
{
Int32[] rotors = new Int32[]
{
Int32.Parse(R1.SelectedItem.ToString()),
Int32.Parse(R2.SelectedItem.ToString()),
Int32.Parse(R3.SelectedItem.ToString())
};
// Initialize rotor values
r.SetRotors(rotors[0], rotors[1], rotors[2]);
// For testing purposes:
/*
for (int i = 1; i <= 28; i++)
{
WriteDebuggingInfo();
r.IncrementRotors();
}
*/
}
private void lblHelp_MouseUp(object sender, MouseButtonEventArgs e)
{
Help frm = new Help();
frm.Show();
}
}
}