private void button1_Click(object sender, EventArgs e) { #region 检测IP是否合法并将合法IP端口信息写入ipMsg.txt文件中 IPAddress myIP; string ip = textBox1.Text.Trim() + "." + textBox2.Text.Trim() + "." + textBox3.Text.Trim() + "." + textBox4.Text.Trim(); string port = textBox5.Text.Trim(); if (IPAddress.TryParse(ip, out myIP)) { MessageBox.Show("当前IP为:" + ip + "\n\n" + "端口号为:" + port, "重置IP成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); string ipStr = textBox1.Text.Trim() + "." + textBox2.Text.Trim() + "." + textBox3.Text.Trim() + "." + textBox4.Text.Trim() + ":" + textBox5.Text.Trim(); //自己写的文件读写类 file wd = new file(); //writer()函数第一个参数为文件名字、第二个为要写如文件的名字 // wd.writer("..\\..\\..\\..\\ipMsg.txt", ipStr); wd.writer("ipMsg.txt", ipStr); } else { MessageBox.Show("IP非法!请重新设置"); } #endregion }
/// <summary> /// 从socket中读取数据 /// </summary> public static void ReciveMsg() { try { //循环读取 while (true) { pripk = name + "pri.txt"; //当前用户的私钥 ppk = name + "pk.txt"; //当前用户的公钥 int length = 0; //字节数组 byte[] byteMsg = new byte[1024 * 1024 * 4]; //获取字节数组大小 length = sokClient.Receive(byteMsg); //获取有用的从0~length个 strMsg = Encoding.UTF8.GetString(byteMsg, 0, length); // MessageBox.Show(strMsg); Thread thread2 = new Thread(new ThreadStart(ShowMsgForm)); //创建新线程 thread2.IsBackground = true; string[] sstr = strMsg.Split('#'); //利用“#”作为消息的分割 //数组长度 int x = sstr.Length; //标记数据类型,当falg>1是为用户登录信息 //falg为数组中消息的个数 int msnumber = 0; for (int j = 0; j < x; j++) { if (sstr[j] != "") { msnumber++; } } if (sstr[0] == name) { cl.label10.ForeColor = Color.Red; cl.label10.Text = "当前序列中有你!"; cl.tslang.Text = "正在进行签名任务,暂时无法重新生成公私钥!"; cl.button1.Enabled = false; } #region 一系列的登录检测 if (sstr[0] == "正在登录!") { //Thread.Sleep(100); //MessageBox.Show(sstr[0] + "\r\n" + sstr[1]); file file = new file(); desKey des = new desKey(); //进过des加密后写入本地server.txt文件下 file.writer("server.txt", des.Encrypt(sstr[1], "abcdefgh")); thread2.Start(); } if (strMsg == "用户名尚未注册!") { // MessageBox.Show( sokClient.Shutdown(SocketShutdown.Both); sokClient.Disconnect(true); //用户没有注册,从新启动客户端 Application.Restart(); } //首次登陆关键信息的注册 // 添加进度提示 //1.提交cpu、硬盘的序列号 //2.生成公钥对、和私钥(都为16进制64位) if (strMsg == "系统检测到用户首次登陆,正在注册关键信息请稍等!")//加入进度条指示! { //生成公私钥,存入指定的文件夹下 ecc.Creatkey(sm2, pripk, ppk);//创建密钥 //根据公私钥生成rs string gykey = ""; ecc.Test_sm2_sign(sm2, pripk, ppk, name); file file = new file(); try { gykey = des.Decrypt(file.reader(ppk), "abcdefgh");//读取客户端公钥,记着des解密 } catch (Exception dd) { MessageBox.Show(dd.ToString()); } Form1 ff = new Form1(); client.upmsg("重新生成用户公钥对" + "#" + gykey + "#" + client.name + "#@#@#@");//将用户公钥发给服务器 fistLog fist = new fistLog(); fist.ShowDialog(); thread2.Start(); } if (strMsg == "服务器检测到,当前配置与注册不符") { MessageBox.Show("服务器检测到,当前电脑配置与注册信息不符\n\n无法完成登录!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Restart(); } #endregion //利用#作为分隔符, /* sstr[0]位用户名, * sstr[1]位服务器的R, * sstr[2]位服务器的S , * sstr[3]为Z, * sstr[4]为c1 * sstr[5]位c2, * sstr[6]位c3, * sstr[7]为时间, * sstr[8]位签名发起人标记 */ if (msnumber == 9) { // MessageBox.Show(sstr[0] + "\r\n" + sstr[1] + "\r\n" + sstr[2] + "\r\n" + sstr[3] + "\r\n" + sstr[4] + "\r\n" + sstr[5] + "\r\n" + sstr[6] + "\r\n" + sstr[7] + "\r\n" + sstr[8] + "\r\n"); //设置签名消息 file file = new file(); //ecc ecc = new ecc(); string pk = des.Decrypt(file.reader(serverpk), "abcdefgh");//读取并解密des加密的服务器公钥 //签名验证 if (client.ecc.Signature_Check(sm2, pk, sstr[3], sstr[1], sstr[2]) == true) { if (sstr[0] == name) { cl.noSignBut.Text = "拒绝签名"; cl.noSignBut.Enabled = true; cl.button2.Enabled = true; } string jmxx = client.ecc.deciphering(sm2, pripk, sstr[4], sstr[5], sstr[6]); if (jmxx != "0") {//倒计时设置 cl.waitBut.Text = jmxx; if (sstr[7].StartsWith("时间") == true) { //MessageBox.Show("时间:\r\n"+sstr[7].ToString()); int len = sstr[7].Length; string s = ""; if (len == 0) { s += sstr[7].Trim()[2]; } if (2 <= len) { s += sstr[7].Trim()[2]; } cl.time1 = Int32.Parse(s) - 1; cl.ss = 59; } } else { MessageBox.Show("数据校验失败!"); } } else { MessageBox.Show("验证不通过!!"); } } #region 第一次接受签名 if (msnumber == 4) { //利用'|'做分割符,sstr[0]为用户名,sstr[1]为签名消息,sstr[2]为时间,sstr[3]代表是否为发起签名的人 //MessageBox.Show(sstr[0] + "\n\n" + sstr[1]); if (sstr[0] == name) { cl.noSignBut.Text = "拒绝签名"; cl.noSignBut.Enabled = true; cl.button2.Enabled = true; } //设置签名消息 cl.waitBut.Text = sstr[1].ToString(); //倒计时设置 if (sstr[2].StartsWith("时间") == true) { int len = sstr[2].Length; string s = ""; if (len == 0) { s += sstr[2].Trim()[2]; } if (2 <= len) { s += sstr[2].Trim()[2]; } cl.time1 = Int32.Parse(s) - 1; cl.ss = 59; } //sstr[3]代表此用户是否为签名的第一个人,由他填写签名内容。“1”代表是签名发起者 if (sstr[3] == "1") { //设置为可写 cl.waitBut.ReadOnly = false; } } #endregion } } catch (SocketException)//服务器离线异常捕获 { //回报服务器下线 cl.label7.ForeColor = Color.Red; cl.label7.Text = "离线"; if (MessageBox.Show("服务器关闭了链接!!\n\n\n是否重新登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { Application.Restart();//服务器关闭后,从新回到登录页面 } else { return; } } catch (Exception e) { MessageBox.Show("客户端“Form1”异常 2:" + e.Message); return; } }
private void setIP_Load(object sender, EventArgs e) { #region IP和端口信息的读取和档文件ipMsg.txt不存在时的初始化 //读取ipMsg.txt中的IP配置 try { string a1, a2, a3, a4, port; a1 = a2 = a3 = a4 = port = ""; int b1, b2, b3, b4, i; b1 = b2 = b3 = b4 = i = 0; //自己写的文件读写类 file re = new file(); //reader()为返回从指定文件都出来的数据,其参数为文件路径 //string read = re.reader("..\\..\\..\\..\\ipMsg.txt"); // string read = re.reader("ipMsg.txt"); int length = read.Length; while (true) { if (b1 == 0 && b2 == 0 && b3 == 0) { if (read[i].ToString() == ".") { b1 = 1; i++; } else { a1 += read[i]; } } if (b1 == 1 && b2 == 0 && b3 == 0) { if (read[i].ToString() == ".") { b2 = 1; i++; } else { a2 += read[i]; } } if (b1 == 1 && b2 == 1 && b3 == 0) { if (read[i].ToString() == ".") { b3 = 1; i++; } else { a3 += read[i]; } } if (b1 == 1 && b2 == 1 && b3 == 1 && b4 == 0) { if (read[i].ToString() == ":") { b4 = 1; i++; } else { a4 += read[i]; } } if (b1 == 1 && b2 == 1 && b3 == 1 && b4 == 1) { if (i == length) { break; } else { port += read[i]; } } i++; } textBox1.Text = a1; textBox2.Text = a2; textBox3.Text = a3; textBox4.Text = a4; textBox5.Text = port; } //当文件不存在的时候新建初并始化ipMsg.txt catch { string ipStr = textBox1.Text.Trim() + "." + textBox2.Text.Trim() + "." + textBox3.Text.Trim() + "." + textBox4.Text.Trim() + ":" + textBox5.Text.Trim(); //自己写的文件读写类 file wd = new file(); //writer()函数第一个参数为文件名字、第二个为要写如文件的名字 wd.writer("ipMsg.txt", ipStr); //wd.writer("..\\..\\..\\..\\ipMsg.txt", ipStr); } #endregion }