-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKGen.cs
More file actions
170 lines (150 loc) · 6.26 KB
/
KGen.cs
File metadata and controls
170 lines (150 loc) · 6.26 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
// генерация
using System;
using System.IO;
using System.Windows.Forms;
using System.Numerics;
using System.Text;
using System.Security.Cryptography;
using ECClasses;
namespace ECCryptoSystem
{
public partial class KeysGen : Form
{
public uint size;
public string pass;
public KeysGen()
{
InitializeComponent();
size = 0;
pass = "";
}
private BigInteger GeneratePrivateKey(ECurve c)
{
return RandomBigInt.randomBigInteger(c);
}
private void EncryptPrivateKey(BigInteger PrivKey, string filename)
{ // шифрование закрытого ключа
byte[] key_data = Encoding.Unicode.GetBytes(PrivKey.ToString());
byte[] code_data = Encoding.Unicode.GetBytes("SpecSignature " + size + " "); // сигнатура (для проверки правильности введенной парольной фразы)
byte[] keyPlusCode_data = new byte[key_data.Length + code_data.Length];
code_data.CopyTo(keyPlusCode_data, 0);
key_data.CopyTo(keyPlusCode_data, code_data.Length);
Rfc2898DeriveBytes cryptoKey = new Rfc2898DeriveBytes(pass, 8, 1000, HashAlgorithmName.SHA256);
byte[] salt = cryptoKey.Salt; // криптографический ключ строится на основе парольной фразы и хэш-алгоритма
AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); // используемый для шифрования симметричный алгоритм
aes.Key = cryptoKey.GetBytes(32);
aes.Mode = CipherMode.ECB;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, null);
FileStream foutStream = new FileStream(filename, FileMode.Create);
foutStream.Write(salt, 0, 8); // запись соли в начало файла
CryptoStream CrStream = new CryptoStream(foutStream, encryptor,
CryptoStreamMode.Write);
CrStream.Write(keyPlusCode_data, 0, keyPlusCode_data.Length);
// очистка памяти с конфиденциальными данными
aes.Clear();
// закрытие потока шифрования
CrStream.Close();
// закрытие файлов
foutStream.Close();
}
private EPoint GeneratePublicKey(EPoint e, BigInteger PrivKey)
{
return EPoint.ECMultiply(e, PrivKey);
}
private void Save_Click(object sender, EventArgs e)
{
try
{
/* выбор кривой */
ECurve crv = new ECurve(size);
/* генерация закрытого ключа */
BigInteger privKey = GeneratePrivateKey(crv);
/* вычисление открытого ключа */
EPoint pubKey = GeneratePublicKey(crv.G, privKey);
/* шифрование закрытого ключа */
EncryptPrivateKey(privKey, private_key.Text);
/* запись открытого ключа */
File.WriteAllText(open_key.Text, pubKey.X.ToString() + " " + pubKey.Y.ToString());
private_key.Text = open_key.Text = "";
l1.Text = "Готово!";
Save.Enabled = false;
}
catch (CryptographicException ex)
{
MessageBox.Show(ex.Message, "Окно будет закрыто...", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Окно будет закрыто...", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Close();
}
}
private void sz_Click(object sender, EventArgs e)
{
//
Save.Enabled = false;
KeySize f5 = new KeySize();
f5.f57 = this;
if (f5.ShowDialog() == DialogResult.Cancel) {
Save.Enabled = true;
}
}
private void file2_Click(object sender, EventArgs e)
{
//
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)
{
PassSet f3 = new PassSet();
f3.f37 = this;
f3.ShowDialog();
//
private_key.Text = saveFileDialog1.FileName;
}
}
private void File1_Click(object sender, EventArgs e)
{
//
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)
{
open_key.Text = saveFileDialog1.FileName;
}
}
private void open_key_TextChanged(object sender, EventArgs e)
{
//
sz.Enabled = !string.IsNullOrEmpty(open_key.Text) &&
!string.IsNullOrEmpty(private_key.Text);
}
private void private_key_TextChanged(object sender, EventArgs e)
{
//
sz.Enabled = !string.IsNullOrEmpty(open_key.Text) &&
!string.IsNullOrEmpty(private_key.Text);
}
}
}