-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormKeyGen.cs
More file actions
98 lines (85 loc) · 3.35 KB
/
FormKeyGen.cs
File metadata and controls
98 lines (85 loc) · 3.35 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C_Sharp_SFTP_Client
{
public partial class FormKeyGen : Form
{
public FormKeyGen()
{
InitializeComponent();
}
private void btnGenKey_Click(object sender, EventArgs e)
{
txtOutput.Text = "[STARTING]";
//define our command args
string command = "ssh-keygen";
string folderPath = txtDestination.Text;
string fileName = txtKeyName.Text;
string filePath = System.IO.Path.Combine(folderPath, fileName);
string arguments = $"-t rsa-sha2-256 -b 2048 -f \"{filePath}\" -N \"\""; ;
try
{
//create a new process - set the starting info
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = arguments,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
//start the new process
using(Process process = new Process())
{
process.StartInfo = processStartInfo;
process.Start();
//read the output
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
//display our output (or any errors)
txtOutput.Text += "[OUTPUT]: " + output + "\r\n";
string res = (string.IsNullOrEmpty(error)) ? "None!" : error;
txtOutput.Text += "[ERRORS]: " + res + "\r\n";
if(!string.IsNullOrEmpty(error))
{
//load up their private key
Utilities.LoadPrivateKeyFile(System.IO.Path.Combine(folderPath, fileName));
}
}
txtOutput.Text += "[INFO]: Both a 2048 bit Public and Private RSA key were created in the directory\r\nThe public key has the \".pub\"extension.";
}
catch (Exception ex)
{
txtOutput.Text += "[ERRORS]: " + ex.Message + "\r\n";
return;
}
}
private void formClosingEvent(object sender, FormClosedEventArgs e)
{
UserInfo._homeForm.Show();
this.Hide();
}
private void txtKeyNameChanged(object sender, EventArgs e)
{
//convert to lowercase and replace spaces with hyphens
string modified = txtKeyName.Text.ToLower().Replace(' ', '-');
//unsubscribe to avoid recursive calls
txtKeyName.TextChanged -= txtKeyNameChanged;
txtKeyName.Text = modified;
//set the cursor to the end of the text
txtKeyName.SelectionStart = txtKeyName.Text.Length;
//subscribe to the event again
txtKeyName.TextChanged += txtKeyNameChanged;
}
}
}