Пример #1
0
        private object[] BlockingReceive(sClient sC)
        {
            byte[] bSize = new byte[4];
            sC.cSocket.Receive(bSize);

            byte[] sBuf      = new byte[BitConverter.ToInt32(bSize, 0)];
            int    iReceived = 0;

            while (iReceived < sBuf.Length)
            {
                iReceived += sC.cSocket.Receive(sBuf, iReceived, sBuf.Length - iReceived, SocketFlags.None);
            }

            Log($"Receiving {sBuf.Length} bytes...", ConsoleColor.Cyan);

            if (sC.eCls != null)
            {
                sBuf = sC.eCls.AES_Decrypt(sBuf);
            }
            else
            {
                sBuf = Encryption.Decompress(sBuf);
            }

            return(Encryption.BinaryFormatterSerializer.Deserialize(sBuf));
        }
Пример #2
0
        private void AcceptCallback(IAsyncResult iAR)
        //Our method for accepting clients
        {
            var client = default(Socket);

            try
            {
                client = sSocket.EndAccept(iAR);
            }
            catch { return; } // This will happen when we shutdown the server

            Log($"Client connected from IP: {client.RemoteEndPoint}", ConsoleColor.Green, true);

            var sC = new sClient
            {
                cSocket = client,
                eCls    = null,
                bSize   = new byte[4],
                sClass  = new SharedClass()
            };

            Clients.Add(sC.cSocket.RemoteEndPoint, sC);

            BeginEncrypting(ref sC);

            sC.cSocket.BeginReceive(sC.bSize, 0, sC.bSize.Length, SocketFlags.None, RetrieveCallback, sC.cSocket.RemoteEndPoint);

            sSocket.BeginAccept(AcceptCallback, null);
        }
Пример #3
0
        private void BlockingSend(sClient sC, params object[] param)
        {
            byte[] bSend = Encryption.BinaryFormatterSerializer.Serialize(param);
            if (sC.eCls != null)
            {
                bSend = sC.eCls.AES_Encrypt(bSend);
            }
            else
            {
                bSend = Encryption.Compress(bSend);
            }

            Log($"Sending {bSend.Length} bytes...", ConsoleColor.Cyan);

            sC.cSocket.Send(BitConverter.GetBytes(bSend.Length));
            sC.cSocket.Send(bSend);
        }
Пример #4
0
        private void BeginEncrypting(ref sClient sC)
        {
            byte[] sSymKey;
            CngKey sCngKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP521);

            byte[] sPublic = sCngKey.Export(CngKeyBlobFormat.EccPublicBlob);

            BlockingSend(sC, Headers.HEADER_HANDSHAKE, sPublic);

            object[] oRecv = BlockingReceive(sC);

            if (!oRecv[0].Equals(Headers.HEADER_HANDSHAKE))
            {
                sC.cSocket.Disconnect(true);
            }

            byte[] cBuf = oRecv[1] as byte[];

            using (var sAlgo = new ECDiffieHellmanCng(sCngKey))
                using (CngKey cPubKey = CngKey.Import(cBuf, CngKeyBlobFormat.EccPublicBlob))
                    sSymKey = sAlgo.DeriveKeyMaterial(cPubKey);

            sC.eCls = new Encryption(sSymKey, HASH_STRENGTH.MINIMAL);
        }