コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            Random        r      = new Random();
            ASCIIEncoding aSCII  = new ASCIIEncoding();
            IPEndPoint    snd    = new IPEndPoint(IPAddress.Any, 0);
            EndPoint      Remote = snd;

            byte[] buf;
            button1.Enabled = false;
            while (true)
            {
                buf = new byte[128];
                int size = socket.ReceiveFrom(buf, ref Remote);
                if (size > 0)
                {
                    string[] str = aSCII.GetString(buf).Split(':');
                    nameA = str[0];
                    nameB = str[1];
                    Int32.TryParse(str[2], out Ra);
                    AtKey = (nameA + "trent").GetHashCode() & 0x7FFFFFFF;
                    BtKey = (nameB + "trent").GetHashCode() & 0x7FFFFFFF;
                    string s        = sesKey.ToString() + ':' + nameA;
                    byte[] crS      = crypt(s, BtKey);
                    byte[] crS2     = crypt(Ra.ToString() + ':' + nameB + ':' + sesKey.ToString() + ':', AtKey);
                    byte[] crS3     = crypt(crS, AtKey);
                    byte[] newArray = new byte[crS2.Length + crS3.Length];
                    crS2.CopyTo(newArray, 0);
                    crS3.CopyTo(newArray, crS2.Length);
                    socket.SendTo(newArray, Remote);
                    button1.Enabled = true;
                    return;
                }
            }
        }
コード例 #2
0
 private void ans_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Up)
     {
         Ra.Focus();
     }
 }
コード例 #3
0
        /* public void FirstCallBack(IAsyncResult aRes)
         * {
         *   try
         *   {
         *
         *       int size = socket.EndReceiveFrom(aRes, ref remotePoint);
         *       byte[] buf = new byte[50];
         *       if (size > 0)
         *       {
         *           byte[] recData = new byte[64];
         *           recData = (byte[])aRes.AsyncState;
         *
         *           ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
         *           string msg = aSCIIEncoding.GetString(recData);
         *           if (initK)
         *           {
         *               if (initX)
         *               {
         *                   if (initNG)
         *                   {
         *                       string[] ng = msg.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
         *                       n = Convert.ToInt64(ng[0]);
         *                       g = Convert.ToInt64(ng[1]);
         *                       initNG = false;
         *                       //msgList.Items.Add("Friend: " + msg);
         *                       socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(FirstCallBack), buf);
         *                       return;
         *                   }
         *                   if (x == 0)
         *                   {
         *                       Random r = new Random();
         *                       x = r.Next(500000, 5000000);
         *                       X = CryptoMath.PowerMod(g, x, n);
         *                       ASCIIEncoding asc = new ASCIIEncoding();
         *                       byte[] msgT = new byte[1500];
         *                       msgT = asc.GetBytes(X.ToString());
         *                       socket.Send(msgT);
         *                       socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(FirstCallBack), buf);
         *
         *                   }
         *                   string xf = aSCIIEncoding.GetString(recData);
         *                   XF = Convert.ToInt64(xf);
         *                   //msgList.Items.Add("Friend XF: " + xf);
         *                   initX = false;
         *
         *               }
         *               Ki = CryptoMath.PowerMod(XF, x, n);
         *               initK = false;
         *               socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(FirstCallBack), buf);
         *               return;
         *           }
         *           byte[] msgb = crypt(recData, Ki);
         *           msg = aSCIIEncoding.GetString(msgb);
         *           msgList.Items.Add("Friend: " + msg);
         *       }
         *
         *       socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(FirstCallBack), buf);
         *
         *
         *   }
         *   catch (Exception e)
         *   {
         *       MessageBox.Show(e.ToString());
         *   }
         * }*/

        private void sendButton_Click(object sender, EventArgs e)
        {
            try
            {
                ASCIIEncoding asc = new ASCIIEncoding();
                Random        r   = new Random();
                Ra = r.Next(100000, 99999999);
                byte[] buf = new byte[50];
                if (firstMsg == "")
                {
                    socket.Connect(trentPoint);
                    firstMsg = textBoxMessage.Text;
                    string msg = name + ':' + friendName + ':' + Ra.ToString();
                    //msgList.Items.Add("Отправили тренту");
                    takeRa = true;
                    takeBa = true;
                    socket.SendTo(asc.GetBytes(msg), trentPoint);
                    socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref trentPoint, new AsyncCallback(KeyCallBack), buf);
                    textBoxMessage.Clear();
                }
                else
                {
                    //byte[] msg = new byte[1500];
                    byte[] cMsg = crypt(textBoxMessage.Text, K);
                    //msg = asc.GetBytes(cMsg);
                    socket.SendTo(cMsg, remotePoint);
                    msgList.Items.Add("You: " + textBoxMessage.Text);
                    textBoxMessage.Clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
コード例 #4
0
 private void Confirm_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Enter)
     {
         Ra.Focus();
     }
     if (e.KeyCode == Keys.Up)
     {
         Password.Focus();
     }
 }
コード例 #5
0
 public void KeyCallBack(IAsyncResult aRes)
 {
     try
     {
         int size = socket.EndReceiveFrom(aRes, ref trentPoint);
         Thread.Sleep(700);
         socket.Connect(remotePoint);
         byte[] buf = new byte[128];
         Random r   = new Random();
         if (size > 0)
         {
             byte[] recData = new byte[128];
             recData = (byte[])aRes.AsyncState;
             ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
             if (init)
             {
                 if (firstMsg == "")
                 {
                     recData = recData.Skip(1).ToArray();
                     byte[]   decrMsg = crypt(recData, Kt);
                     string   dMsg    = aSCIIEncoding.GetString(decrMsg);
                     string[] g       = dMsg.Split(':');
                     K  = Convert.ToInt32(g[0]);
                     Rb = r.Next(10000, 9999999);
                     byte[] crMsg = crypt(Rb.ToString(), K);
                     socket.SendTo(crMsg, remotePoint);
                     socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(KeyCallBack), buf);
                     firstMsg = "1";
                     return;
                 }
                 if (takeRa)
                 {
                     byte[]   decrMsg = crypt(recData, Kt);
                     int      n       = Array.LastIndexOf(decrMsg, (byte)58);
                     byte[]   b       = decrMsg.Take(n).ToArray();
                     byte[]   b2      = decrMsg.Skip(n).ToArray();
                     string[] dMsg    = aSCIIEncoding.GetString(b).Split(':');
                     takeRa = false;
                     if (Ra.ToString() != dMsg[0])
                     {
                         MessageBox.Show("Ra не совпадает!");
                         return;
                     }
                     else
                     {
                         K = Convert.ToInt32(dMsg[2]);
                         socket.SendTo(b2, remotePoint);
                         socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(KeyCallBack), buf);
                         return;
                     }
                 }
                 if (takeBa)
                 {
                     //byte[] recData = new byte[128];
                     recData = (byte[])aRes.AsyncState;
                     //ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
                     byte[] decrMsg = crypt(recData, K);
                     byte[] b       = new byte[decrMsg.Length];
                     for (int i = 0; i < decrMsg.Length; i++)
                     {
                         if (48 > decrMsg[i] || decrMsg[i] > 57)
                         {
                             Array.Copy(decrMsg, b, i);
                             break;
                         }
                     }
                     string s    = aSCIIEncoding.GetString(b);
                     int    Rb_1 = Convert.ToInt32(s) - 1;
                     socket.SendTo(crypt(Rb_1.ToString(), K), remotePoint);
                     msgList.Items.Add("Ключ установлен!");
                     decrMsg = crypt(firstMsg, K);
                     init    = false;
                     socket.SendTo(decrMsg, remotePoint);
                     msgList.Items.Add("You: " + firstMsg);
                     socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(KeyCallBack), buf);
                 }
                 else
                 {
                     byte[] decrMsg = crypt(recData, K);
                     byte[] b       = new byte[decrMsg.Length];
                     takeBa = false;
                     for (int i = 0; i < decrMsg.Length; i++)
                     {
                         if (48 > decrMsg[i] || decrMsg[i] > 57)
                         {
                             Array.Copy(decrMsg, b, i);
                             break;
                         }
                     }
                     string s = aSCIIEncoding.GetString(b);
                     if (Rb != Convert.ToInt32(s) + 1)
                     {
                         MessageBox.Show("Rb не совпадает!");
                         return;
                     }
                     else
                     {
                         msgList.Items.Add("Ключ установлен!");
                         init = false;
                         socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(KeyCallBack), buf);
                         return;
                     }
                 }
             }
             else
             {
                 byte[] encA = crypt(recData, K);
                 string rA   = aSCIIEncoding.GetString(encA);
                 if (rA.IndexOf('?') != -1)
                 {
                     rA = rA.Substring(0, rA.IndexOf('?'));
                 }
                 else if (rA.IndexOf('\t') != -1)
                 {
                     rA = rA.Substring(0, rA.IndexOf('\t'));
                 }
                 msgList.Items.Add(friendName + ": " + rA);
                 socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref remotePoint, new AsyncCallback(KeyCallBack), buf);
                 return;
             }
         }
         //socket.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref trentPoint, new AsyncCallback(KeyCallBack), buf);
     }
     catch (Exception e)
     {
         MessageBox.Show(e.ToString());
     }
 }