Пример #1
0
        // 获取字符串的MD5值,1为UTF-8编码,2位Hex
        public static String GetMD5(String strSrc, int index)
        {
            try
            {
                if (strSrc == null)
                {
                    MessageBox.Show("输入数据不能为空!", "消息提示框");
                }

                byte[] byt = null;
                if (index == 1)
                {
                    byt = Encoding.UTF8.GetBytes(strSrc);
                }
                else if (index == 2)
                {
                    byt = MyTools.HexStringToBytes(strSrc);
                }

                MD5    md5    = new MD5CryptoServiceProvider();
                byte[] result = md5.ComputeHash(byt);

                return(MyTools.BytesToHexString(result));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "消息提示框");
            }

            return(null);
        }
Пример #2
0
        // 获取源数据UTF-8编码的byte,Hex编码的密钥,返回16进制字符串
        public static String Get3DES(String strSrc, string key)
        {
            SymmetricAlgorithm tdes = new TripleDESCryptoServiceProvider();

            // 密钥
            tdes.Key = MyTools.HexStringToBytes(key);
            // 初始值
            tdes.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            // 指定加密的运算模式
            tdes.Mode = System.Security.Cryptography.CipherMode.ECB;
            // 获取或设置加密算法的填充模式
            tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            // 块大小
            tdes.BlockSize = 64;

            ICryptoTransform ct;

            System.IO.MemoryStream ms;
            CryptoStream           cs;

            try
            {
                ct = tdes.CreateEncryptor(tdes.Key, tdes.IV);   // 创建加密对象

                byte[] byt = Encoding.UTF8.GetBytes(strSrc);    // 转换为byte数组
                int    len = byt.Length;                        // 获取数组长度

                int    diff = 8 - len % 8;                      // 距8的整数倍还差多少
                byte[] byt1 = new byte[len + diff];             // 定义大于byt的最小8的整数倍个空间

                Array.Copy(byt, 0, byt1, 0, byt.Length);        // 将与数据拷贝过去
                byt1[len] = 0x80;                               //
                for (int j = 0; j < diff - 1; j++)
                {
                    byt1[len + j + 1] = 0x00;
                }

                ms = new System.IO.MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt1, 0, byt1.Length);
                cs.FlushFinalBlock();
                if (cs != null)
                {
                    cs.Close();
                }

                byte[] temp   = ms.ToArray();
                byte[] result = new byte[temp.Length - 8];
                Array.Copy(temp, 0, result, 0, temp.Length - 8);

                return(MyTools.BytesToHexString(result));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "消息提示框");
            }

            return(null);
        }
Пример #3
0
 /** 显示数据 */
 private void ShowReadData(byte[] data)
 {
     this.BeginInvoke(new MethodInvoker(delegate
     {
         RecvRegionShow(MyTools.BytesToHexString(data), true);
         DataCountShow();
     }));
 }
Пример #4
0
        public static String Get3Mac(String srcStr, string key)
        {
            try
            {
                // 获取密钥
                string lKey = key.Substring(0, 16);
                string rKey = key.Substring(16, 16);

                // 初始值
                byte[] btIV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                // 获取3DES加密后的结果
                // byte[] bys = MyTools.HexStringToBytes(srcStr);
                String strTemp = MyEnctypt.Get3DES(srcStr, key);                         // 该方法获取源数据UTF-8编码的byte,Hex编码的密钥,返回16进制字符串
                byte[] md5     = MyTools.HexStringToBytes(MyEnctypt.GetMD5(strTemp, 2)); // 获取字符串的MD5值,1为UTF-8编码,2为Hex

                byte[] bysTemp  = new byte[8];                                           // 临时数组
                byte[] bysTemp1 = new byte[8];                                           // 临时数组

                Array.Copy(md5, 0, bysTemp, 0, 8);                                       // 将md5的前8个byte拷贝到bysTemp中
                byte[] bysTemp2 = DoXOR(bysTemp, btIV);                                  // 将前8个byte和初始值求异或

                for (int i = 1; i < md5.Length / 8; i++)                                 // 轮流DES加密、异或
                {
                    Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8);               // 加密
                    Array.Copy(md5, i * 8, bysTemp1, 0, 8);                              // 获取下一组
                    Array.Copy(DoXOR(bysTemp, bysTemp1), 0, bysTemp2, 0, 8);             // 异或
                }

                byte[] tail = new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8);  // 加密
                Array.Copy(DoXOR(bysTemp, tail), 0, bysTemp2, 0, 8);    // 异或

                Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8);  // 左8个字节密钥加密
                Array.Copy(Decrypt(bysTemp, rKey), 0, bysTemp2, 0, 8);  // 右8个字节密钥解密
                Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8);  // 左8个字节密钥加密

                return(MyTools.BytesToHexString(bysTemp));              // 返回16进制字符串
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "消息提示框");
            }

            return(null);
        }
Пример #5
0
        /** 计算MD5 */
        private void btnCheckMD5Execute01_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textMD5Path01.Text != null && !"".Equals(this.textMD5Path01.Text))
                {
                    FileStream file = new FileStream(this.textMD5Path01.Text, FileMode.Open);
                    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    byte[] retVal = md5.ComputeHash(file);
                    file.Close();

                    this.textMD5Result01.Text = MyTools.BytesToHexString(retVal);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("检查文件是否存在!" + ex.Message, "错误");
            }
        }
Пример #6
0
        private void btnCheckResultSHA1_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtBoxCheckResultSHA1.Text = string.Empty;
                if (this.txtBoxCheckSelectSHA1.Text != null && !"".Equals(this.txtBoxCheckSelectSHA1.Text))
                {
                    FileStream file = new FileStream(this.txtBoxCheckSelectSHA1.Text, FileMode.Open);
                    byte[]     retVal;
                    if (this.radioButtonSHA512.Checked)
                    {
                        SHA512 sha512 = new SHA512Managed();
                        retVal = sha512.ComputeHash(file);
                        sha512.Clear();
                    }
                    else if (this.radioButtonSHA384.Checked)
                    {
                        SHA384 sha384 = new SHA384Managed();
                        retVal = sha384.ComputeHash(file);
                        sha384.Clear();
                    }
                    else if (this.radioButtonSHA256.Checked)
                    {
                        SHA256 sha256 = new SHA256Managed();
                        retVal = sha256.ComputeHash(file);
                        sha256.Clear();
                    }
                    else
                    {
                        SHA1 sha1 = new SHA1CryptoServiceProvider();
                        retVal = SHA1.Create().ComputeHash(file);
                        sha1.Clear();
                    }
                    file.Close();

                    this.txtBoxCheckResultSHA1.Text = MyTools.BytesToHexString(retVal);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("检查文件是否存在!" + ex.Message, "错误");
            }
        }
Пример #7
0
 /** 计算方法 */
 private string ChineseCharToHex_Method(string str)
 {
     if (this.radioButton02_01.Checked)
     {
         byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(str);
         if (this.radioButton02_03.Checked)
         {
             return(MyTools.addBlank(MyTools.BytesToHexString(bytes), int.Parse(this.textBox02_01.Text), false));
         }
         return(MyTools.BytesToHexString(bytes));
     }
     else
     {
         byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(str);
         if (this.radioButton02_03.Checked)
         {
             return(MyTools.addBlank(MyTools.BytesToHexString(bytes), int.Parse(this.textBox02_01.Text), false));
         }
         return(MyTools.BytesToHexString(bytes));
     }
 }
Пример #8
0
 private void btnEncryptDES2_Click(object sender, EventArgs e)
 {
     byte[] bys    = Encoding.UTF8.GetBytes(textEncryptDESData.Text);
     byte[] result = MyEnctypt.Encrypt(bys, textEncryptDESKey.Text);
     textEncryptDESResult.Text = MyTools.BytesToHexString(result);
 }
Пример #9
0
 public void SendMsg(string msg)
 {
     if (msg == string.Empty)
     {
         MessageBox.Show("发送区内容为空!", "警告");
     }
     else
     {
         if (NetworkForm.network.listBoxOnline.SelectedItems.Count == 0)
         {
             this.ShowMsg("【未选择客户端】则发送给所有的客户端!");
             foreach (var item in NetworkForm.network.listBoxOnline.Items)
             {
                 Socket client = null;
                 if (dictServerSocketConn.TryGetValue(item.ToString(), out client))
                 {
                     byte[] bytes = null;
                     if (NetworkForm.network.radioBtnSendGBK.Checked)
                     {
                         bytes = Encoding.GetEncoding("GBK").GetBytes(msg);
                         if (NetworkForm.network.chboxDisplaySend.Checked && NetworkForm.network.radioBtnHEXSendYes.Checked)
                         {
                             this.ShowMsg("【发送到:" + item.ToString() + "】" + MyTools.BytesToHexString(bytes, true));
                         }
                         else if (NetworkForm.network.chboxDisplaySend.Checked && NetworkForm.network.radioBtnHEXSendNo.Checked)
                         {
                             this.ShowMsg("【发送到:" + item.ToString() + "】" + msg);
                         }
                         else
                         {
                         }
                     }
                     else
                     {
                         bytes = Encoding.GetEncoding("UTF-8").GetBytes(msg);
                         if (NetworkForm.network.chboxDisplaySend.Checked && NetworkForm.network.radioBtnHEXSendYes.Checked)
                         {
                             this.ShowMsg("【发送到:" + item.ToString() + "】" + MyTools.BytesToHexString(bytes, true));
                         }
                         else if (NetworkForm.network.chboxDisplaySend.Checked && NetworkForm.network.radioBtnHEXSendNo.Checked)
                         {
                             this.ShowMsg("【发送到:" + item.ToString() + "】" + msg);
                         }
                         else
                         {
                         }
                     }
                     client.Send(bytes);
                     NetworkForm.network.textBoxSendBytes.Text = (int.Parse(NetworkForm.network.textBoxSendBytes.Text) + bytes.Length).ToString();
                 }
             }
         }
         else
         {
             foreach (var item in NetworkForm.network.listBoxOnline.SelectedItems)
             {
                 Socket client = null;
                 if (dictServerSocketConn.TryGetValue(item.ToString(), out client))
                 {
                     byte[] bytes = null;
                     if (NetworkForm.network.radioBtnSendGBK.Checked)
                     {
                         bytes = Encoding.GetEncoding("GBK").GetBytes(msg);
                     }
                     else
                     {
                         bytes = Encoding.GetEncoding("UTF-8").GetBytes(msg);
                     }
                     client.Send(bytes);
                     NetworkForm.network.textBoxSendBytes.Text = (int.Parse(NetworkForm.network.textBoxSendBytes.Text) + bytes.Length).ToString();
                 }
             }
         }
     }
 }
Пример #10
0
        private void RecMsg(object sokConnectionparn)
        {
            Socket sokClient = sokConnectionparn as Socket;

            while (true)
            {
                // 定义一个缓存区
                byte[] arrMsgRec = new byte[1024];
                byte[] tmp;
                // 将接收到的数据保存
                int length = -1;
                try
                {
                    length = sokClient.Receive(arrMsgRec);
                    if (length > 0)
                    {
                        tmp = new byte[length];
                        NetworkForm.network.textBoxRecvBytes.Text = (int.Parse(NetworkForm.network.textBoxRecvBytes.Text) + length).ToString();
                        Array.Copy(arrMsgRec, tmp, length);
                        if (NetworkForm.network.chkbHEX.Checked)
                        {
                            ShowMsg("【" + sokClient.RemoteEndPoint.ToString() + "】" + MyTools.BytesToHexString(tmp, true).Trim());
                        }
                        else
                        {
                            if (NetworkForm.network.radioButtonUtf8.Checked)
                            {
                                ShowMsg("【" + sokClient.RemoteEndPoint.ToString() + "】" + Encoding.GetEncoding("UTF-8").GetString(tmp));
                            }
                            else
                            {
                                ShowMsg("【" + sokClient.RemoteEndPoint.ToString() + "】" + Encoding.GetEncoding("GBK").GetString(tmp));
                            }
                        }
                        if (NetworkForm.network.textSendRegion1.Text != null && NetworkForm.network.textSendRegion1.Text != string.Empty && NetworkForm.network.checkBoxSend1.Checked)
                        {
                            if (NetworkForm.network.textSendInterval.Text != string.Empty)
                            {
                                int sleep = int.Parse(NetworkForm.network.textSendInterval.Text);
                                Thread.Sleep(sleep);
                            }
                            byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(NetworkForm.network.textSendRegion1.Text);
                            sokClient.Send(bytes);
                            NetworkForm.network.textBoxSendBytes.Text = (int.Parse(NetworkForm.network.textBoxSendBytes.Text) + bytes.Length).ToString();
                        }
                        if (NetworkForm.network.textSendRegion2.Text != null && NetworkForm.network.textSendRegion2.Text != string.Empty && NetworkForm.network.checkBoxSend2.Checked)
                        {
                            if (NetworkForm.network.textSendInterval.Text != string.Empty)
                            {
                                int sleep = int.Parse(NetworkForm.network.textSendInterval.Text);
                                Thread.Sleep(sleep);
                            }
                            byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(NetworkForm.network.textSendRegion2.Text);
                            sokClient.Send(bytes);
                            NetworkForm.network.textBoxSendBytes.Text = (int.Parse(NetworkForm.network.textBoxSendBytes.Text) + bytes.Length).ToString();
                        }
                        if (NetworkForm.network.textSendRegion3.Text != null && NetworkForm.network.textSendRegion3.Text != string.Empty && NetworkForm.network.checkBoxSend3.Checked)
                        {
                            if (NetworkForm.network.textSendInterval.Text != string.Empty)
                            {
                                int sleep = int.Parse(NetworkForm.network.textSendInterval.Text);
                                Thread.Sleep(sleep);
                            }
                            byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(NetworkForm.network.textSendRegion3.Text);
                            sokClient.Send(bytes);
                            NetworkForm.network.textBoxSendBytes.Text = (int.Parse(NetworkForm.network.textBoxSendBytes.Text) + bytes.Length).ToString();
                        }
                    }
                    else
                    {
                        // 从 通信套接字 集合中删除被中断连接的通信套接字;
                        dictServerSocketConn.Remove(sokClient.RemoteEndPoint.ToString());
                        // 从通信线程集合中删除被中断连接的通信线程对象;
                        dictServerSocketThread.Remove(sokClient.RemoteEndPoint.ToString());
                        // 从列表中移除被中断的连接IP
                        NetworkForm.network.listBoxOnline.Items.Remove(sokClient.RemoteEndPoint.ToString());
                        ShowMsg("【" + sokClient.RemoteEndPoint.ToString() + "】" + "断开连接\r\n");

                        break;
                    }
                }
                catch (SocketException se)
                {
                    if (ServerFlag)
                    {
                        // 从通信套接字集合中删除被中断连接的通信套接字;
                        dictServerSocketConn.Remove(sokClient.RemoteEndPoint.ToString());
                        // 从通信线程集合中删除被中断连接的通信线程对象;
                        dictServerSocketThread.Remove(sokClient.RemoteEndPoint.ToString());
                        // 从列表中移除被中断的连接IP
                        NetworkForm.network.listBoxOnline.Items.Remove(sokClient.RemoteEndPoint.ToString());
                        ShowMsg("【异常消息】:" + se.Message + "\r\n");
                    }

                    break;
                }
                catch (Exception se)
                {
                    if (ServerFlag)
                    {
                        // 从 通信套接字 集合中删除被中断连接的通信套接字;
                        dictServerSocketConn.Remove(sokClient.RemoteEndPoint.ToString());
                        // 从通信线程集合中删除被中断连接的通信线程对象;
                        dictServerSocketThread.Remove(sokClient.RemoteEndPoint.ToString());
                        // 从列表中移除被中断的连接IP
                        NetworkForm.network.listBoxOnline.Items.Remove(sokClient.RemoteEndPoint.ToString());
                        ShowMsg("【异常消息】:" + se.Message + "\r\n");
                    }
                    break;
                }
            }
        }