-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt.cs
More file actions
127 lines (108 loc) · 4.74 KB
/
Encrypt.cs
File metadata and controls
127 lines (108 loc) · 4.74 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
// шифрование
using ECClasses;
using System;
using System.Numerics;
using System.Linq;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace ECCryptoSystem
{
public partial class Encryption : Form
{
public Encryption()
{
InitializeComponent();
}
private uint[] keysizes = new uint[4] { 192, 256, 384, 521}; // валидные размеры ключей
private void File1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = @"D:\",
Title = "Найти",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "txt",
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 2,
RestoreDirectory = true //,
//ReadOnlyChecked = true,
//ShowReadOnly = true
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
open_key.Text = openFileDialog1.FileName;
}
}
private void open_key_TextChanged(object sender, EventArgs e)
{
//
Crypt.Enabled = !string.IsNullOrEmpty(open_key.Text);
}
private void Crypt_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(msg.Text))
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog()
{
InitialDirectory = @"D:\",
Title = "Сохранить шифротекст",
CheckFileExists = false,
CheckPathExists = false,
DefaultExt = "txt",
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 2,
RestoreDirectory = true
};
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
shifr.Text = saveFileDialog1.FileName;
try
{
string[] pubKeyStr = File.ReadAllText(open_key.Text).Split(' '); // читаем открытый ключ
(BigInteger x, BigInteger y) = (BigInteger.Parse(pubKeyStr[0]), BigInteger.Parse(pubKeyStr[1]));
int k = x.ToByteArray().Length * 8;
var size = keysizes.OrderBy(z => Math.Abs(z - k)).First();
/* выбор кривой */
ECurve crv = new ECurve(size);
EPoint Qa = new EPoint(x, y, crv);
/* генерация случайного числа */
BigInteger r = RandomBigInt.randomBigInteger(crv);
/* вычисление rG */
EPoint rG = EPoint.ECMultiply(crv.G, r);
/* вычисление S */
EPoint S = EPoint.ECMultiply(Qa, r);
/* преобразование текста сообщения m в число P */
byte[] m = Encoding.UTF8.GetBytes(msg.Text);
BigInteger P = new BigInteger(m);
/* вычисление шифротекста */
BigInteger Cm = (S.X + P) % crv.N;
/* запись шифротекстов */
File.WriteAllText(shifr.Text, Cm.ToString());
File.WriteAllText(shifr.Text.Replace(".", "RG."), rG.X.ToString() + " " + rG.Y.ToString());
l2.Text = "Готово!";
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, "Окно будет закрыто...", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Окно будет закрыто...", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Close();
}
}
}
else
{
MessageBox.Show("Введите сообщение!", "Wrong message",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}