-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
94 lines (88 loc) · 3.64 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
using System.Windows.Forms;
using System.Text;
namespace password_gen
//TODO: doubble klicking a line should select all the text
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonGen_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
buttonPrint.Enabled = false;
int items = Convert.ToInt32(numericUpDownItems.Value);
string result = "";
for (int i = 0; i < items; i++)
{
result += string.Format("{0}\r\n", GeneratePassword());
progressBar1.Maximum = items;
progressBar1.Value++;
Task.Delay(1);
}
textBoxGen.Text = result;
buttonPrint.Enabled = true;
}
private void buttonPrint_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text File|*.txt";
saveFileDialog1.Title = "Save a text File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
fs.Close();
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
for (int i = 1; i < textBoxGen.Lines.Length; i++)
{
int j = i;
sw.WriteLine(String.Format("{0}: {1}", i, textBoxGen.Lines[j - 1]));
}
}
}
buttonPrint.Enabled = false;
}
private string GeneratePassword()
{
string result = "";
string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string charWithNum = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string advnaced = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}:\"<>?|/*-,.;'[]=\\`";
string charWithSymb = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}:\"<>?|/*-,.;'[]=\\`";
Random random = new Random();
if((checkBox2.Checked == true) && (checkBox1.Checked == true) && (checkBox3.Checked == true))
{
for (int i = 0; i < numericUpDown1.Value; i++)
{
result += advnaced[random.Next(advnaced.Length)];
}
}
else if((checkBox2.Checked == true) && (checkBox1.Checked == true) && (checkBox3.Checked == false))
{
for (int i = 0; i < numericUpDown1.Value; i++)
{
result += charWithNum[random.Next(charWithNum.Length)];
}
}else if((checkBox2.Checked == true) && (checkBox1.Checked == false) && (checkBox3.Checked == false))
{
for (int i = 0; i < numericUpDown1.Value; i++)
{
result += characters[random.Next(characters.Length)];
}
}else if((checkBox2.Checked == true) && (checkBox1.Checked == false) && (checkBox3.Checked == true))
{
for(int i = 0; i < numericUpDown1.Value; i++)
{
result += charWithSymb[random.Next(charWithSymb.Length)];
}
}
return result;
}
}
}