-
Notifications
You must be signed in to change notification settings - Fork 14
/
FormAdd.cs
264 lines (232 loc) · 9.16 KB
/
FormAdd.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace Coffeed
{
public partial class FormAdd : Form
{
public string _privateKey;
public string _publicKey;
private static UnicodeEncoding _encoder = new UnicodeEncoding();
public List<Account> DB = new List<Account>();
int _MODIFY_INDEX;
public FormAdd(int modifyIndex = -99)
{
InitializeComponent();
try
{
_publicKey = Application.StartupPath + @"\PublicKey.xml";
_privateKey = Properties.Settings.Default.privkeypath;
_MODIFY_INDEX = modifyIndex;
if (File.Exists(Path.Combine(Application.StartupPath,"DATA.dat"))) DB = LoadDB();
if (modifyIndex != -99)
{
frmAddGroup add_group = new frmAddGroup();
frmAddGroup.RefreshGroups();
groupBox.Items.Clear();
groupBox.Items.AddRange(frmAddGroup.groups.Split(','));
typeBox.SelectedItem = Decrypt(DB[modifyIndex].Type);
groupBox.SelectedItem = Decrypt(DB[modifyIndex].Group);
txtFriendly.Text = Decrypt(DB[modifyIndex].Name);
txtIP.Text = Decrypt(DB[modifyIndex].IP);
txtUser.Text = Decrypt(DB[modifyIndex].User);
txtPass.Text = Decrypt(DB[modifyIndex].Pass);
txtPort.Text = Decrypt(DB[modifyIndex].Port);
if (Decrypt(DB[modifyIndex].SFTP).ToLowerInvariant() == "true")
{
checkFTP.Checked = true;
}
else
{
checkFTP.Checked = false;
}
//checkFTP.Enabled = bool.Parse());
}
}
catch (Exception err)
{
Logging.M(err.Message, "Oops, there's an error that needs special attetion.");
Logging.LogError(err);
}
}
private void Form2_Load(object sender, EventArgs e)
{
_publicKey = Application.StartupPath + @"\PublicKey.xml";
_privateKey = Properties.Settings.Default.privkeypath;
groupBox.Items.AddRange(frmAddGroup.groups.Split(','));
}
public string Decrypt(string data)
{
try
{
var rsa = new RSACryptoServiceProvider();
var dataArray = data.Split(new char[] { ',' });
byte[] dataByte = new byte[dataArray.Length];
for (int i = 0; i < dataArray.Length; i++)
{
dataByte[i] = Convert.ToByte(dataArray[i]);
}
rsa.FromXmlString(File.ReadAllText(_privateKey));
var decryptedByte = rsa.Decrypt(dataByte, false);
return _encoder.GetString(decryptedByte);
}
catch (Exception err)
{
Logging.M(err.Message, "Oops, there's an error that needs special attetion.");
Logging.LogError(err);
return string.Empty;
}
}
public string Encrypt(string data)
{
try
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(System.IO.File.ReadAllText(_publicKey));
var dataToEncrypt = _encoder.GetBytes(data);
var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
var length = encryptedByteArray.Count();
var item = 0;
var sb = new StringBuilder();
foreach (var x in encryptedByteArray)
{
item++;
sb.Append(x);
if (item < length)
sb.Append(",");
}
return sb.ToString();
}
catch (Exception err)
{
Logging.M(err.Message, "Oops, there's an error that needs special attetion.");
Logging.LogError(err);
return string.Empty;
}
}
/*
* MessageBox.Show(Encrypt(textBox1.Text));
* Clipboard.SetText(Encrypt(textBox1.Text));
*/
private bool c(string s)
{
bool havingstring = false;
havingstring = !string.IsNullOrEmpty(s);
return havingstring;
}
private void button1_Click(object sender, EventArgs e)
{
if (c(typeBox.SelectedIndex.ToString()) && c(txtFriendly.Text) && c(txtIP.Text) && c(txtUser.Text) && c(txtPass.Text) && c(txtFriendly.Text) && c(txtPort.Text) && c(groupBox.SelectedItem.ToString()))
{
// MODIFY
if (_MODIFY_INDEX != -99)
{
DB[_MODIFY_INDEX].Type = Encrypt(typeBox.SelectedItem.ToString());
DB[_MODIFY_INDEX].Name = Encrypt(txtFriendly.Text);
DB[_MODIFY_INDEX].IP = Encrypt(txtIP.Text);
DB[_MODIFY_INDEX].User = Encrypt(txtUser.Text);
DB[_MODIFY_INDEX].Pass = Encrypt(txtPass.Text);
DB[_MODIFY_INDEX].Port = Encrypt(txtPort.Text);
DB[_MODIFY_INDEX].SFTP = Encrypt(checkFTP.Checked.ToString().ToLowerInvariant());
DB[_MODIFY_INDEX].Group = Encrypt(groupBox.SelectedItem.ToString());
}
// ADD NEW
else
{
var x = new Account();
x.Type = Encrypt(typeBox.SelectedItem.ToString());
x.Name = Encrypt(txtFriendly.Text);
x.IP = Encrypt(txtIP.Text);
x.User = Encrypt(txtUser.Text);
x.Pass = Encrypt(txtPass.Text);
x.Port = Encrypt(txtPort.Text);
x.SFTP = Encrypt(checkFTP.Checked.ToString().ToLowerInvariant());
x.Group = Encrypt(groupBox.SelectedItem.ToString());
DB.Add(x);
}
SaveDB(DB);
this.Close();
}
else
{
MessageBox.Show("One or more fields are empty, please consider to fill them.", "Oops, fields needs atettion!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private List<Account> LoadDB()
{
try
{
BinaryFormatter bf = new BinaryFormatter();
return (List<Account>)bf.Deserialize(new MemoryStream(File.ReadAllBytes(Path.Combine(Application.StartupPath,"DATA.dat"))));
}
catch (Exception err)
{
Logging.M(err.Message, "Oops, there's an error that needs special attetion.");
Logging.LogError(err);
return new List<Account>();
}
}
internal void SaveDB(List<Account> dbdata)
{
try
{
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, dbdata);
File.WriteAllBytes(Path.Combine(Application.StartupPath,"DATA.dat"), ms.ToArray());
}
}
catch (Exception err)
{
Logging.M(err.Message, "Oops, there's an error that needs special attetion.");
Logging.LogError(err);
return;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (typeBox.SelectedItem.ToString() == "FileZilla")
{
checkFTP.Visible = true;
checkFTP.Checked = true;
label5.Visible = true;
txtPort.Visible = true;
}
else if (typeBox.SelectedItem.ToString() == "RDP")
{
label5.Visible = false;
txtPort.Visible = false;
checkFTP.Visible = false;
}
else
{
checkFTP.Visible = false;
label5.Visible = true;
txtPort.Visible = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
frmAddGroup add_group = new frmAddGroup();
add_group.ShowDialog();
frmAddGroup.RefreshGroups();
groupBox.Items.Clear();
groupBox.Items.AddRange(frmAddGroup.groups.Split(','));
}
private void button3_Click(object sender, EventArgs e)
{
string strongPassword = System.Web.Security.Membership.GeneratePassword(18, 1);
txtPass.Text = strongPassword;
}
private void button4_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtPass.Text);
}
}
}