-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
173 lines (155 loc) · 6.57 KB
/
Form1.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
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;
using lab1_Encryption_.Classes;
using lab1_Encryption_.Forms;
namespace lab1_Encryption_
{
public partial class Form1 : Form
{
ICryptographer Cryptographer;
protected byte[] encryptedData;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CryptographerControl = cryptographerControl1;
buttonEncrypt.Enabled = false;
buttonDecrypt.Enabled = false;
}
private void CryptographerControl_ValueChanged(object sender, EventArgs e)
{
var cryptoTypes = new Dictionary<Type, Action>
{
{
typeof(BitCryptographer),
() =>
{
var control = (BitCryptographerControl)CryptographerControl;
var cryptographer = (BitCryptographer)Cryptographer;
cryptographer.Bias = (int)control.numericUpDownBias.Value;
}
},
{
typeof(KeyCryptographer),
() =>
{
var control = (KeyCryptographerControl)CryptographerControl;
var cryptographer = (KeyCryptographer)Cryptographer;
cryptographer.Key = control.textBoxKey.Text;
}
},
{
typeof(GOSTCryptographer),
() =>
{
var control = (GOSTCryptographerControl)CryptographerControl;
var cryptographer = (GOSTCryptographer)Cryptographer;
cryptographer.Key = control.textBoxKey.Text;
}
},
{
typeof(RSACryptographer),
() =>
{
var control = (RSACryptographerControl)CryptographerControl;
var cryptographer = (RSACryptographer)Cryptographer;
var p = Convert.ToUInt64(control.textBoxP.Text);
var q = Convert.ToUInt64(control.textBoxQ.Text);
cryptographer.SetKeys(p, q);
var nValue = cryptographer.n;
var eValue = cryptographer.e;
control.textBoxN.Text = Convert.ToString(nValue);
control.textBoxE.Text = Convert.ToString(eValue);
}
}
};
cryptoTypes[Cryptographer.GetType()]();
}
private void ButtonEncrypt_Click(object sender, EventArgs e)
{
var data = Encoding.UTF8.GetBytes(textBoxOriginal.Text);
encryptedData = Cryptographer.Encrypt(data); // Храним зашифрованный текст в переменной, т.к. textbox стирает служебные символы.
textBoxEncrypted.Text = Encoding.ASCII.GetString(encryptedData.Where(x => x > 0 ).ToArray()); // +parse zeros ('\0' to '0'):
}
private void ButtonDecrypt_Click(object sender, EventArgs e)
{
var bytes = Cryptographer.Decrypt(encryptedData);
textBoxDecrypted.Text = Encoding.UTF8.GetString(bytes);
}
CryptographerControl CryptographerControl;
private void ComboBoxCryptographerType_SelectedIndexChanged(object sender, EventArgs e)
{
int index = comboBoxCryptographerType.SelectedIndex;
var cryptoTypes = new Dictionary<int, Func<ICryptographer>>
{
{
0,
() =>
{
var newControl = new BitCryptographerControl();
ReplaceControl(newControl);
newControl.numericUpDownBias.ValueChanged += CryptographerControl_ValueChanged;
return new BitCryptographer((int)newControl.numericUpDownBias.Value);
}
},
{
1,
() =>
{
var newControl = new KeyCryptographerControl();
ReplaceControl(newControl);
newControl.textBoxKey.TextChanged += CryptographerControl_ValueChanged;
return new KeyCryptographer(newControl.textBoxKey.Text);
}
},
{
2,
() =>
{
var newControl = new GOSTCryptographerControl();
ReplaceControl(newControl);
newControl.textBoxKey.TextChanged += CryptographerControl_ValueChanged;
return new GOSTCryptographer(newControl.textBoxKey.Text);
}
},
{
3,
() =>
{
var newControl = new RSACryptographerControl();
ReplaceControl(newControl);
newControl.textBoxP.TextChanged += CryptographerControl_ValueChanged;
newControl.textBoxQ.TextChanged += CryptographerControl_ValueChanged;
var p = Convert.ToUInt64(newControl.textBoxP.Text);
var q = Convert.ToUInt64(newControl.textBoxQ.Text);
var cryptographer = new RSACryptographer(p, q);
var nValue = cryptographer.n;
var eValue = cryptographer.e;
newControl.textBoxN.Text = Convert.ToString(nValue);
newControl.textBoxE.Text = Convert.ToString(eValue);
return cryptographer;
}
}
};
buttonEncrypt.Enabled = true;
buttonDecrypt.Enabled = true;
Cryptographer = cryptoTypes[index]();
}
protected void ReplaceControl(CryptographerControl newControl)
{
Controls.Remove(CryptographerControl);
newControl.Location = CryptographerControl.Location;
Controls.Add(newControl);
CryptographerControl = newControl;
}
}
}