Beispiel #1
0
        /// <summary>
        /// Sends an encrypted message to each connected client in the chat
        /// with their public key
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(Message message)
        {
            //convert message to XML
            string messageXml = Message.ToXML(message);
            List <ConnectedClientEncryptedMessage> clientEncryptedMessages = new List <ConnectedClientEncryptedMessage>();

            foreach (ConnectedClient client in this.GetConnectedClients())
            {
                string key = "";
                string IV  = "";

                string clientEncryptedMessage = Cryptor.AesEncrypt(messageXml, ref key, ref IV);
                string encryptedKey           = Cryptor.RsaEncrypt(key, client.PublicKey);
                string encryptedIV            = Cryptor.RsaEncrypt(IV, client.PublicKey);

                clientEncryptedMessages.Add(new ConnectedClientEncryptedMessage(client.UniqueID, this.UniqueID, encryptedKey, encryptedIV, clientEncryptedMessage));
            }

            JsonPacket packet = new JsonPacket(Constants.REQUEST_CLIENT_SEND_CHAT_MESSAGE);

            packet.Add("CLIENT_UNIQUE_ID", Client.Get().UniqueID);
            packet.Add("CLIENT_TOKEN", Client.Get().Token);
            packet.Add("CHAT_UNIQUE_ID", this.UniqueID);

            //attach client encryptedMessages
            packet.Add("CHAT_CLIENT_MESSAGES", JsonConvert.SerializeObject(clientEncryptedMessages));

            JsonPacket response = Request.Send(packet);
            //TODO: check response for errors
        }
Beispiel #2
0
        /// <summary>
        /// Checks the server for client messages sent by other users it then decrypts these messages storing them
        /// in the chat
        /// </summary>
        public void CheckForMessages()
        {
            JsonPacket packet = new JsonPacket(Constants.REQUEST_CHAT_CHECK_MESSAGE);

            packet.Add("CLIENT_UNIQUE_ID", Client.Get().UniqueID);
            packet.Add("CLIENT_TOKEN", Client.Get().Token);
            packet.Add("CHAT_UNIQUE_ID", this.UniqueID);

            JsonPacket response = Request.Send(packet);

            string messagesJSON = response.Data["MESSAGES"];

            List <ConnectedClientEncryptedMessage> messages = JsonConvert.DeserializeObject <List <ConnectedClientEncryptedMessage> >(messagesJSON);

            if (messages.Count > 0)
            {
                foreach (var message in messages)
                {
                    string decryptedKey = Cryptor.RsaDecrypt(message.EncryptedAESKey, Client.Get().CryptographicKeyPair.PrivateKey);
                    string decryptedIV  = Cryptor.RsaDecrypt(message.EncryptedAESIV, Client.Get().CryptographicKeyPair.PrivateKey);

                    Message msg = Message.FromXML(Cryptor.AesDecrypt(message.EncryptedMessage, decryptedKey, decryptedIV));
                    if (!Messages.Any(k => k.UniqueID == msg.UniqueID))
                    {
                        Messages.Add(msg);
                    }
                }
            }
        }
Beispiel #3
0
 public Message(string sender, string content)
 {
     UniqueID = Cryptor.GenerateRandomString(16, true);
     Sender   = sender;
     Content  = content;
 }