private void Form1_Load(object sender, EventArgs e) { //Thread th = new Thread(GetPortToPortList); GetPortToPortList(); listBox1.HorizontalScrollbar = true; DeEnCode dec = new DeEnCode(); dec.RSAKey(out privatekey, out publickey); string[] net = GetNetWork(); foreach (string ip in net) { comboBox1.Items.Add(ip); } comboBox1.SelectedIndex = 0; }
private void button4_Click(object sender, EventArgs e) { if (listBox1.Items == null) { MessageBox.Show(Warning.NotneedSave, "Warning", MessageBoxButtons.OK); return; } string neirongencode = string.Empty; foreach (string bb in listBox1.Items) { neirongencode = bb + "," + neirongencode; } neirongencode.Substring(0, neirongencode.Count() - 1); string hexkey = publickey; DeEnCode dec = new DeEnCode(); string encodething = dec.EncryptByRSA(neirongencode, hexkey); string base64encodeprivate = dec.Base64Code(privatekey); encodething = encodething + "&" + base64encodeprivate; SaveFileDialog sfd = new SaveFileDialog { Filter = "|*.txt" }; DialogResult result = sfd.ShowDialog(); if (result == DialogResult.OK) { DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(sfd.FileName)); DirectorySecurity ds = new DirectorySecurity(Path.GetDirectoryName(sfd.FileName), AccessControlSections.Access); object a = di.GetAccessControl(); //判断权限 if (ds.AreAccessRulesProtected) { MessageBox.Show(Warning.MayBeError, "Warning", MessageBoxButtons.OKCancel); } string path = sfd.FileName.ToString(); FileStream fs = (FileStream)sfd.OpenFile(); Encoder enc = Encoding.UTF8.GetEncoder(); char[] charData = encodething.ToCharArray(); byte[] byData = new byte[charData.Length]; enc.GetBytes(charData, 0, charData.Length, byData, 0, true); fs.Write(byData, 0, byData.Length); fs.Flush(); fs.Close(); MessageBox.Show("成功", "Success", MessageBoxButtons.OK); } }
private void button6_Click(object sender, EventArgs e) { try { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "|*.txt"; DialogResult dr = ofd.ShowDialog(); if (dr != DialogResult.OK) { MessageBox.Show(Warning.Errorfilechoose, "Error", MessageBoxButtons.OK); return; } FileStream fs = (FileStream)ofd.OpenFile(); byte[] readfilebyte = new byte[fs.Length]; fs.Read(readfilebyte, 0, (int)fs.Length); fs.Close(); Decoder dec = Encoding.UTF8.GetDecoder(); char[] charData = new char[readfilebyte.Length]; dec.GetChars(readfilebyte, 0, readfilebyte.Length, charData, 0); string neirong = new string(charData); string[] neirongandprivate = neirong.Split('&'); if (neirongandprivate.Count() != 2) { MessageBox.Show(Warning.Errorinputfile, "Error", MessageBoxButtons.OK); return; } DeEnCode decode = new DeEnCode(); string privatedecode = decode.Base64Decode(neirongandprivate[1]); neirong = decode.DecryptByRSA(neirongandprivate[0], privatedecode); List <string> peizhi = neirong.Split(',').ToList(); peizhi.RemoveAll(m => m == ""); int countall = peizhi.Count(); int countok = 0; WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); if (!principal.IsInRole(WindowsBuiltInRole.Administrator)) { MessageBox.Show(Warning.MustAdministrator, "Error", MessageBoxButtons.OKCancel); return; } foreach (string peizhione in peizhi) { List <string> getipandport = peizhione.Split(' ').ToList(); getipandport.RemoveAll(m => m == ""); Regex rg = new Regex("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"); if (!rg.IsMatch(getipandport[0]) || !rg.IsMatch(getipandport[2])) { continue; } if (!int.TryParse(getipandport[1], out int lport) || !int.TryParse(getipandport[3], out int rport)) { continue; } if (lport > 65535 || lport < 0 || rport > 65535 || rport < 0) { continue; } string comm = "netsh interface portproxy add v4tov4 listenaddress=" + getipandport[0] + " listenport=" + getipandport[1] + " connectaddress=" + getipandport[2] + " connectport=" + getipandport[3] + ""; RunScript(comm); countok++; } string mess = string.Empty; if (countall == countok) { mess = Warning.OtherCannotExp; } GetPortToPortList(); //MessageBox.Show("识别到" + countall + "条记录,\r\n" + countok + "条记录以添加," + mess, "Success", MessageBoxButtons.OK); MessageBox.Show(string.Format(Warning.AddError, new int[] { countall, countok }) + mess, "Success", MessageBoxButtons.OK); } catch { MessageBox.Show(Warning.NotKnowerrorFile, "Error", MessageBoxButtons.OK); return; } }