Exemplo n.º 1
0
 public void sendMessage(string message, string receiver)
 {
     Chatmessage chatMessage = new Chatmessage();
     chatMessage.Transmitter = _user;
     chatMessage.Receiver = receiver;
     chatMessage.Message = message;
     byte[] msg = _clientEncryption.EncryptRijndael(Converter.fromObjectToByteArray(chatMessage));
     sendMessage(msg);
 }
Exemplo n.º 2
0
 public void openUserInfo(object sender, EventArgs e)
 {
     Chatmessage message = new Chatmessage();
     message.MessageType = Chatmessage.MESSAGE_TYPE_USER_INFO;
     message.Transmitter = txtUser.Text;
     message.Receiver = listUser.SelectedItem.ToString();
     message.Message = "userInfo";
     _client.sendMessage(message);
 }
Exemplo n.º 3
0
        public void InitializeConnection(string IP, string User)
        {
            _user = User;
            IPAddress ipAddress = IPAddress.Parse(IP);
            byte[] response;
            byte[] answer;

            // Check if IP is reachable
            bool check = checkIfServerIsReachable(ipAddress);

            if (check == false)
            {
                throw new Exception("Server ist zur Zeit nicht erreichbar!");
            }

            // erstmal verbinden
            try {
                _tcpServer = new TcpClient();
                _tcpServer.Connect(ipAddress, _port);
            }
            catch (Exception)
            {
                _tcpServer = null;
                throw new Exception("Server ist zur Zeit nicht erreichbar! (Antwortet nicht auf Anfragen)");
            }

            _swSender = new BinaryWriter(_tcpServer.GetStream());
            _srReceiver = new BinaryReader(_tcpServer.GetStream());

            // sende meinen public key zum server
            byte[] myPubKey = Converter.fromStringToByteArray(_clientEncryption.getPublicKey());
            sendMessage(myPubKey);

            // empfange rij key + iv vom server (verschlüsselt mit meinem pub key)
            Int32 length;
            length = _srReceiver.ReadInt32();
            response = new byte[length];
            response = _srReceiver.ReadBytes(length);
            byte[] rijKey = _clientEncryption.DecryptRSA(response);
            length = _srReceiver.ReadInt32();
            response = new byte[length];
            response = _srReceiver.ReadBytes(length);
            byte[] rijIV = _clientEncryption.DecryptRSA(response);

            // setup rij für spätere kommunikation
            _clientEncryption.setUpRijndael(rijKey, rijIV);

            // sende chatmessage klasse mit signature, usernamen und willkommensnachricht
            Library.Chatmessage message = new Library.Chatmessage();
            message.Transmitter = User;
            message.Message = "Welcome";
            message.MessageType = Library.Chatmessage.MESSAGE_TYPE_CONNECT;
            message.OperatingSystem = Environment.OSVersion;

            answer = _clientEncryption.EncryptRijndael(Converter.fromObjectToByteArray(message));
            sendMessage(answer);

            _thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
            _thrMessaging.IsBackground = true;
            _thrMessaging.Start();
            connected = true;
        }
Exemplo n.º 4
0
 public void sendMessage(Chatmessage message)
 {
     byte[] msg = _clientEncryption.EncryptRijndael(Converter.fromObjectToByteArray(message));
     sendMessage(msg);
 }
 public StatusChangedEventArgs(Chatmessage message)
 {
     EventMsg = message;
 }
Exemplo n.º 6
0
        public void closeConnection()
        {
            if (connected) {
                connected = false;
                Chatmessage message = new Chatmessage();
                message.Transmitter = _user;
                message.Receiver = "global";
                message.Message = "Goodbye";
                message.MessageType = Chatmessage.MESSAGE_TYPE_DISCONNECT;
                sendMessage(message);
            }

            _users.Clear();
        }
 public StatusChangedEventArgs(Chatmessage message)
 {
     EventMsg = message;
 }
Exemplo n.º 8
0
        private void AcceptClient()
        {
            srReceiver = new System.IO.BinaryReader(tcpClient.GetStream());
            swSender = new System.IO.BinaryWriter(tcpClient.GetStream());

            byte[] answer;
            byte[] response;
            Int32 length;
            // lese laenge von pub key von client
            length = srReceiver.ReadInt32();
            // empfange client pub key
            response = new byte[length];
            response = srReceiver.ReadBytes(length);
            string clientPubKey = Converter.fromByteArrayToString(response);
            Encryption clientEncryption = new Encryption(clientPubKey);

            // sende mit client pub key verschlüsselt rij key + iv
            answer = clientEncryption.EncryptRSA(serverEncryption.getRijKey());
            sendMessage(answer);
            answer = clientEncryption.EncryptRSA(serverEncryption.getRijIV());
            sendMessage(answer);

            // empfange chatmessage objekt mit usernamen signature und message
            length = srReceiver.ReadInt32();
            response = new byte[length];
            response = srReceiver.ReadBytes(length);

            Chatmessage message = (Chatmessage) Converter.fromByteArrayToObject(serverEncryption.DecryptRijndael(response));
            int pos = 0;
            if (message.Signature != _acceptedSignature)
            {
                pos = -1;
            }
            currUser = message.Transmitter;
            if (currUser != "")
            {
                Chatmessage serverResponse = new Chatmessage();
                serverResponse.Transmitter = "Administrator";
                serverResponse.Receiver = currUser;
                if (pos == -1)
                {
                    serverResponse.Message = "0|Wrong Client.";
                    byte[] msg = serverEncryption.EncryptRijndael(Converter.fromObjectToByteArray(serverResponse));
                    sendMessage(msg);
                    CloseConnection();
                    return;
                }
                else if (ChatServer.userInfos.ContainsKey(currUser) == true)
                {
                    serverResponse.Message = "0|This username already exists.";
                    byte[] msg = serverEncryption.EncryptRijndael(Converter.fromObjectToByteArray(serverResponse));
                    sendMessage(msg);
                    CloseConnection();
                    return;
                }
                else if (currUser == "Administrator")
                {
                    serverResponse.Message = "0|This username is reserved.";
                    byte[] msg = serverEncryption.EncryptRijndael(Converter.fromObjectToByteArray(serverResponse));
                    sendMessage(msg);
                    CloseConnection();
                    return;
                }
                else
                {
                    serverResponse.Message = "1|Connected Successfully";
                    byte[] msg = serverEncryption.EncryptRijndael(Converter.fromObjectToByteArray(serverResponse));
                    sendMessage(msg);

                    // send actual user list
                    MemoryStream stream = new MemoryStream();
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(stream, ChatServer.users);
                    msg = stream.ToArray();
                    sendMessage(msg);

                    // add new user
                    ChatServer.AddUser(tcpClient, currUser, serverEncryption, message.OperatingSystem);
                }
            }
            else
            {
                CloseConnection();
                return;
            }

            try
            {
                while ((length = Convert.ToInt32(srReceiver.ReadInt32())) != 0)
                {
              				 response = new byte[length];
                     response = srReceiver.ReadBytes(length);
                     var rawResponse = Converter.fromByteArrayToObject(serverEncryption.DecryptRijndael(response));
                     Chatmessage chatResponse = (Chatmessage) rawResponse;

                     switch (chatResponse.MessageType) {
                     	case Chatmessage.MESSAGE_TYPE_DISCONNECT:
                     		// client wants do disconnect
                     		ChatServer.RemoveUser(currUser);
                     		break;
                     	case Chatmessage.MESSAGE_TYPE_USER_INFO:
                     		// send user info of a user to client
                     		ChatServer.SendMessage(currUser, chatResponse.Message, chatResponse.Receiver, Chatmessage.MESSAGE_TYPE_USER_INFO);
                     		break;
                     	case Chatmessage.MESSAGE_TYPE_MESSAGE:
                 		default:
                     		// normal chat message or default behaviour
                     		if (chatResponse.Message.Length > 0) {
                     			ChatServer.SendMessage(currUser, chatResponse.Message, chatResponse.Receiver, Chatmessage.MESSAGE_TYPE_MESSAGE);
                     		} else {
                     			ChatServer.RemoveUser(currUser);
                     		}
                 			break;
                     }
                }
            }
            catch
            {
                ChatServer.RemoveUser(currUser);
            }
        }
Exemplo n.º 9
0
 protected static void sendMessage(Chatmessage message, User user)
 {
     BinaryWriter swSender;
     try
     {
         Encryption tmp = user.Encryption;
         swSender = new BinaryWriter(user.TcpClient.GetStream());
         byte[] sendMessage = tmp.EncryptRijndael(Converter.fromObjectToByteArray(message));
         swSender.Write(sendMessage.Length);
         swSender.Flush();
         swSender.Write(sendMessage);
         swSender.Flush();
         swSender = null;
     }
     catch
     {
         RemoveUser(user.Username);
     }
 }
Exemplo n.º 10
0
        public static void SendMessage(string From, string Message, string Receiver, string MessageType)
        {
            // overwrite message with specific user info message
            if (MessageType == Chatmessage.MESSAGE_TYPE_USER_INFO) {
                Message = "Login: "******" "
                    + ChatServer.userInfos[From].LoginTime.ToLongTimeString()
                    + Environment.NewLine
                    + "Operating System: " + ChatServer.userInfos[From].OperatingSystem.VersionString;
            }
            Chatmessage userMessage = new Chatmessage(From, Receiver, Message, MessageType);
            e = new StatusChangedEventArgs(userMessage);
            OnStatusChanged(e);

            if (Receiver == "global")
            {
                foreach (KeyValuePair<string, User> entry in ChatServer.userInfos)
                {
                    sendMessage(userMessage, entry.Value);
                }
            }
            else
            {
                sendMessage(userMessage, ChatServer.userInfos[Receiver]);
            }
        }
Exemplo n.º 11
0
        public static void SendAdminMessage(string Message)
        {
            Chatmessage adminMessage = new Chatmessage("Administrator", "global", Message, Library.Chatmessage.MESSAGE_TYPE_MESSAGE);

            e = new StatusChangedEventArgs(adminMessage);
            OnStatusChanged(e);

            foreach (KeyValuePair<string, User> entry in ChatServer.userInfos) {
                sendMessage(adminMessage, entry.Value);
            }
        }
Exemplo n.º 12
0
        public void InitializeConnection(string IP, string User)
        {
            _user = User;
            IPAddress ipAddress = IPAddress.Parse(IP);

            byte[] response;
            byte[] answer;

            // Check if IP is reachable
            bool check = checkIfServerIsReachable(ipAddress);

            if (check == false)
            {
                throw new Exception("Server ist zur Zeit nicht erreichbar!");
            }

            // erstmal verbinden
            try {
                _tcpServer = new TcpClient();
                _tcpServer.Connect(ipAddress, _port);
            }
            catch (Exception)
            {
                _tcpServer = null;
                throw new Exception("Server ist zur Zeit nicht erreichbar! (Antwortet nicht auf Anfragen)");
            }

            _swSender   = new BinaryWriter(_tcpServer.GetStream());
            _srReceiver = new BinaryReader(_tcpServer.GetStream());

            // sende meinen public key zum server
            byte[] myPubKey = Converter.fromStringToByteArray(_clientEncryption.getPublicKey());
            sendMessage(myPubKey);

            // empfange rij key + iv vom server (verschlüsselt mit meinem pub key)
            Int32 length;

            length   = _srReceiver.ReadInt32();
            response = new byte[length];
            response = _srReceiver.ReadBytes(length);
            byte[] rijKey = _clientEncryption.DecryptRSA(response);
            length   = _srReceiver.ReadInt32();
            response = new byte[length];
            response = _srReceiver.ReadBytes(length);
            byte[] rijIV = _clientEncryption.DecryptRSA(response);

            // setup rij für spätere kommunikation
            _clientEncryption.setUpRijndael(rijKey, rijIV);

            // sende chatmessage klasse mit signature, usernamen und willkommensnachricht
            Library.Chatmessage message = new Library.Chatmessage();
            message.Transmitter     = User;
            message.Message         = "Welcome";
            message.MessageType     = Library.Chatmessage.MESSAGE_TYPE_CONNECT;
            message.OperatingSystem = Environment.OSVersion;

            answer = _clientEncryption.EncryptRijndael(Converter.fromObjectToByteArray(message));
            sendMessage(answer);

            _thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
            _thrMessaging.IsBackground = true;
            _thrMessaging.Start();
            connected = true;
        }
Exemplo n.º 13
0
 private void UpdateStatus(Chatmessage message)
 {
     if (message.MessageType == Chatmessage.MESSAGE_TYPE_USER_INFO) {
         return;
     }
     if (message.Transmitter == "Administrator") {
         UpdateStatus(message.Message);
     } else {
         if (System.Configuration.ConfigurationManager.AppSettings["LogUserMessages"] == "true") {
             UpdateStatus(message.Message);
         }
     }
 }