Exemplo n.º 1
0
        private static void DoAccept(IAsyncResult res)
        {
            s = (Socket)res.AsyncState;
            c = s.EndAccept(res);
            s.BeginAccept(new AsyncCallback(DoAccept), s);
            SocketObject sObject = new SocketObject {
                clientSocket = c
            };

            ipendp = (IPEndPoint)c.RemoteEndPoint;
            Console.WriteLine(ipendp.Address + " --CONNECT--");

            /* öffentlicher Schlüssel wird serialisiert und verschickt */
            XmlSerializer xml_ser = new XmlSerializer(typeof(KeyStruct));
            MemoryStream  str     = new MemoryStream();

            xml_ser.Serialize(str, publicKey);
            buffer = str.ToArray();
            c.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(DoSend), c);
            str.Close();

            /* Nachrichten werden empfangen */
            sObject.buffer = new byte[4096];
            c.BeginReceive(sObject.buffer, 0, 4096, SocketFlags.None, new AsyncCallback(DoReceive), sObject);
        }
Exemplo n.º 2
0
        private static void DoReceive(IAsyncResult res)
        {
            try
            {
                SocketObject sObject = (SocketObject)res.AsyncState;
                sObject.clientSocket.EndReceive(res);

                XmlSerializer xml_ser = new XmlSerializer(typeof(KeyStruct));
                MemoryStream  str     = new MemoryStream(sObject.buffer);
                /* Objekt deserialisieren und entschlüsseln */
                Console.WriteLine(ipendp.Address + " " + ElGamalDecrypt((KeyStruct)xml_ser.Deserialize(str), privateKey));
                str.Close();

                /* Wiederholen des Receive-Vorgangs */
                sObject.buffer = new byte[4096];
                sObject.clientSocket.BeginReceive(sObject.buffer, 0, 4096, SocketFlags.None, new AsyncCallback(DoReceive), sObject);
            }
            catch (Exception)
            {
                Console.WriteLine(ipendp.Address + " --DISCONNECT--\n");
            }
        }