Example #1
0
        /// <summary>
        /// 读取公钥(进过des加密)
        /// </summary>
        /// <param name="key">传出字节类型的公钥</param>
        /// <param name="file">要读的文件名</param>
        public void ReadpublicKey(out byte[] key, string file)
        {
            string[] str = File.ReadAllLines(file, Encoding.Default);
            desKey   des = new desKey();

            key = strToToHexByte(des.Decrypt(str[0], "abcdefgh"));
        }
Example #2
0
        /// <summary>
        /// 读取私钥(进过des加密)
        /// </summary>
        /// <param name="prikey">传出大数类型的私钥</param>
        /// <param name="file">文件名</param>
        public void Readprikey(out BigInteger prikey, string file)
        {
            string[] str = File.ReadAllLines(file, Encoding.Default);
            desKey   des = new desKey();

            prikey = new BigInteger(des.Decrypt(str[0], "abcdefgh"), 16);
        }
Example #3
0
 /// <summary>
 /// 经过des加密后、写入秘钥(用户名pri.txt)
 /// </summary>
 /// <param name="userKey">公钥</param>
 /// <param name="file">要写入的文件名</param>
 public void WriterKey(string userKey, string file)
 {
     try
     {
         string     path = file;
         FileStream Ali;
         desKey     des = new desKey();
         Ali = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件
         StreamWriter sr = new StreamWriter(Ali);
         sr.WriteLine(des.Encrypt(userKey, "abcdefgh"));
         // MessageBox.Show("\r\n公钥长度:" + userKey.Length);
         sr.Close();
     }
     catch (Exception ee)
     {
         MessageBox.Show("ecc类异常 2\r\n" + ee.ToString());
     }
 }
Example #4
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() + "%";
            }
        }
Example #5
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());
            }
        }
Example #6
0
        /// <summary>
        /// 将rs写入一个文件下(不经过des加密)
        /// </summary>
        /// <param name="r">R</param>
        /// <param name="s">S</param>
        /// <param name="rsfile">rs要存入的文件夹路径</param>
        public void Writers(BigInteger r, BigInteger s, string rsfile)//写入r,s,签名者公钥,用于验证
        {
            try
            {
                FileStream Ali;
                string     path = rsfile;
                desKey     des  = new desKey();
                Ali = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件
                StreamWriter sr = new StreamWriter(Ali);

                //RS
                sr.Write(r.ToString(16));//开始写入值
                sr.Write("#");
                sr.Write(s.ToString(16));
                sr.Close();
            }
            catch (Exception ss)
            {
                MessageBox.Show("写入RS时出错!\r\n" + ss.ToString());

                throw;
            }
        }
Example #7
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;
            }
        }