Exemplo n.º 1
0
 private void regBut_Click(object sender, EventArgs e)
 {
     if (textBox1.Text.Trim() != "")
     {
         file     file = new file();
         string   f    = file.reader("ipMsg.txt"); //读取保存在本地的IP
         string[] sstr = f.Split(':');             //利用":"分割IP和端口
         this.WindowState = FormWindowState.Minimized;
         this.Hide();
         client.Connecting(sstr[0], sstr[1]);    //启动服务器
         client.clientSER(textBox1.Text.Trim()); //发送登录消息
         client.lod.ShowDialog();                //弹出来个虚假的登录提示条
     }
     else
     {
         MessageBox.Show("错误!\r\n\r\n账号不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 2
0
        private void jdtiao_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value++;
                //label3.Text = progressBar1.Maximum.ToString();
                double f = Math.Round((((double)progressBar1.Value / (double)progressBar1.Maximum) * 100), 0);

                if (f < 50)
                {
                    label2.Text = "正在加密";
                }
                if (f < 65 && f > 55)
                {
                    label2.Text = "加密完成";
                }
                if (f >= 85)
                {
                    label2.Text = "正在发送!";
                }
                if (f == 100)
                {
                    Form1 ff = new Form1();

                    //ecc ecc = new ecc();
                    file   file = new file();
                    desKey des  = new desKey();
                    //公钥加密:利用服务器的公钥加密
                    c = client.ecc.Test_sm2_cipher(client.sm2, waitBut.Text.Trim(), "server.txt");

                    //数字签名算法:利用公私钥和自己的身份标识进行生成签名
                    z = client.ecc.Test_sm2_sign(client.sm2, client.pripk, client.ppk, client.name);

                    rs = file.reader(client.name + "rs.txt");

                    client.upmsg("*#*#" + c[0] + "#" + c[1] + "#" + c[2] + "#" + z + "#" + rs + "#" + client.name + "#" + DateTime.Now.ToString());
                }
                label1.Text = f.ToString() + "%";
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 经过des加密后、写入秘钥(用户名pri.txt)
        /// </summary>
        /// <param name="userD">秘钥</param>
        /// <param name="file">要写入的文件名</param>
        public void WriterKey(BigInteger userD, string file)
        {
            try
            {
                string     path = file;
                FileStream Ali;
                file       fi  = new file();
                desKey     des = new desKey();

                Ali = new FileStream(path, FileMode.Create, FileAccess.Write); //创建写入文件
                StreamWriter sr = new StreamWriter(Ali);
                string       ss = des.Encrypt(userD.ToString(16), "abcdefgh"); //des加密,"abcdefgh"为加密随机数

                sr.WriteLine(ss);                                              //写入文件


                sr.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show("ecc类异常 1\r\n" + ee.ToString());
            }
        }
Exemplo n.º 4
0
 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
 }
Exemplo n.º 5
0
        /// <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;
            }
        }
Exemplo n.º 6
0
        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
        }