/// <summary> /// Convert this color to an hex string with format #AARRGGBB /// </summary> /// <returns>Hex string.</returns> public string ColorToHex() { return(String.Format("#{0}{1}{2}{3}" , Ab.ToString("X").Length == 1 ? String.Format("0{0}", Ab.ToString("X")) : Ab.ToString("X") , Rb.ToString("X").Length == 1 ? String.Format("0{0}", Rb.ToString("X")) : Rb.ToString("X") , Gb.ToString("X").Length == 1 ? String.Format("0{0}", Gb.ToString("X")) : Gb.ToString("X") , Bb.ToString("X").Length == 1 ? String.Format("0{0}", Bb.ToString("X")) : Bb.ToString("X"))); }
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()); } }