void Send()
        {
            NetworkStream networkStream = new NetworkStream(this.ClientSocket);

            while (true)
            {
                string  str     = Console.ReadLine();
                Postman postMan = new Postman();

                switch (str.ToUpper())
                {
                case "SEND_KEY":
                    postMan.Type    = Postman.PostmanType.SEND_KEY;
                    postMan.Payload = Mysterio.ConvertKeyToBytes(this.ClientMysterio.PubKey);
                    break;

                default:
                    postMan.Type    = Postman.PostmanType.SEND_MESSAGE;
                    postMan.Payload = Mysterio.Encrypt(this.ServerMysterio.PubKey, str);
                    break;
                }

                Postman.SendPackage(networkStream, postMan);
                if (str.ToUpper().Equals("QUIT"))
                {
                    break;
                }
            }

            networkStream.Close();
        }
        void Receive()
        {
            NetworkStream networkStream = new NetworkStream(this.ClientSocket);

            while (true)
            {
                Postman postMan = Postman.GetPackage(networkStream);
                Console.WriteLine(postMan);
                switch (postMan.Type)
                {
                case Postman.PostmanType.SEND_KEY:
                    this.ServerMysterio.PubKey = Mysterio.ConvertBytesToKey(postMan.Payload);
                    break;

                case Postman.PostmanType.SEND_MESSAGE:
                    string plainText = Mysterio.Decrypt(this.ClientMysterio.PriKey, postMan.Payload);
                    Console.WriteLine(plainText);
                    break;
                }
                if (postMan.Type == Postman.PostmanType.DISCONNECT)
                {
                    break;
                }
            }
            networkStream.Close();
        }
Example #3
0
        public static void Main(string[] args)
        {
            Console.InputEncoding  = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;

            Mysterio s = new Mysterio(2048);

            Mysterio c = new Mysterio(2048);

            byte[] sPri = Mysterio.ConvertKeyToBytes(s.PriKey);

            byte[] cPri = Mysterio.ConvertKeyToBytes(c.PriKey);

            string plain = "Gửi từ server";

            byte[] s_to_c = Mysterio.Encrypt(c.PubKey, plain);

            string in_c_result = Mysterio.Decrypt(c.PriKey, s_to_c);

            Console.WriteLine("in_c_result: " + in_c_result);

            plain = "gưởi từ client";

            byte[] c_to_s = Mysterio.Encrypt(s.PubKey, plain);

            string in_s_result = Mysterio.Decrypt(s.PriKey, c_to_s);

            Console.WriteLine("in_s_result: " + in_s_result);

            Console.ReadLine();
        }
Example #4
0
        private void Receive()
        {
            try
            {
                NetworkStream netStream = new NetworkStream(this.ReceiveSocket);
                while (true)
                {
                    Postman postman = Postman.GetPackage(netStream);
                    RenderForm();
                    switch (postman.Type)
                    {
                    case Postman.PostmanType.SEND_KEY:
                        this.OtherMysterio.PubKey = Mysterio.ConvertBytesToKey(postman.Payload);
                        break;

                    case Postman.PostmanType.SEND_MESSAGE:
                        string plainText = Mysterio.Decrypt(this.MyMysterio.PriKey, postman.Payload);
                        ThreadHelper.AppendMessages(this, txtMessages, DateTime.Now.ToString("MM/dd/yyyy h:mm:ss tt\t") + plainText, false);
                        break;
                    }
                    AddEvent(postman.Type.ToString(), Encoding.Unicode.GetString(postman.Payload));
                }
            }catch (Exception e)
            {
                //MessageBox.Show(e.Message);
                Stop();
            }
        }
Example #5
0
        private void Send()
        {
            NetworkStream netStream = new NetworkStream(this.ReceiveSocket);
            Postman       postman   = new Postman()
            {
                Type    = Postman.PostmanType.SEND_KEY,
                Payload = Mysterio.ConvertKeyToBytes(this.MyMysterio.PubKey)
            };

            Postman.SendPackage(netStream, postman);
            AddEvent(postman.Type.ToString(), Encoding.Unicode.GetString(postman.Payload));
        }
Example #6
0
        private void Send(string mess)
        {
            NetworkStream netStream = new NetworkStream(this.ReceiveSocket);
            Postman       postman   = new Postman()
            {
                Type    = Postman.PostmanType.SEND_MESSAGE,
                Payload = Mysterio.Encrypt(this.OtherMysterio.PubKey, mess)
            };

            Postman.SendPackage(netStream, postman);
            AddEvent(postman.Type.ToString(), Encoding.Unicode.GetString(postman.Payload));
        }
 public Client(string serverIP, int serverPort)
 {
     this.ClientMysterio = new Mysterio(2048);
     this.ServerMysterio = new Mysterio();
     this.ServerIp       = serverIP;
     this.ServerPort     = serverPort;
     this.IpEndPoint     = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
     try
     {
         this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         Start();
     }
     catch (Exception e)
     {
         Stop();
         Console.WriteLine(e.Message);
     }
 }
 public Server(int serverPort)
 {
     this.ServerMysterio = new Mysterio(2048);
     this.ClientMysterio = new Mysterio();
     this.ServerPort     = serverPort;
     this.IpEndPoint     = new IPEndPoint(IPAddress.Any, serverPort);
     try
     {
         this.ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         ServerSocket.Bind(this.IpEndPoint);
         Start();
     }
     catch (Exception e)
     {
         Stop();
         Console.WriteLine(e.Message);
     }
 }