コード例 #1
0
        private Package ProcessMessage(Package pck)
        {
            // Console.WriteLine("#Get Message Client: ");
            // pck.Print();

            Package response = new Package();
            RSA     certRSA  = new RSA(certificateKey);

            string[]   respCertf = certRSA.decrypt(pck.GetHeader("Certificate")).Split(';');
            BigInteger n         = BigInteger.Parse(respCertf[2]);
            BigInteger e         = BigInteger.Parse(respCertf[3]);
            int        idUser    = GetUserByPublicKey(n, e);

            switch (pck.GetHeader("Command"))
            {
            case "Connect":
                UserData user = new UserData(n, e, pck.GetHeader("Username"));
                users.Add(user);
                response.SetHeader("Certificate", certificate);
                break;

            case "Disconnect":
                users.RemoveAt(idUser);
                break;

            case "DES":
                string desEnc = pck.GetHeader("DES Key");
                users[idUser].desKey = rsa.decrypt(desEnc);

                RSA resRSA = new RSA(users[idUser].key);
                response.SetHeader("DES Key", resRSA.encrypt(ecbKey));
                break;

            case "Message":
                Message msg = new Message(users[idUser], ECB.decrypt(pck.GetContent(), users[idUser].desKey));
                bufferMessage.Add(msg);

                break;

            case "New Message":
                string lastIndex = ECB.decrypt(pck.GetContent(), users[idUser].desKey);
                int    index     = int.Parse(lastIndex);

                if (index < bufferMessage.Count)
                {
                    response.SetContent(bufferMessage[index].user.username + " = " + bufferMessage[index].message);
                }

                break;
            }

            if (response.GetContent() != "")
            {
                response.SetContent(ECB.encrypt(response.GetContent(), ecbKey));
            }

            return(response);
        }
コード例 #2
0
ファイル: Chat.cs プロジェクト: tafci/DES-Implementation
        private Package SendCommand(Package pck, int port)
        {
            TcpClient  client         = new TcpClient();
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);

            client.Connect(serverEndPoint);
            NetworkStream clientStream = client.GetStream();
            ASCIIEncoding encoder      = new ASCIIEncoding();

            if (pck.GetContent() != "")
            {
                pck.SetContent(ECB.encrypt(pck.GetContent(), ecbKey));
            }

            String s = pck.GetString();

            byte[] buffer = encoder.GetBytes(s);

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();

            byte[] bufferResponse = new byte[4096];
            clientStream.Read(bufferResponse, 0, 4096);

            String response = encoder.GetString(bufferResponse).Replace("\0", "");

            client.Close();

            Package resp = new Package();

            resp.SetByString(response);

            //Console.WriteLine("#Response From Server: ");
            //resp.Print();

            if (port == 1234 && resp.GetContent() != "")
            {
                resp.SetContent(ECB.decrypt(resp.GetContent(), publicDes));
            }

            return(resp);
        }