-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnigmaBreakerForm.cs
214 lines (179 loc) · 7.37 KB
/
EnigmaBreakerForm.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
using EPQProjectCMD;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EncryptionToolsGUI
{
public partial class EnigmaBreakerForm : FormWithText
{
public EnigmaBreakerForm(EnigmaForm parent)
{
InitializeComponent();
this.parent = parent;
rotorCountCB.SelectedIndex = 0;
//reflectorCB.SelectedIndex = 1;
}
private void enterTextButton_Click(object sender, EventArgs e)
{
enterTextButton();
}
EnigmaForm parent;
private void label2_Click(object sender, EventArgs e)
{
}
private void holdSizeChanged()
{
if (initHoldSizeTB.Text.Length == 0 || !initHoldSizeTB.Text.All(Char.IsDigit)
|| initHoldSizeTB.Text.All(x => x == '0'))
{
invalidHoldLabel.Visible = true;
}
else
{
invalidHoldLabel.Visible = false;
}
}
private void initHoldSizeTB_TextChanged(object sender, EventArgs e)
{
holdSizeChanged();
}
private void finalHoldSizeTB_TextChanged(object sender, EventArgs e)
{
holdSizeChanged();
}
/*private void fixedReflectorCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (fixedReflectorCheckBox.Checked)
reflectorCB.Enabled = true;
else
reflectorCB.Enabled = false;
}*/
private void findSolutionsButton_Click(object sender, EventArgs e)
{
rotorCount = rotorCountCB.SelectedIndex;
holdSize = Int32.Parse(initHoldSizeTB.Text);
plugCount = Int32.Parse(plugNumberTB.Text);
finalSize = holdSize / 2;
if (finalSize == 0)
finalSize = 1;
numOfEncryptions = Enigma.numOfEncryptionsToBreak(rotorCount + 3, holdSize, plugCount, finalSize);
if (!invalidHoldLabel.Visible && !invalidPlugNumberLabel.Visible)
{
progressBar1.Maximum = numOfEncryptions;
progressBar1.Step = 1;
progressBar1.Value = 0;
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = 0;
string text = CaesarAndVigenere.stripText(ciphertext).ToUpper();
int finalSize = holdSize / 2;
if (finalSize == 0)
finalSize = 1;
numOfEncryptions = Enigma.numOfEncryptionsToBreak(rotorCountCB.SelectedIndex + 3, Int32.Parse(initHoldSizeTB.Text),
Int32.Parse(plugNumberTB.Text), finalSize);
//System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
//watch.Stop();
//long elapsedMs = watch.ElapsedMilliseconds;
//Console.WriteLine((double)elapsedMs/1000.0);
/* dt.Columns.Add("Score", typeof(double));
* dt.Columns.Add("Positions", typeof(int[]));
* dt.Columns.Add("Rotors", typeof(int[]));
* dt.Columns.Add("Rings", typeof(int[]));
* dt.Columns.Add("Plugboard", typeof(char[][]));
*/
DataTable finalResultsDT = new DataTable();
finalResultsDT.Columns.Add("Score", typeof(double));
finalResultsDT.Columns.Add("Positions", typeof(string));
finalResultsDT.Columns.Add("Rotors", typeof(string));
finalResultsDT.Columns.Add("Rings", typeof(string));
finalResultsDT.Columns.Add("Plugboard", typeof(string));
finalResultsDT.Columns.Add("Text", typeof(string));
foreach (DataRow row in results.Rows)
{
DataRow newRow = finalResultsDT.NewRow();
newRow[0] = row[0];
newRow[1] = new string(((int[])row[1]).Select(x => (char)(x + 65)).ToArray());
newRow[2] = new string(((int[])row[2]).Select(x => (char)(x + 48)).ToArray());
newRow[3] = new string(((int[])row[3]).Select(x => (char)(x + 65)).ToArray());
StringBuilder plugBoard = new StringBuilder();
foreach (char[] plug in (char[][])row[4])
{
plugBoard.Append('[').Append(plug[0]).Append(", ").Append(plug[1]).Append("] ");
}
newRow[4] = plugBoard.ToString();
newRow[5] = (new Enigma((string)newRow[2], 'B', (string)newRow[1], (char[][])((char[][])row[4]).Clone(), (string)newRow[3])).encrypt(text);
finalResultsDT.Rows.Add(newRow);
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = finalResultsDT;
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);
dataGridView1.Columns["Text"].Width = 250;
}
private void plugNumberTB_TextChanged(object sender, EventArgs e)
{
if (plugNumberTB.Text.Length != 0 && plugNumberTB.Text.All(Char.IsDigit))
{
int plugNumber = Int32.Parse(plugNumberTB.Text);
if (plugNumber > 13)
{
invalidPlugNumberLabel.Visible = true;
largePlugCountWarningLabel.Visible = false;
}
else
{
invalidPlugNumberLabel.Visible = false;
if (plugNumber > 10)
largePlugCountWarningLabel.Visible = true;
else
largePlugCountWarningLabel.Visible = false;
}
}
else
{
invalidPlugNumberLabel.Visible = true;
largePlugCountWarningLabel.Visible = false;
}
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
//if (e.ColumnIndex != 5)
// return;
parent.launchEncrypt(dataGridView1.Rows[e.RowIndex], ciphertext);
parent.Focus();
}
public void Update(int i, string threadName)
{
Console.WriteLine("Called! ");
if (InvokeRequired)
{
BeginInvoke(new Action<int, string>(Update), new object[] { i, threadName });
return;
}
//label1.Text = threadName;
Console.WriteLine(+progressBar1.Value + ", " + (progressBar1.Value + i) + "; " + numOfEncryptions);
progressBar1.Value = Math.Min(progressBar1.Value + i, numOfEncryptions);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string text = CaesarAndVigenere.stripText(ciphertext).ToUpper();
results = Enigma.finalEnigma(text, rotorCount + 3, holdSize, plugCount,
finalSize, this);
Console.WriteLine("Done: " + results == null);
}
int rotorCount;
int holdSize;
int plugCount;
int finalSize;
int numOfEncryptions;
DataTable results;
}
}